Commit b732f8d7 authored by Oliver Bartsch's avatar Oliver Bartsch Committed by Georg Ringer
Browse files

[TASK] Deprecate GeneralUtility::uniqueList

Since over a decade (according to the timestamp),
GeneralUtility::uniqueList() does not allow an array as
first argument and furthermore doesn't use the second
parameter anymore. Since then, both have thrown an
`InvalidArgumentException`. Furthermore, this method
does not belong to GeneralUtility at all.

Therefore this method is now deprecated and a new one,
using proper type hints, a return type, no second argument
and a correct PHPDoc is added to StringUtility.

All usages in the core are replaced with the new method,
ensuring proper types.

Resolves: #92607
Releases: master
Change-Id: Ibca5c2c312258dfc63c74f0ff11408d11226f7b6
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66190


Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz's avatarDaniel Goerz <daniel.goerz@posteo.de>
Tested-by: default avatarAlexander Schnitzler <git@alexanderschnitzler.de>
Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: Daniel Goerz's avatarDaniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: default avatarAlexander Schnitzler <git@alexanderschnitzler.de>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
parent 7e95ee3a
......@@ -57,6 +57,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Core\Versioning\VersionState;
/**
......@@ -141,7 +142,7 @@ class BackendUtility
$unsetMovePointers = false
) {
if ($fields !== '*') {
$internalFields = GeneralUtility::uniqueList($fields . ',uid,pid');
$internalFields = StringUtility::uniqueList($fields . ',uid,pid');
$row = self::getRecord($table, $uid, $internalFields, $where, $useDeleteClause);
self::workspaceOL($table, $row, -99, $unsetMovePointers);
if (is_array($row)) {
......
......@@ -50,6 +50,7 @@ use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Install\Service\SessionService;
/**
......@@ -1309,17 +1310,17 @@ class BackendUserAuthentication extends AbstractUserAuthentication
$this->dataLists['webmount_list'] = '0,' . $this->dataLists['webmount_list'];
}
// The lists are cleaned for duplicates
$this->groupData['webmounts'] = GeneralUtility::uniqueList($this->dataLists['webmount_list']);
$this->groupData['pagetypes_select'] = GeneralUtility::uniqueList($this->dataLists['pagetypes_select']);
$this->groupData['tables_select'] = GeneralUtility::uniqueList($this->dataLists['tables_modify'] . ',' . $this->dataLists['tables_select']);
$this->groupData['tables_modify'] = GeneralUtility::uniqueList($this->dataLists['tables_modify']);
$this->groupData['non_exclude_fields'] = GeneralUtility::uniqueList($this->dataLists['non_exclude_fields']);
$this->groupData['explicit_allowdeny'] = GeneralUtility::uniqueList($this->dataLists['explicit_allowdeny']);
$this->groupData['allowed_languages'] = GeneralUtility::uniqueList($this->dataLists['allowed_languages']);
$this->groupData['custom_options'] = GeneralUtility::uniqueList($this->dataLists['custom_options']);
$this->groupData['modules'] = GeneralUtility::uniqueList($this->dataLists['modList']);
$this->groupData['available_widgets'] = GeneralUtility::uniqueList($this->dataLists['available_widgets']);
$this->groupData['file_permissions'] = GeneralUtility::uniqueList($this->dataLists['file_permissions']);
$this->groupData['webmounts'] = StringUtility::uniqueList($this->dataLists['webmount_list'] ?? '');
$this->groupData['pagetypes_select'] = StringUtility::uniqueList($this->dataLists['pagetypes_select'] ?? '');
$this->groupData['tables_select'] = StringUtility::uniqueList(($this->dataLists['tables_modify'] ?? '') . ',' . ($this->dataLists['tables_select'] ?? ''));
$this->groupData['tables_modify'] = StringUtility::uniqueList($this->dataLists['tables_modify'] ?? '');
$this->groupData['non_exclude_fields'] = StringUtility::uniqueList($this->dataLists['non_exclude_fields'] ?? '');
$this->groupData['explicit_allowdeny'] = StringUtility::uniqueList($this->dataLists['explicit_allowdeny'] ?? '');
$this->groupData['allowed_languages'] = StringUtility::uniqueList($this->dataLists['allowed_languages'] ?? '');
$this->groupData['custom_options'] = StringUtility::uniqueList($this->dataLists['custom_options'] ?? '');
$this->groupData['modules'] = StringUtility::uniqueList($this->dataLists['modList'] ?? '');
$this->groupData['available_widgets'] = StringUtility::uniqueList($this->dataLists['available_widgets'] ?? '');
$this->groupData['file_permissions'] = StringUtility::uniqueList($this->dataLists['file_permissions'] ?? '');
$this->groupData['workspace_perms'] = $this->dataLists['workspace_perms'];
if (!empty(trim($this->groupData['webmounts']))) {
......@@ -1988,9 +1989,9 @@ TCAdefaults.sys_note.email = ' . $this->user['email'];
$this->setWorkspace($this->user['workspace_id']);
// Limiting the DB mountpoints if there any selected in the workspace record
$this->initializeDbMountpointsInWorkspace();
$allowed_languages = $this->getTSConfig()['options.']['workspaces.']['allowed_languages.'][$this->workspace] ?? '';
if (!empty($allowed_languages)) {
$this->groupData['allowed_languages'] = GeneralUtility::uniqueList($allowed_languages);
$allowed_languages = (string)($this->getTSConfig()['options.']['workspaces.']['allowed_languages.'][$this->workspace] ?? '');
if ($allowed_languages !== '') {
$this->groupData['allowed_languages'] = StringUtility::uniqueList($allowed_languages);
}
}
......
......@@ -645,9 +645,14 @@ class GeneralUtility
* @param string $in_list Accept multiple parameters which can be comma-separated lists of values and arrays.
* @param mixed $secondParameter Dummy field, which if set will show a warning!
* @return string Returns the list without any duplicates of values, space around values are trimmed
* @deprecated since TYPO3 v11, will be removed in TYPO3 v12. Use StringUtility::uniqueList() instead.
*/
public static function uniqueList($in_list, $secondParameter = null)
{
trigger_error(
'GeneralUtility::uniqueList() is deprecated and will be removed in v12. Use StringUtility::uniqueList() instead.',
E_USER_DEPRECATED
);
if (is_array($in_list)) {
throw new \InvalidArgumentException('TYPO3 Fatal Error: TYPO3\\CMS\\Core\\Utility\\GeneralUtility::uniqueList() does NOT support array arguments anymore! Only string comma lists!', 1270853885);
}
......
......@@ -150,4 +150,16 @@ class StringUtility
}
return $result;
}
/**
* Takes a comma-separated list and removes all duplicates.
* If a value in the list is trim(empty), the value is ignored.
*
* @param string $list Accept a comma-separated list of values.
* @return string Returns the list without any duplicates of values, space around values are trimmed.
*/
public static function uniqueList(string $list): string
{
return implode(',', array_unique(GeneralUtility::trimExplode(',', $list, true)));
}
}
.. include:: ../../Includes.txt
===========================================================
Deprecation: #92607 - Deprecated GeneralUtility::uniqueList
===========================================================
See :issue:`92607`
Description
===========
Since over a decade, the :php:`GeneralUtility::uniqueList()` method does not
accept an :php:`array` as first argument anymore. Furthermore the second
parameter was since then not longer be used. Both have thrown an
:php:`InvalidArgumentException`.
As the method doesn't belong to :php:`GeneralUtility` at all, a new refactored
version was added to :php:`StringUtility`. Therefore, the exceptions were removed
along with the unused second parameter. The first parameter is now type hinted
:php:`string` and the return type :php:`string` was added. Furthermore, the
PHPDoc was updated accordingly.
Impact
======
Calling the method will trigger a PHP deprecation notice.
Affected Installations
======================
TYPO3 installations with custom third-party extensions calling this method.
Migration
=========
Use the new :php`StringUtility::uniqueList()` method instead and ensure you
pass a valid string as first argument and ommit the second argument.
.. index:: PHP-API, FullyScanned, ext:core
......@@ -621,36 +621,6 @@ class GeneralUtilityTest extends UnitTestCase
self::assertSame(1000, count(explode(',', $list)));
}
///////////////////////////////
// Tests concerning uniqueList
///////////////////////////////
/**
* @test
* @param string $initialList
* @param string $unifiedList
* @dataProvider uniqueListUnifiesCommaSeparatedListDataProvider
*/
public function uniqueListUnifiesCommaSeparatedList($initialList, $unifiedList)
{
self::assertSame($unifiedList, GeneralUtility::uniqueList($initialList));
}
/**
* Data provider for uniqueListUnifiesCommaSeparatedList
*
* @return array
*/
public function uniqueListUnifiesCommaSeparatedListDataProvider()
{
return [
'List without duplicates' => ['one,two,three', 'one,two,three'],
'List with two consecutive duplicates' => ['one,two,two,three,three', 'one,two,three'],
'List with non-consecutive duplicates' => ['one,two,three,two,three', 'one,two,three'],
'One item list' => ['one', 'one'],
'Empty list' => ['', '']
];
}
///////////////////////////////
// Tests concerning isFirstPartOfStr
///////////////////////////////
......
......@@ -330,4 +330,35 @@ class StringUtilityTest extends UnitTestCase
],
];
}
/**
* Data provider for uniqueListUnifiesCommaSeparatedList
*
* @return \Generator
*/
public function uniqueListUnifiesCommaSeparatedListDataProvider(): \Generator
{
yield 'List without duplicates' => ['one,two,three', 'one,two,three'];
yield 'List with two consecutive duplicates' => ['one,two,two,three,three', 'one,two,three'];
yield 'List with non-consecutive duplicates' => ['one,two,three,two,three', 'one,two,three'];
yield 'One item list' => ['one', 'one'];
yield 'Empty list' => ['', ''];
yield 'No list, just a comma' => [',', ''];
yield 'List with leading comma' => [',one,two', 'one,two'];
yield 'List with trailing comma' => ['one,two,', 'one,two'];
yield 'List with multiple consecutive commas' => ['one,,two', 'one,two'];
}
/**
* @test
*
* @param string $initialList
* @param string $unifiedList
*
* @dataProvider uniqueListUnifiesCommaSeparatedListDataProvider
*/
public function uniqueListUnifiesCommaSeparatedList(string $initialList, string $unifiedList): void
{
self::assertSame($unifiedList, StringUtility::uniqueList($initialList));
}
}
......@@ -1043,4 +1043,11 @@ return [
'Deprecation-92598-Workspace-relatedMethodsFixVersioningPid.rst',
],
],
'TYPO3\CMS\Core\Utility\GeneralUtility::uniqueList' => [
'numberOfMandatoryArguments' => 1,
'maximumNumberOfArguments' => 2,
'restFiles' => [
'Deprecation-92607-DeprecatedGeneralUtilityuniqueList.rst'
],
],
];
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment