Updated Rector to commit 0c695e2e0589c17bb607ec7c5fe0c49740ef5317

0c695e2e05 [Downgrade PHP 8.0] Add DowngradeDereferenceableOperationRector (#1442)
This commit is contained in:
Tomas Votruba 2021-12-09 17:01:18 +00:00
parent 21b47be5bd
commit a65233ea4f
10 changed files with 122 additions and 19 deletions

View File

@ -5,6 +5,7 @@ namespace RectorPrefix20211209;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp80\Rector\ArrayDimFetch\DowngradeDereferenceableOperationRector;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
@ -45,6 +46,7 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
// Nette
new \Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation('Nette\\DI\\Attributes\\Inject', 'inject'),
]);
$services->set(\Rector\DowngradePhp80\Rector\ArrayDimFetch\DowngradeDereferenceableOperationRector::class);
$services->set(\Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector::class);
$services->set(\Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector::class);
$services->set(\Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector::class);

View File

@ -187,4 +187,8 @@ final class AttributeKey
* @var string
*/
public const HAS_NEW_INHERITED_TYPE = 'has_new_inherited_type';
/**
* @var string
*/
public const WRAPPED_IN_PARENTHESES = 'wrapped_in_parentheses';
}

View File

@ -0,0 +1,90 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp80\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\MagicConst;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/variable_syntax_tweaks
*
* @see \Rector\Tests\DowngradePhp80\Rector\ArrayDimFetch\DowngradeDereferenceableOperationRector\DowngradeDereferenceableOperationRectorTest
*/
final class DowngradeDereferenceableOperationRector extends \Rector\Core\Rector\AbstractRector
{
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Add parentheses around non-dereferenceable expressions.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
function getFirstChar(string $str, string $suffix = '')
{
return "$str$suffix"[0];
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
function getFirstChar(string $str, string $suffix = '')
{
return ("$str$suffix")[0];
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\ArrayDimFetch::class];
}
/**
* @param ArrayDimFetch $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($this->shouldSkip($node)) {
return null;
}
$node->var->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::WRAPPED_IN_PARENTHESES, \true);
return $node;
}
private function shouldSkip(\PhpParser\Node\Expr\ArrayDimFetch $arrayDimFetch) : bool
{
if ($arrayDimFetch->dim === null) {
return \true;
}
if ($arrayDimFetch->var instanceof \PhpParser\Node\Scalar\Encapsed) {
return $this->hasParentheses($arrayDimFetch);
}
if ($arrayDimFetch->var instanceof \PhpParser\Node\Scalar\MagicConst) {
return $this->hasParentheses($arrayDimFetch);
}
return \true;
}
private function hasParentheses(\PhpParser\Node\Expr\ArrayDimFetch $arrayDimFetch) : bool
{
$wrappedInParentheses = $arrayDimFetch->var->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::WRAPPED_IN_PARENTHESES);
if ($wrappedInParentheses === \true) {
return \true;
}
\assert($arrayDimFetch->dim !== null);
// already checked in shouldSkip()
$oldTokens = $this->file->getOldTokens();
$varEndTokenPos = $arrayDimFetch->var->getEndTokenPos();
$dimStartTokenPos = $arrayDimFetch->dim->getStartTokenPos();
for ($i = $varEndTokenPos + 1; $i < $dimStartTokenPos; ++$i) {
if (!isset($oldTokens[$i])) {
continue;
}
if ($oldTokens[$i] !== ')') {
continue;
}
return \true;
}
return \false;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '0c2d1aec3ee5d14f83b48a70ddfe3f2714980449';
public const PACKAGE_VERSION = '0c695e2e0589c17bb607ec7c5fe0c49740ef5317';
/**
* @var string
*/
public const RELEASE_DATE = '2021-12-09 15:44:25';
public const RELEASE_DATE = '2021-12-09 17:45:08';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211209\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

View File

@ -147,6 +147,11 @@ final class BetterStandardPrinter extends \PhpParser\PrettyPrinter\Standard
$content = $this->pStmts($fileWithoutNamespace->stmts, \false);
return \ltrim($content);
}
protected function p(\PhpParser\Node $node, $parentFormatPreserved = \false) : string
{
$content = parent::p($node, $parentFormatPreserved);
return $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::WRAPPED_IN_PARENTHESES) === \true ? '(' . $content . ')' : $content;
}
/**
* This allows to use both spaces and tabs vs. original space-only
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2004,6 +2004,7 @@ return array(
'Rector\\DowngradePhp74\\Rector\\Property\\DowngradeTypedPropertyRector' => $baseDir . '/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php',
'Rector\\DowngradePhp80\\NodeAnalyzer\\NamedToUnnamedArgs' => $baseDir . '/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php',
'Rector\\DowngradePhp80\\NodeAnalyzer\\UnnamedArgumentResolver' => $baseDir . '/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php',
'Rector\\DowngradePhp80\\Rector\\ArrayDimFetch\\DowngradeDereferenceableOperationRector' => $baseDir . '/rules/DowngradePhp80/Rector/ArrayDimFetch/DowngradeDereferenceableOperationRector.php',
'Rector\\DowngradePhp80\\Rector\\Catch_\\DowngradeNonCapturingCatchesRector' => $baseDir . '/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php',
'Rector\\DowngradePhp80\\Rector\\ClassConstFetch\\DowngradeClassOnObjectToGetClassRector' => $baseDir . '/rules/DowngradePhp80/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php',
'Rector\\DowngradePhp80\\Rector\\ClassMethod\\DowngradeAbstractPrivateMethodInTraitRector' => $baseDir . '/rules/DowngradePhp80/Rector/ClassMethod/DowngradeAbstractPrivateMethodInTraitRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625
class ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d', '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\ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit2b4e877593f8f88c3998a9357665b46d::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit2b4e877593f8f88c3998a9357665b46d::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequireea6be250a9b3ddb5ee7f07c3b6239625($fileIdentifier, $file);
composerRequire2b4e877593f8f88c3998a9357665b46d($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequireea6be250a9b3ddb5ee7f07c3b6239625($fileIdentifier, $file)
function composerRequire2b4e877593f8f88c3998a9357665b46d($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625
class ComposerStaticInit2b4e877593f8f88c3998a9357665b46d
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -2394,6 +2394,7 @@ class ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625
'Rector\\DowngradePhp74\\Rector\\Property\\DowngradeTypedPropertyRector' => __DIR__ . '/../..' . '/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php',
'Rector\\DowngradePhp80\\NodeAnalyzer\\NamedToUnnamedArgs' => __DIR__ . '/../..' . '/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php',
'Rector\\DowngradePhp80\\NodeAnalyzer\\UnnamedArgumentResolver' => __DIR__ . '/../..' . '/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php',
'Rector\\DowngradePhp80\\Rector\\ArrayDimFetch\\DowngradeDereferenceableOperationRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/ArrayDimFetch/DowngradeDereferenceableOperationRector.php',
'Rector\\DowngradePhp80\\Rector\\Catch_\\DowngradeNonCapturingCatchesRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php',
'Rector\\DowngradePhp80\\Rector\\ClassConstFetch\\DowngradeClassOnObjectToGetClassRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php',
'Rector\\DowngradePhp80\\Rector\\ClassMethod\\DowngradeAbstractPrivateMethodInTraitRector' => __DIR__ . '/../..' . '/rules/DowngradePhp80/Rector/ClassMethod/DowngradeAbstractPrivateMethodInTraitRector.php',
@ -3793,9 +3794,9 @@ class ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitea6be250a9b3ddb5ee7f07c3b6239625::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit2b4e877593f8f88c3998a9357665b46d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2b4e877593f8f88c3998a9357665b46d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2b4e877593f8f88c3998a9357665b46d::$classMap;
}, null, ClassLoader::class);
}

View File

@ -12,8 +12,8 @@ if (!class_exists('GenerateChangelogCommand', false) && !interface_exists('Gener
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211209\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625', false) && !interface_exists('ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625', false) && !trait_exists('ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625', false)) {
spl_autoload_call('RectorPrefix20211209\ComposerAutoloaderInitea6be250a9b3ddb5ee7f07c3b6239625');
if (!class_exists('ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d', false) && !interface_exists('ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d', false) && !trait_exists('ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d', false)) {
spl_autoload_call('RectorPrefix20211209\ComposerAutoloaderInit2b4e877593f8f88c3998a9357665b46d');
}
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('RectorPrefix20211209\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -81,9 +81,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211209\print_node(...func_get_args());
}
}
if (!function_exists('composerRequireea6be250a9b3ddb5ee7f07c3b6239625')) {
function composerRequireea6be250a9b3ddb5ee7f07c3b6239625() {
return \RectorPrefix20211209\composerRequireea6be250a9b3ddb5ee7f07c3b6239625(...func_get_args());
if (!function_exists('composerRequire2b4e877593f8f88c3998a9357665b46d')) {
function composerRequire2b4e877593f8f88c3998a9357665b46d() {
return \RectorPrefix20211209\composerRequire2b4e877593f8f88c3998a9357665b46d(...func_get_args());
}
}
if (!function_exists('scanPath')) {