Updated Rector to commit 9163864800

9163864800 Remove DowngradeGeneratedScalarTypesRector as way hacky to particular string solution, better use patching (#2289)
This commit is contained in:
Tomas Votruba 2022-05-11 16:29:59 +00:00
parent f3598b9c94
commit a48a02b46d
9 changed files with 21 additions and 198 deletions

View File

@ -22,7 +22,6 @@ use Rector\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector;
use Rector\DowngradePhp70\Rector\MethodCall\DowngradeMethodCallOnCloneRector;
use Rector\DowngradePhp70\Rector\New_\DowngradeAnonymousClassRector;
use Rector\DowngradePhp70\Rector\Spaceship\DowngradeSpaceshipRector;
use Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector;
use Rector\DowngradePhp70\Rector\TryCatch\DowngradeCatchThrowableRector;
return static function (\Rector\Config\RectorConfig $rectorConfig) : void {
$rectorConfig->phpVersion(\Rector\Core\ValueObject\PhpVersion::PHP_56);
@ -41,7 +40,6 @@ return static function (\Rector\Config\RectorConfig $rectorConfig) : void {
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\FuncCall\DowngradeUncallableValueCallToCallUserFuncRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\GroupUse\SplitGroupedUseImportsRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\MethodCall\DowngradeMethodCallOnCloneRector::class);
$rectorConfig->rule(\Rector\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector::class);

View File

@ -1,4 +1,4 @@
# 512 Rules Overview
# 511 Rules Overview
<br>
@ -28,7 +28,7 @@
- [DowngradePhp56](#downgradephp56) (5)
- [DowngradePhp70](#downgradephp70) (19)
- [DowngradePhp70](#downgradephp70) (18)
- [DowngradePhp71](#downgradephp71) (11)
@ -4221,27 +4221,6 @@ Replace the 2nd argument of `dirname()`
<br>
### DowngradeGeneratedScalarTypesRector
Refactor scalar types in PHP code in string snippets, e.g. generated container code from symfony/dependency-injection
- class: [`Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector`](../rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php)
```diff
$code = <<<'EOF'
- public function getParameter(string $name)
+ /**
+ * @param string $name
+ */
+ public function getParameter($name)
{
return $name;
}
EOF;
```
<br>
### DowngradeInstanceofThrowableRector
Add `instanceof Exception` check as a fallback to `instanceof Throwable` to support exception hierarchies in PHP 5

View File

@ -1,152 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp70\Rector\String_;
use RectorPrefix20220511\Nette\Utils\Strings;
use PhpParser\Error;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\PhpParser\Parser\InlineCodeParser;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector;
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @changelog https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1516
*
* @see \Rector\Tests\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector\DowngradeGeneratedScalarTypesRectorTest
*/
final class DowngradeGeneratedScalarTypesRector extends \Rector\Core\Rector\AbstractRector
{
/**
* Extends list here as needed
* @var string[]
*/
private const FILES_TO_INCLUDE = [
// https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1516
'vendor/symfony/dependency-injection/Dumper/PhpDumper.php',
];
/**
* @var PhpRectorInterface[]
*/
private $phpRectors = [];
/**
* @readonly
* @var \Rector\Core\PhpParser\Parser\InlineCodeParser
*/
private $inlineCodeParser;
/**
* @readonly
* @var \Rector\Core\Contract\PhpParser\NodePrinterInterface
*/
private $nodePrinter;
public function __construct(\Rector\Core\PhpParser\Parser\InlineCodeParser $inlineCodeParser, \Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector $downgradeScalarTypeDeclarationRector, \Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector $downgradeVoidTypeDeclarationRector, \Rector\Core\Contract\PhpParser\NodePrinterInterface $nodePrinter)
{
$this->inlineCodeParser = $inlineCodeParser;
$this->nodePrinter = $nodePrinter;
$this->phpRectors = [$downgradeScalarTypeDeclarationRector, $downgradeVoidTypeDeclarationRector];
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Refactor scalar types in PHP code in string snippets, e.g. generated container code from symfony/dependency-injection', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
$code = <<<'EOF'
public function getParameter(string $name)
{
return $name;
}
EOF;
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$code = <<<'EOF'
/**
* @param string $name
*/
public function getParameter($name)
{
return $name;
}
EOF;
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Scalar\String_::class];
}
/**
* @param String_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
$smartFileInfo = $this->file->getSmartFileInfo();
// this rule is parsing strings, so it heavy on performance; to lower it, we'll process only known opt-in files
if (!$this->isRelevantFileInfo($smartFileInfo)) {
return null;
}
$stringKind = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::KIND);
if (!\in_array($stringKind, [\PhpParser\Node\Scalar\String_::KIND_NOWDOC, \PhpParser\Node\Scalar\String_::KIND_HEREDOC], \true)) {
return null;
}
// we assume its a function list - see https://github.com/symfony/symfony/blob/ad91659ea9b2a964f933bf27d0d1f1ef60fe9417/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1513-L1560
try {
$nodes = $this->inlineCodeParser->parse('<?php class SomeClass { ' . $node->value . ' }');
} catch (\PhpParser\Error $exception) {
// nothing we can do
return null;
}
if ($nodes === []) {
return null;
}
// * replace scalar types with docs
// * remove return type
// somehow we want to call all Rector rules here
$nodeTraverser = $this->createNodeTraverser();
$changedNodes = $nodeTraverser->traverse($nodes);
if (!$changedNodes[0] instanceof \PhpParser\Node\Stmt\Class_) {
return null;
}
$node->value = $this->printClassStmts($changedNodes[0]);
return $node;
}
private function isRelevantFileInfo(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo) : bool
{
// for tests
if (\Rector\Testing\PHPUnit\StaticPHPUnitEnvironment::isPHPUnitRun()) {
return \true;
}
foreach (self::FILES_TO_INCLUDE as $fileToInclude) {
if (\substr_compare($fileInfo->getRealPath(), $fileToInclude, -\strlen($fileToInclude)) === 0) {
return \true;
}
}
return \false;
}
private function printClassStmts(\PhpParser\Node\Stmt\Class_ $class) : string
{
$refactoredContent = '';
foreach ($class->stmts as $classStmt) {
$refactoredContent .= $this->nodePrinter->prettyPrint([$classStmt]) . \PHP_EOL;
}
return $refactoredContent;
}
private function createNodeTraverser() : \PhpParser\NodeTraverser
{
$nodeTraverser = new \PhpParser\NodeTraverser();
foreach ($this->phpRectors as $phpRector) {
$nodeTraverser->addVisitor($phpRector);
}
return $nodeTraverser;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'b8ff9ab25c00a2d6f9ad86d6999be4cb5fd2020f';
public const PACKAGE_VERSION = '9163864800fc12275062f70a2d490cfd1d55047f';
/**
* @var string
*/
public const RELEASE_DATE = '2022-05-11 14:38:55';
public const RELEASE_DATE = '2022-05-11 16:21:59';
/**
* @var string
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8::getLoader();
return ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a::getLoader();

View File

@ -2007,7 +2007,6 @@ return array(
'Rector\\DowngradePhp70\\Rector\\MethodCall\\DowngradeMethodCallOnCloneRector' => $baseDir . '/rules/DowngradePhp70/Rector/MethodCall/DowngradeMethodCallOnCloneRector.php',
'Rector\\DowngradePhp70\\Rector\\New_\\DowngradeAnonymousClassRector' => $baseDir . '/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php',
'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',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8
class ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire7f7147627f3b0978b8e8020733ae9ef8($fileIdentifier, $file);
composerRequireec88b0f2d4840b45d0dc2d87bf7c174a($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8
* @param string $file
* @return void
*/
function composerRequire7f7147627f3b0978b8e8020733ae9ef8($fileIdentifier, $file)
function composerRequireec88b0f2d4840b45d0dc2d87bf7c174a($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 ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8
class ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -2376,7 +2376,6 @@ class ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8
'Rector\\DowngradePhp70\\Rector\\MethodCall\\DowngradeMethodCallOnCloneRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/MethodCall/DowngradeMethodCallOnCloneRector.php',
'Rector\\DowngradePhp70\\Rector\\New_\\DowngradeAnonymousClassRector' => __DIR__ . '/../..' . '/rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php',
'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',
@ -3889,9 +3888,9 @@ class ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7f7147627f3b0978b8e8020733ae9ef8::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitec88b0f2d4840b45d0dc2d87bf7c174a::$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('RectorPrefix20220511\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8', false) && !interface_exists('ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8', false) && !trait_exists('ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8', false)) {
spl_autoload_call('RectorPrefix20220511\ComposerAutoloaderInit7f7147627f3b0978b8e8020733ae9ef8');
if (!class_exists('ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a', false) && !interface_exists('ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a', false) && !trait_exists('ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a', false)) {
spl_autoload_call('RectorPrefix20220511\ComposerAutoloaderInitec88b0f2d4840b45d0dc2d87bf7c174a');
}
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('RectorPrefix20220511\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220511\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire7f7147627f3b0978b8e8020733ae9ef8')) {
function composerRequire7f7147627f3b0978b8e8020733ae9ef8() {
return \RectorPrefix20220511\composerRequire7f7147627f3b0978b8e8020733ae9ef8(...func_get_args());
if (!function_exists('composerRequireec88b0f2d4840b45d0dc2d87bf7c174a')) {
function composerRequireec88b0f2d4840b45d0dc2d87bf7c174a() {
return \RectorPrefix20220511\composerRequireec88b0f2d4840b45d0dc2d87bf7c174a(...func_get_args());
}
}
if (!function_exists('scanPath')) {