-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Refactor various methods GeneralUtility (#1646)
Resolves: #526
- Loading branch information
1 parent
c868d6b
commit 4320c00
Showing
9 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
src/Rector/v8/v1/RefactorVariousGeneralUtilityMethodsRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v8\v1; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; | ||
use PhpParser\Node\Expr\BinaryOp\Mul; | ||
use PhpParser\Node\Expr\BinaryOp\Plus; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Expression; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Core\Utility\VersionNumberUtility; | ||
|
||
/** | ||
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/8.1/Deprecation-75621-GeneralUtilityMethods.html | ||
*/ | ||
final class RefactorVariousGeneralUtilityMethodsRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const COMPAT_VERSION = 'compat_version'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const CONVERT_MICROTIME = 'convertMicrotime'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const RAW_URL_ENCODE_JS = 'rawUrlEncodeJS'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const RAW_URL_ENCODE_FP = 'rawUrlEncodeFP'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const GET_MAXIMUM_PATH_LENGTH = 'getMaximumPathLength'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const LCFIRST = 'lcfirst'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const PARTS = 'parts'; | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isMethodStaticCallOrClassMethodObjectType($node, GeneralUtility::class)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isNames($node->name, [ | ||
self::COMPAT_VERSION, | ||
self::CONVERT_MICROTIME, | ||
self::RAW_URL_ENCODE_JS, | ||
self::RAW_URL_ENCODE_FP, | ||
self::LCFIRST, | ||
self::GET_MAXIMUM_PATH_LENGTH, | ||
])) { | ||
return null; | ||
} | ||
|
||
switch ($this->getName($node->name)) { | ||
case self::COMPAT_VERSION: | ||
return new GreaterOrEqual( | ||
$this->createStaticCall(VersionNumberUtility::class, 'convertVersionNumberToInteger', [ | ||
new ConstFetch(new Name('TYPO3_branch')), | ||
]), | ||
$this->createStaticCall(VersionNumberUtility::class, 'convertVersionNumberToInteger', $node->args) | ||
); | ||
case self::CONVERT_MICROTIME: | ||
$funcCall = $this->createFuncCall('explode', [new String_(' '), $node->args[0]->value]); | ||
$this->addNodeBeforeNode(new Expression(new Assign(new Variable(self::PARTS), $funcCall)), $node); | ||
|
||
return $this->createFuncCall('round', [ | ||
new Mul(new Plus( | ||
new ArrayDimFetch(new Variable(self::PARTS), new LNumber(0)), | ||
new ArrayDimFetch(new Variable(self::PARTS), new LNumber(1)) | ||
), new LNumber(1000)), | ||
]); | ||
case self::RAW_URL_ENCODE_JS: | ||
return $this->createFuncCall('str_replace', [ | ||
'%20', | ||
' ', | ||
$this->createFuncCall('rawurlencode', $node->args), | ||
]); | ||
case self::RAW_URL_ENCODE_FP: | ||
return $this->createFuncCall('str_replace', [ | ||
'%2F', | ||
'/', | ||
$this->createFuncCall('rawurlencode', $node->args), | ||
]); | ||
case self::LCFIRST: | ||
return $this->createFuncCall(self::LCFIRST, $node->args); | ||
case self::GET_MAXIMUM_PATH_LENGTH: | ||
return new ConstFetch(new Name('PHP_MAXPATHLEN')); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('', [new CodeSample(<<<'PHP' | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$url = 'https://www.domain.com/'; | ||
$url = GeneralUtility::rawUrlEncodeFP($url); | ||
PHP | ||
, <<<'PHP' | ||
$url = 'https://www.domain.com/'; | ||
$url = str_replace('%2F', '/', rawurlencode($url)); | ||
PHP | ||
)]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/compat_version.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
GeneralUtility::compat_version('foobar'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\VersionNumberUtility; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch) >= VersionNumberUtility::convertVersionNumberToInteger('foobar'); | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
tests/Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/convert_microtime.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$microtime = GeneralUtility::convertMicrotime('10 00'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
$parts = explode(' ', '10 00'); | ||
|
||
$microtime = round(($parts[0] + $parts[1]) * 1000); | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/get_maximum_path_length.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$maximum = GeneralUtility::getMaximumPathLength(); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$maximum = PHP_MAXPATHLEN; | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
tests/Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/lcfirst.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$string = GeneralUtility::lcfirst('foo'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$string = lcfirst('foo'); | ||
|
||
?> |
17 changes: 17 additions & 0 deletions
17
tests/Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/raw_url_encode_fp.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$url = 'https://www.domain.com/'; | ||
$url = GeneralUtility::rawUrlEncodeFP($url); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$url = 'https://www.domain.com/'; | ||
$url = str_replace('%2F', '/', rawurlencode($url)); | ||
|
||
?> |
17 changes: 17 additions & 0 deletions
17
tests/Rector/v8/v1/RefactorVariousGeneralUtilityMethods/Fixture/raw_url_encode_js.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$url = 'https://www.domain.com/'; | ||
$url = GeneralUtility::rawUrlEncodeJS($url); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
$url = 'https://www.domain.com/'; | ||
$url = str_replace('%20', ' ', rawurlencode($url)); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...1/RefactorVariousGeneralUtilityMethods/RefactorVariousGeneralUtilityMethodsRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v8\v1\RefactorVariousGeneralUtilityMethods; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Rector\v8\v1\RefactorVariousGeneralUtilityMethodsRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class RefactorVariousGeneralUtilityMethodsRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideDataForTest() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideDataForTest(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return RefactorVariousGeneralUtilityMethodsRector::class; | ||
} | ||
} |