Updated Rector to commit 3b0e8b6486

3b0e8b6486 [DowngradePhp80] Add DowngradePhpTokenRector (#426)
This commit is contained in:
Tomas Votruba 2021-07-11 22:22:32 +00:00
parent f3645ab6a9
commit 4a9dac4d4c
7 changed files with 125 additions and 19 deletions

View File

@ -0,0 +1,104 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp80\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\DowngradePhpTokenRectorTest
*/
final class DowngradePhpTokenRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var string
*/
private const PHP_TOKEN = 'PhpToken';
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('"something()" will be renamed to "somethingElse()"', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
$tokens = \PhpToken::tokenize($code);
foreach ($tokens as $phpToken) {
$name = $phpToken->getTokenName();
$text = $phpToken->text;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$tokens = token_get_all($code);
foreach ($tokens as $token) {
$name = is_array($token) ? token_name($token[0]) : null;
$text = is_array($token) ? $token[1] : $token;
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\StaticCall::class, \PhpParser\Node\Expr\MethodCall::class, \PhpParser\Node\Expr\PropertyFetch::class];
}
/**
* @param StaticCall|MethodCall|PropertyFetch $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node instanceof \PhpParser\Node\Expr\StaticCall) {
return $this->refactorStaticCall($node);
}
if ($node instanceof \PhpParser\Node\Expr\MethodCall) {
return $this->refactorMethodCall($node);
}
return $this->refactorPropertyFetch($node);
}
private function refactorStaticCall(\PhpParser\Node\Expr\StaticCall $staticCall) : ?\PhpParser\Node\Expr\FuncCall
{
if (!$this->isObjectType($staticCall->class, new \PHPStan\Type\ObjectType(self::PHP_TOKEN))) {
return null;
}
if (!$this->isName($staticCall->name, 'tokenize')) {
return null;
}
return new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('token_get_all'), $staticCall->args);
}
private function refactorMethodCall(\PhpParser\Node\Expr\MethodCall $methodCall) : ?\PhpParser\Node\Expr\Ternary
{
if (!$this->isObjectType($methodCall->var, new \PHPStan\Type\ObjectType(self::PHP_TOKEN))) {
return null;
}
if (!$this->isName($methodCall->name, 'getTokenName')) {
return null;
}
$isArrayFuncCall = new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('is_array'), [new \PhpParser\Node\Arg($methodCall->var)]);
$arrayDimFetch = new \PhpParser\Node\Expr\ArrayDimFetch($methodCall->var, new \PhpParser\Node\Scalar\LNumber(0));
$tokenGetNameFuncCall = new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('token_name'), [new \PhpParser\Node\Arg($arrayDimFetch)]);
return new \PhpParser\Node\Expr\Ternary($isArrayFuncCall, $tokenGetNameFuncCall, $this->nodeFactory->createNull());
}
private function refactorPropertyFetch(\PhpParser\Node\Expr\PropertyFetch $propertyFetch) : ?\PhpParser\Node\Expr\Ternary
{
if (!$this->isObjectType($propertyFetch->var, new \PHPStan\Type\ObjectType(self::PHP_TOKEN))) {
return null;
}
if (!$this->isName($propertyFetch->name, 'text')) {
return null;
}
$isArrayFuncCall = new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('is_array'), [new \PhpParser\Node\Arg($propertyFetch->var)]);
$arrayDimFetch = new \PhpParser\Node\Expr\ArrayDimFetch($propertyFetch->var, new \PhpParser\Node\Scalar\LNumber(1));
return new \PhpParser\Node\Expr\Ternary($isArrayFuncCall, $arrayDimFetch, $propertyFetch->var);
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'fb9ad83c32770022a3816ee06c88d92cd84dc322';
public const PACKAGE_VERSION = '3b0e8b64866125b42f2d06fd84715c6263e41a12';
/**
* @var string
*/
public const RELEASE_DATE = '2021-07-11 17:22:12';
public const RELEASE_DATE = '2021-07-12 00:13:32';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210711\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921::getLoader();
return ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58::getLoader();

View File

@ -2172,6 +2172,7 @@ return array(
'Rector\\DowngradePhp80\\Rector\\MethodCall\\DowngradeNamedArgumentRector' => $baseDir . '/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php',
'Rector\\DowngradePhp80\\Rector\\NullsafeMethodCall\\DowngradeNullsafeToTernaryOperatorRector' => $baseDir . '/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php',
'Rector\\DowngradePhp80\\Rector\\Property\\DowngradeUnionTypeTypedPropertyRector' => $baseDir . '/rules/DowngradePhp80/Rector/Property/DowngradeUnionTypeTypedPropertyRector.php',
'Rector\\DowngradePhp80\\Rector\\StaticCall\\DowngradePhpTokenRector' => $baseDir . '/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php',
'Rector\\DowngradePhp80\\Reflection\\DefaultParameterValueResolver' => $baseDir . '/rules/DowngradePhp80/Reflection/DefaultParameterValueResolver.php',
'Rector\\DowngradePhp80\\ValueObject\\DowngradeAttributeToAnnotation' => $baseDir . '/rules/DowngradePhp80/ValueObject/DowngradeAttributeToAnnotation.php',
'Rector\\EarlyReturn\\NodeFactory\\InvertedIfFactory' => $baseDir . '/rules/EarlyReturn/NodeFactory/InvertedIfFactory.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921
class ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit4433eec00bdfae5239023a9477ab5c58::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit4433eec00bdfae5239023a9477ab5c58::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire52f7372f8a7868c9cb3f603a94b1b921($fileIdentifier, $file);
composerRequire4433eec00bdfae5239023a9477ab5c58($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire52f7372f8a7868c9cb3f603a94b1b921($fileIdentifier, $file)
function composerRequire4433eec00bdfae5239023a9477ab5c58($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921
class ComposerStaticInit4433eec00bdfae5239023a9477ab5c58
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -2527,6 +2527,7 @@ class ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921
'Rector\\DowngradePhp80\\Rector\\MethodCall\\DowngradeNamedArgumentRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php',
'Rector\\DowngradePhp80\\Rector\\NullsafeMethodCall\\DowngradeNullsafeToTernaryOperatorRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php',
'Rector\\DowngradePhp80\\Rector\\Property\\DowngradeUnionTypeTypedPropertyRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/Property/DowngradeUnionTypeTypedPropertyRector.php',
'Rector\\DowngradePhp80\\Rector\\StaticCall\\DowngradePhpTokenRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php',
'Rector\\DowngradePhp80\\Reflection\\DefaultParameterValueResolver' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Reflection/DefaultParameterValueResolver.php',
'Rector\\DowngradePhp80\\ValueObject\\DowngradeAttributeToAnnotation' => __DIR__ . '/../..' . '/rules/DowngradePhp80/ValueObject/DowngradeAttributeToAnnotation.php',
'Rector\\EarlyReturn\\NodeFactory\\InvertedIfFactory' => __DIR__ . '/../..' . '/rules/EarlyReturn/NodeFactory/InvertedIfFactory.php',
@ -3847,9 +3848,9 @@ class ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit52f7372f8a7868c9cb3f603a94b1b921::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit4433eec00bdfae5239023a9477ab5c58::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4433eec00bdfae5239023a9477ab5c58::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4433eec00bdfae5239023a9477ab5c58::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20210711\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921', false) && !interface_exists('ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921', false) && !trait_exists('ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921', false)) {
spl_autoload_call('RectorPrefix20210711\ComposerAutoloaderInit52f7372f8a7868c9cb3f603a94b1b921');
if (!class_exists('ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58', false) && !interface_exists('ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58', false) && !trait_exists('ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58', false)) {
spl_autoload_call('RectorPrefix20210711\ComposerAutoloaderInit4433eec00bdfae5239023a9477ab5c58');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210711\Doctrine\Inflector\Inflector');
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210711\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire52f7372f8a7868c9cb3f603a94b1b921')) {
function composerRequire52f7372f8a7868c9cb3f603a94b1b921() {
return \RectorPrefix20210711\composerRequire52f7372f8a7868c9cb3f603a94b1b921(...func_get_args());
if (!function_exists('composerRequire4433eec00bdfae5239023a9477ab5c58')) {
function composerRequire4433eec00bdfae5239023a9477ab5c58() {
return \RectorPrefix20210711\composerRequire4433eec00bdfae5239023a9477ab5c58(...func_get_args());
}
}
if (!function_exists('parseArgs')) {