Skip to content

Commit

Permalink
[TASK] Refactor various methods GeneralUtility (#1646)
Browse files Browse the repository at this point in the history
Resolves: #526
  • Loading branch information
sabbelasichon authored Nov 23, 2020
1 parent c868d6b commit 4320c00
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 1 deletion.
146 changes: 146 additions & 0 deletions src/Rector/v8/v1/RefactorVariousGeneralUtilityMethodsRector.php
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
)]);
}
}
39 changes: 38 additions & 1 deletion stubs/Core/Utility/GeneralUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ public static function array2xml_cs(array $array, $docTag = 'phparray', array $o
{
// Set default charset unless explicitly specified
$charset = $charset ?: 'utf-8';

// Return XML:
return '<?xml version="1.0" encoding="' . htmlspecialchars($charset) . '" standalone="yes" ?>' . LF . self::array2xml($array, '', 0, $docTag, 0, $options);
return '<?xml version="1.0" encoding="'.htmlspecialchars($charset).'" standalone="yes" ?>'.LF.self::array2xml($array, '', 0, $docTag, 0, $options);
}

public static function array2xml(array $array, $NSprefix = '', $level = 0, $docTag = 'phparray', $spaceInd = 0, array $options = [], array $stackData = []): string
Expand All @@ -177,4 +178,40 @@ public static function csvValues(array $row, $delim = ',', $quote = '"'): void
{

}

public static function compat_version(): void
{
}

public static function convertMicrotime(): void
{
}

public static function deHSCentities(): void
{
}

public static function slashJS(): void
{
}

public static function rawUrlEncodeJS(): void
{
}

public static function rawUrlEncodeFP(): void
{
}

public static function lcfirst(): void
{
}

public static function getMaximumPathLength(): void
{
}

public static function wrapJS($string, $_ = null): void
{
}
}
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');

?>
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);

?>
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;

?>
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');

?>
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));

?>
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));

?>
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;
}
}

0 comments on commit 4320c00

Please sign in to comment.