Commit 93c754af authored by Benni Mack's avatar Benni Mack
Browse files

[!!!][TASK] Remove String-related helper methods

Resolves: #96155
Related: #95293
Related: #95257
Releases: main
Change-Id: I5aa8d747b98eb75ac7938323d97e99cad477761a
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72397


Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 9caf10ee
......@@ -665,22 +665,6 @@ class GeneralUtility
return count($p) === 2 ? $p[0] : '';
}
/**
* Returns TRUE if the first part of $str matches the string $partStr
*
* @param string $str Full string to check
* @param string $partStr Reference string which must be found as the "first part" of the full string
* @return bool TRUE if $partStr was found to be equal to the first part of $str
* @deprecated will be removed in TYPO3 v12.0. Use native PHP str_starts_with() with proper casting instead.
*/
public static function isFirstPartOfStr($str, $partStr)
{
trigger_error('GeneralUtility::isFirstPartOfStr() will be removed in TYPO3 v12.0. Use PHPs str_starts_with() function instead.', E_USER_DEPRECATED);
$str = is_array($str) ? '' : (string)$str;
$partStr = is_array($partStr) ? '' : (string)$partStr;
return $partStr !== '' && strpos($str, $partStr, 0) === 0;
}
/**
* Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M)
*
......
......@@ -20,72 +20,6 @@ namespace TYPO3\CMS\Core\Utility;
*/
class StringUtility
{
/**
* Returns TRUE if $haystack begins with $needle.
* The input string is not trimmed before and search is done case sensitive.
*
* @param string $haystack Full string to check
* @param string $needle Reference string which must be found as the "first part" of the full string
* @throws \InvalidArgumentException
* @return bool TRUE if $needle was found to be equal to the first part of $haystack
* @deprecated will be removed in TYPO3 v12.0. Use PHP's native str_starts_with() function instead.
*/
public static function beginsWith($haystack, $needle)
{
trigger_error('StringUtility::beginsWith() will be removed in TYPO3 v12.0. Use PHPs str_starts_with() function instead.', E_USER_DEPRECATED);
// Sanitize $haystack and $needle
if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
throw new \InvalidArgumentException(
'$haystack can not be interpreted as string',
1347135546
);
}
if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
throw new \InvalidArgumentException(
'$needle can not be interpreted as string or has zero length',
1347135547
);
}
$haystack = (string)$haystack;
$needle = (string)$needle;
return $needle !== '' && strpos($haystack, $needle) === 0;
}
/**
* Returns TRUE if $haystack ends with $needle.
* The input string is not trimmed before and search is done case sensitive.
*
* @param string $haystack Full string to check
* @param string $needle Reference string which must be found as the "last part" of the full string
* @throws \InvalidArgumentException
* @return bool TRUE if $needle was found to be equal to the last part of $haystack
* @deprecated will be removed in TYPO3 v12.0. Use PHP's native str_ends_with() function instead.
*/
public static function endsWith($haystack, $needle)
{
trigger_error('StringUtility::endsWith() will be removed in TYPO3 v12.0. Use PHPs str_ends_with() function instead.', E_USER_DEPRECATED);
// Sanitize $haystack and $needle
if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
throw new \InvalidArgumentException(
'$haystack can not be interpreted as string',
1347135544
);
}
if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
throw new \InvalidArgumentException(
'$needle can not be interpreted as string or has no length',
1347135545
);
}
$haystackLength = strlen($haystack);
$needleLength = strlen($needle);
if (!$haystackLength || $needleLength > $haystackLength) {
return false;
}
$position = strrpos((string)$haystack, (string)$needle);
return $position !== false && $position === $haystackLength - $needleLength;
}
/**
* This function generates a unique id by using the more entropy parameter.
* Furthermore the dots are removed so the id can be used inside HTML attributes e.g. id.
......
......@@ -63,12 +63,15 @@ The following PHP static class methods that have previously been marked as depre
- :php:`\TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getInstance()`
- :php:`\TYPO3\CMS\Core\Resource\TextExtraction\TextExtractorRegistry::getInstance()`
- :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable()`
- :php:`\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr()`
- :php:`\TYPO3\CMS\Core\Utility\GeneralUtility::rmFromList()`
- :php:`\TYPO3\CMS\Core\Utility\GeneralUtility::stdAuthCode()`
- :php:`\TYPO3\CMS\Core\Utility\GeneralUtility::uniqueList()`
- :php:`\TYPO3\CMS\Core\Utility\HttpUtility::redirect()`
- :php:`\TYPO3\CMS\Core\Utility\HttpUtility::setResponseCode()`
- :php:`\TYPO3\CMS\Core\Utility\HttpUtility::setResponseCodeAndExit()`
- :php:`\TYPO3\CMS\Core\Utility\StringUtility::beginsWith()`
- :php:`\TYPO3\CMS\Core\Utility\StringUtility::endsWith()`
- :php:`\TYPO3\CMS\Form\Service\TranslationService::getInstance()`
- :php:`\TYPO3\CMS\T3editor\Registry\AddonRegistry::getInstance()`
- :php:`\TYPO3\CMS\T3editor\Registry\ModeRegistry::getInstance()`
......
......@@ -46,68 +46,4 @@ class GeneralUtilityTest extends UnitTestCase
$result = GeneralUtility::compileSelectedGetVarsFromArray($filter, $getArray, true);
self::assertSame($expected, $result);
}
///////////////////////////////
// Tests concerning isFirstPartOfStr
///////////////////////////////
/**
* Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
*
* @return array
*/
public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider(): array
{
return [
'match first part of string' => ['hello world', 'hello'],
'match whole string' => ['hello', 'hello'],
'integer is part of string with same number' => ['24', 24],
'string is part of integer with same number' => [24, '24'],
'integer is part of string starting with same number' => ['24 beer please', 24],
];
}
/**
* @test
* @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
*/
public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part): void
{
self::assertTrue(GeneralUtility::isFirstPartOfStr($string, $part));
}
/**
* Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
*
* @return array
*/
public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider(): array
{
return [
'no string match' => ['hello', 'bye'],
'no case sensitive string match' => ['hello world', 'Hello'],
'array is not part of string' => ['string', []],
'string is not part of array' => [[], 'string'],
'NULL is not part of string' => ['string', null],
'string is not part of NULL' => [null, 'string'],
'NULL is not part of array' => [[], null],
'array is not part of NULL' => [null, []],
'empty string is not part of empty string' => ['', ''],
'NULL is not part of empty string' => ['', null],
'false is not part of empty string' => ['', false],
'empty string is not part of NULL' => [null, ''],
'empty string is not part of false' => [false, ''],
'empty string is not part of zero integer' => [0, ''],
'zero integer is not part of NULL' => [null, 0],
'zero integer is not part of empty string' => ['', 0],
];
}
/**
* @test
* @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
*/
public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part): void
{
self::assertFalse(GeneralUtility::isFirstPartOfStr($string, $part));
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\UnitDeprecated\Utility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Testcase for class \TYPO3\CMS\Core\Utility\StringUtility
*/
class StringUtilityTest extends UnitTestCase
{
/**
* Data provider for endsWithReturnsTrueForMatchingFirstPart
*
* @return array
*/
public function endsWithReturnsTrueForMatchingLastPartDataProvider(): array
{
return [
'match last part of string' => ['hello world', 'world'],
'match last char of string' => ['hello world', 'd'],
'match whole string' => ['hello', 'hello'],
'integer is part of string with same number' => ['24', 24],
'string is part of integer with same number' => [24, '24'],
'integer is part of string ending with same number' => ['please gimme beer, 24', 24],
];
}
/**
* @test
* @dataProvider endsWithReturnsTrueForMatchingLastPartDataProvider
*/
public function endsWithReturnsTrueForMatchingLastPart($string, $part): void
{
self::assertTrue(StringUtility::endsWith($string, $part));
}
/**
* Data provider for check endsWithReturnsFalseForNotMatchingLastPart
*
* @return array
*/
public function endsWithReturnsFalseForNotMatchingLastPartDataProvider(): array
{
return [
'no string match' => ['hello', 'bye'],
'no case sensitive string match' => ['hello world', 'World'],
'string is part but not last part' => ['hello world', 'worl'],
'integer is not part of empty string' => ['', 0],
'longer string is not part of shorter string' => ['a', 'aa'],
];
}
/**
* @test
* @dataProvider endsWithReturnsFalseForNotMatchingLastPartDataProvider
*/
public function endsWithReturnsFalseForNotMatchingLastPart($string, $part): void
{
self::assertFalse(StringUtility::endsWith($string, $part));
}
/**
* Data provider for endsWithReturnsThrowsExceptionWithInvalidArguments
*
* @return array
*/
public function endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider(): array
{
return [
'array is not part of string' => ['string', [], 1347135545],
'NULL is not part of string' => ['string', null, 1347135545],
'empty string is not part of string' => ['string', '', 1347135545],
'string is not part of array' => [[], 'string', 1347135544],
'NULL is not part of array' => [[], null, 1347135544],
'string is not part of NULL' => [null, 'string', 1347135544],
'array is not part of NULL' => [null, [], 1347135544],
'integer is not part of NULL' => [null, 0, 1347135544],
'empty string is not part of NULL' => [null, '', 1347135544],
'NULL is not part of empty string' => ['', null, 1347135545],
'FALSE is not part of empty string' => ['', false, 1347135545],
'empty string is not part of FALSE' => [false, '', 1347135545],
'empty string is not part of integer' => [0, '', 1347135545],
'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
'object is not part of string' => ['foo', new \stdClass(), 1347135545],
];
}
/**
* @test
* @dataProvider endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
*/
public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode($expectedException);
StringUtility::endsWith($string, $part);
}
/**
* Data provider for beginsWithReturnsTrueForMatchingFirstPart
*
* @return array
*/
public function beginsWithReturnsTrueForMatchingFirstPartDataProvider(): array
{
return [
'match first part of string' => ['hello world', 'hello'],
'match first char of string' => ['hello world', 'h'],
'match whole string' => ['hello', 'hello'],
'integer is part of string with same number' => ['24', 24],
'string is part of integer with same number' => [24, '24'],
'integer is part of string starting with same number' => ['24, please gimme beer', 24],
];
}
/**
* @test
* @dataProvider beginsWithReturnsTrueForMatchingFirstPartDataProvider
*/
public function beginsWithReturnsTrueForMatchingFirstPart($string, $part): void
{
self::assertTrue(StringUtility::beginsWith($string, $part));
}
/**
* Data provider for check beginsWithReturnsFalseForNotMatchingFirstPart
*
* @return array
*/
public function beginsWithReturnsFalseForNotMatchingFirstPartDataProvider(): array
{
return [
'no string match' => ['hello', 'bye'],
'no case sensitive string match' => ['hello world', 'Hello'],
'string in empty string' => ['', 'foo'],
];
}
/**
* @test
* @dataProvider beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
*/
public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part): void
{
self::assertFalse(StringUtility::beginsWith($string, $part));
}
/**
* Data provider for beginsWithReturnsThrowsExceptionWithInvalidArguments
*
* @return array
*/
public function beginsWithReturnsInvalidArgumentDataProvider(): array
{
return [
'array is not part of string' => ['string', [], 1347135547],
'NULL is not part of string' => ['string', null, 1347135547],
'empty string is not part of string' => ['string', '', 1347135547],
'string is not part of array' => [[], 'string', 1347135546],
'NULL is not part of array' => [[], null, 1347135546],
'string is not part of NULL' => [null, 'string', 1347135546],
'array is not part of NULL' => [null, [], 1347135546],
'integer is not part of NULL' => [null, 0, 1347135546],
'empty string is not part of NULL' => [null, '', 1347135546],
'NULL is not part of empty string' => ['', null, 1347135547],
'FALSE is not part of empty string' => ['', false, 1347135547],
'empty string is not part of FALSE' => [false, '', 1347135547],
'empty string is not part of integer' => [0, '', 1347135547],
'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
'object is not part of string' => ['foo', new \stdClass(), 1347135547],
];
}
/**
* @test
* @dataProvider beginsWithReturnsInvalidArgumentDataProvider
*
* @param mixed $string
* @param mixed $part
* @param int $expectedException
*/
public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, int $expectedException): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode($expectedException);
StringUtility::beginsWith($string, $part);
}
}
......@@ -1186,6 +1186,7 @@ return [
'maximumNumberOfArguments' => 2,
'restFiles' => [
'Deprecation-95257-GeneralUtilityisFirstPartOfStr.rst',
'Breaking-96107-DeprecatedFunctionalityRemoved.rst',
],
],
'TYPO3\CMS\Core\Utility\StringUtility::beginsWith' => [
......@@ -1193,6 +1194,7 @@ return [
'maximumNumberOfArguments' => 2,
'restFiles' => [
'Deprecation-95293-StringUtilitystartsWithAndStringUtilityendsWith.rst',
'Breaking-96107-DeprecatedFunctionalityRemoved.rst',
],
],
'TYPO3\CMS\Core\Utility\StringUtility::endsWith' => [
......@@ -1200,6 +1202,7 @@ return [
'maximumNumberOfArguments' => 2,
'restFiles' => [
'Deprecation-95293-StringUtilitystartsWithAndStringUtilityendsWith.rst',
'Breaking-96107-DeprecatedFunctionalityRemoved.rst',
],
],
'TYPO3\CMS\Core\Resource\Index\ExtractorRegistry::getInstance' => [
......
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