Updated Rector to commit 85719ff0e72bae77930c8b3149dbb029e8e0b99b

85719ff0e7 [Downgrade 7.0] Strip unnecessary parentheses around expressions (#1617)
This commit is contained in:
Tomas Votruba 2022-01-24 08:38:25 +00:00
parent c0379e66b3
commit f61a2947b9
9 changed files with 150 additions and 19 deletions

View File

@ -9,6 +9,7 @@ use Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRecto
use Rector\DowngradePhp70\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector;
use Rector\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector;
use Rector\DowngradePhp70\Rector\Expression\DowngradeDefineArrayConstantRector;
use Rector\DowngradePhp70\Rector\FuncCall\DowngradeDirnameLevelsRector;
use Rector\DowngradePhp70\Rector\FuncCall\DowngradeSessionStartArrayOptionsRector;
@ -46,4 +47,5 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector::class);
$services->set(\Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector::class);
$services->set(\Rector\DowngradePhp70\Rector\MethodCall\DowngradeMethodCallOnCloneRector::class);
$services->set(\Rector\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector::class);
};

View File

@ -0,0 +1,106 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp70\Rector\Expr;
use PhpParser\Node;
use PhpParser\Node\Expr;
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\StaticPropertyFetch;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp70\Tokenizer\WrappedInParenthesesAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/uniform_variable_syntax
*
* @see \Rector\Tests\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector\DowngradeUnnecessarilyParenthesizedExpressionRectorTest
*/
final class DowngradeUnnecessarilyParenthesizedExpressionRector extends \Rector\Core\Rector\AbstractRector
{
private const PARENTHESIZABLE_NODES = [\PhpParser\Node\Expr\ArrayDimFetch::class, \PhpParser\Node\Expr\PropertyFetch::class, \PhpParser\Node\Expr\MethodCall::class, \PhpParser\Node\Expr\StaticPropertyFetch::class, \PhpParser\Node\Expr\StaticCall::class, \PhpParser\Node\Expr\FuncCall::class];
/**
* @readonly
* @var \Rector\DowngradePhp70\Tokenizer\WrappedInParenthesesAnalyzer
*/
private $wrappedInParenthesesAnalyzer;
public function __construct(\Rector\DowngradePhp70\Tokenizer\WrappedInParenthesesAnalyzer $wrappedInParenthesesAnalyzer)
{
$this->wrappedInParenthesesAnalyzer = $wrappedInParenthesesAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove parentheses around expressions allowed by Uniform variable syntax RFC where they are not necessary to prevent parse errors on PHP 5.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
($f)['foo'];
($f)->foo;
($f)->foo();
($f)::$foo;
($f)::foo();
($f)();
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$f['foo'];
$f->foo;
$f->foo();
$f::$foo;
$f::foo();
$f();
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [
// TODO: Make PHPStan rules allow Expr namespace for its subclasses.
\PhpParser\Node\Expr::class,
];
}
/**
* @param ArrayDimFetch|PropertyFetch|MethodCall|StaticPropertyFetch|StaticCall|FuncCall $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node\Expr
{
if (!\in_array(\get_class($node), self::PARENTHESIZABLE_NODES, \true)) {
return null;
}
$leftSubNode = $this->getLeftSubNode($node);
if ($leftSubNode === null) {
return null;
}
if (!$this->wrappedInParenthesesAnalyzer->isParenthesized($this->file, $leftSubNode)) {
return null;
}
// Parenthesization is not part of the AST and Rector only re-generates code for AST nodes that changed.
// Lets remove the original node reference forcing the re-generation of the corresponding code.
// The code generator will only put parentheses where strictly necessary, which other rules should handle.
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE, null);
return $node;
}
private function getLeftSubNode(\PhpParser\Node $node) : ?\PhpParser\Node
{
switch (\true) {
case $node instanceof \PhpParser\Node\Expr\ArrayDimFetch:
return $node->var;
case $node instanceof \PhpParser\Node\Expr\PropertyFetch:
return $node->var;
case $node instanceof \PhpParser\Node\Expr\MethodCall:
return $node->var;
case $node instanceof \PhpParser\Node\Expr\StaticPropertyFetch:
return $node->class;
case $node instanceof \PhpParser\Node\Expr\StaticCall:
return $node->class;
case $node instanceof \PhpParser\Node\Expr\FuncCall:
return $node->name;
default:
return null;
}
}
}

View File

@ -0,0 +1,19 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp70\Tokenizer;
use PhpParser\Node;
use Rector\Core\ValueObject\Application\File;
final class WrappedInParenthesesAnalyzer
{
public function isParenthesized(\Rector\Core\ValueObject\Application\File $file, \PhpParser\Node $node) : bool
{
$oldTokens = $file->getOldTokens();
$startTokenPos = $node->getStartTokenPos();
$endTokenPos = $node->getEndTokenPos();
$previousTokenPos = $startTokenPos >= 0 ? $startTokenPos - 1 : -1;
$nextTokenPos = $endTokenPos >= 0 ? $endTokenPos + 1 : -1;
return isset($oldTokens[$previousTokenPos]) && $oldTokens[$previousTokenPos] === '(' && isset($oldTokens[$nextTokenPos]) && $oldTokens[$nextTokenPos] === ')';
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'a53b3d37eba05703b13f667065706301adc708e1';
public const PACKAGE_VERSION = '85719ff0e72bae77930c8b3149dbb029e8e0b99b';
/**
* @var string
*/
public const RELEASE_DATE = '2022-01-24 09:27:41';
public const RELEASE_DATE = '2022-01-24 09:31:03';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20220124\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 ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63::getLoader();
return ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173::getLoader();

View File

@ -1977,6 +1977,7 @@ return array(
'Rector\\DowngradePhp70\\Rector\\ClassMethod\\DowngradeSelfTypeDeclarationRector' => $baseDir . '/rules/DowngradePhp70/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php',
'Rector\\DowngradePhp70\\Rector\\Coalesce\\DowngradeNullCoalesceRector' => $baseDir . '/rules/DowngradePhp70/Rector/Coalesce/DowngradeNullCoalesceRector.php',
'Rector\\DowngradePhp70\\Rector\\Declare_\\DowngradeStrictTypeDeclarationRector' => $baseDir . '/rules/DowngradePhp70/Rector/Declare_/DowngradeStrictTypeDeclarationRector.php',
'Rector\\DowngradePhp70\\Rector\\Expr\\DowngradeUnnecessarilyParenthesizedExpressionRector' => $baseDir . '/rules/DowngradePhp70/Rector/Expr/DowngradeUnnecessarilyParenthesizedExpressionRector.php',
'Rector\\DowngradePhp70\\Rector\\Expression\\DowngradeDefineArrayConstantRector' => $baseDir . '/rules/DowngradePhp70/Rector/Expression/DowngradeDefineArrayConstantRector.php',
'Rector\\DowngradePhp70\\Rector\\FuncCall\\DowngradeDirnameLevelsRector' => $baseDir . '/rules/DowngradePhp70/Rector/FuncCall/DowngradeDirnameLevelsRector.php',
'Rector\\DowngradePhp70\\Rector\\FuncCall\\DowngradeSessionStartArrayOptionsRector' => $baseDir . '/rules/DowngradePhp70/Rector/FuncCall/DowngradeSessionStartArrayOptionsRector.php',
@ -1991,6 +1992,7 @@ return array(
'Rector\\DowngradePhp70\\Rector\\Spaceship\\DowngradeSpaceshipRector' => $baseDir . '/rules/DowngradePhp70/Rector/Spaceship/DowngradeSpaceshipRector.php',
'Rector\\DowngradePhp70\\Rector\\String_\\DowngradeGeneratedScalarTypesRector' => $baseDir . '/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php',
'Rector\\DowngradePhp70\\Rector\\TryCatch\\DowngradeCatchThrowableRector' => $baseDir . '/rules/DowngradePhp70/Rector/TryCatch/DowngradeCatchThrowableRector.php',
'Rector\\DowngradePhp70\\Tokenizer\\WrappedInParenthesesAnalyzer' => $baseDir . '/rules/DowngradePhp70/Tokenizer/WrappedInParenthesesAnalyzer.php',
'Rector\\DowngradePhp71\\Rector\\Array_\\SymmetricArrayDestructuringToListRector' => $baseDir . '/rules/DowngradePhp71/Rector/Array_/SymmetricArrayDestructuringToListRector.php',
'Rector\\DowngradePhp71\\Rector\\ClassConst\\DowngradeClassConstantVisibilityRector' => $baseDir . '/rules/DowngradePhp71/Rector/ClassConst/DowngradeClassConstantVisibilityRector.php',
'Rector\\DowngradePhp71\\Rector\\FuncCall\\DowngradeIsIterableRector' => $baseDir . '/rules/DowngradePhp71/Rector/FuncCall/DowngradeIsIterableRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63
class ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173', '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\ComposerStaticInit19438c6da99564141376cdd144a6fb63::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit19438c6da99564141376cdd144a6fb63::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire19438c6da99564141376cdd144a6fb63($fileIdentifier, $file);
composerRequire81dc169b4b341e97db2f44e6a4b90173($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63
* @param string $file
* @return void
*/
function composerRequire19438c6da99564141376cdd144a6fb63($fileIdentifier, $file)
function composerRequire81dc169b4b341e97db2f44e6a4b90173($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit19438c6da99564141376cdd144a6fb63
class ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2372,6 +2372,7 @@ class ComposerStaticInit19438c6da99564141376cdd144a6fb63
'Rector\\DowngradePhp70\\Rector\\ClassMethod\\DowngradeSelfTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php',
'Rector\\DowngradePhp70\\Rector\\Coalesce\\DowngradeNullCoalesceRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/Coalesce/DowngradeNullCoalesceRector.php',
'Rector\\DowngradePhp70\\Rector\\Declare_\\DowngradeStrictTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/Declare_/DowngradeStrictTypeDeclarationRector.php',
'Rector\\DowngradePhp70\\Rector\\Expr\\DowngradeUnnecessarilyParenthesizedExpressionRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/Expr/DowngradeUnnecessarilyParenthesizedExpressionRector.php',
'Rector\\DowngradePhp70\\Rector\\Expression\\DowngradeDefineArrayConstantRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/Expression/DowngradeDefineArrayConstantRector.php',
'Rector\\DowngradePhp70\\Rector\\FuncCall\\DowngradeDirnameLevelsRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/FuncCall/DowngradeDirnameLevelsRector.php',
'Rector\\DowngradePhp70\\Rector\\FuncCall\\DowngradeSessionStartArrayOptionsRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/FuncCall/DowngradeSessionStartArrayOptionsRector.php',
@ -2386,6 +2387,7 @@ class ComposerStaticInit19438c6da99564141376cdd144a6fb63
'Rector\\DowngradePhp70\\Rector\\Spaceship\\DowngradeSpaceshipRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/Spaceship/DowngradeSpaceshipRector.php',
'Rector\\DowngradePhp70\\Rector\\String_\\DowngradeGeneratedScalarTypesRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php',
'Rector\\DowngradePhp70\\Rector\\TryCatch\\DowngradeCatchThrowableRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/TryCatch/DowngradeCatchThrowableRector.php',
'Rector\\DowngradePhp70\\Tokenizer\\WrappedInParenthesesAnalyzer' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Tokenizer/WrappedInParenthesesAnalyzer.php',
'Rector\\DowngradePhp71\\Rector\\Array_\\SymmetricArrayDestructuringToListRector' => __DIR__ . '/../..' . '/rules/DowngradePhp71/Rector/Array_/SymmetricArrayDestructuringToListRector.php',
'Rector\\DowngradePhp71\\Rector\\ClassConst\\DowngradeClassConstantVisibilityRector' => __DIR__ . '/../..' . '/rules/DowngradePhp71/Rector/ClassConst/DowngradeClassConstantVisibilityRector.php',
'Rector\\DowngradePhp71\\Rector\\FuncCall\\DowngradeIsIterableRector' => __DIR__ . '/../..' . '/rules/DowngradePhp71/Rector/FuncCall/DowngradeIsIterableRector.php',
@ -3872,9 +3874,9 @@ class ComposerStaticInit19438c6da99564141376cdd144a6fb63
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit19438c6da99564141376cdd144a6fb63::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit19438c6da99564141376cdd144a6fb63::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit19438c6da99564141376cdd144a6fb63::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit81dc169b4b341e97db2f44e6a4b90173::$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('RectorPrefix20220124\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63', false) && !interface_exists('ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63', false) && !trait_exists('ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63', false)) {
spl_autoload_call('RectorPrefix20220124\ComposerAutoloaderInit19438c6da99564141376cdd144a6fb63');
if (!class_exists('ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173', false) && !interface_exists('ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173', false) && !trait_exists('ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173', false)) {
spl_autoload_call('RectorPrefix20220124\ComposerAutoloaderInit81dc169b4b341e97db2f44e6a4b90173');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20220124\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -71,9 +71,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220124\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire19438c6da99564141376cdd144a6fb63')) {
function composerRequire19438c6da99564141376cdd144a6fb63() {
return \RectorPrefix20220124\composerRequire19438c6da99564141376cdd144a6fb63(...func_get_args());
if (!function_exists('composerRequire81dc169b4b341e97db2f44e6a4b90173')) {
function composerRequire81dc169b4b341e97db2f44e6a4b90173() {
return \RectorPrefix20220124\composerRequire81dc169b4b341e97db2f44e6a4b90173(...func_get_args());
}
}
if (!function_exists('scanPath')) {