Updated Rector to commit 55be345ca9

55be345ca9 Various improvements (#2291)
This commit is contained in:
Tomas Votruba 2022-05-11 17:53:44 +00:00
parent f0a799c4c1
commit 060b1642ed
13 changed files with 74 additions and 51 deletions

View File

@ -116,8 +116,8 @@ CODE_SAMPLE
if (!$assignScope instanceof \PHPStan\Analyser\Scope) {
return null;
}
$currentStatement = $this->betterNodeFinder->resolveCurrentStatement($funcCall);
if (!$currentStatement instanceof \PhpParser\Node\Stmt) {
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($funcCall);
if (!$currentStmt instanceof \PhpParser\Node\Stmt) {
return null;
}
$isCompactOfUndefinedVariables = $this->arrayItemsAnalyzer->hasArrayExclusiveDefinedVariableNames($array, $assignScope);
@ -138,7 +138,7 @@ CODE_SAMPLE
$firstArg = $funcCall->args[0];
$assignVariable = $firstArg->value;
$preAssign = new \PhpParser\Node\Expr\Assign($assignVariable, $array);
$this->nodesToAddCollector->addNodeBeforeNode($preAssign, $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode($preAssign, $currentStmt);
return $expr;
}
private function refactorAssignArray(\PhpParser\Node\Expr $expr, \PhpParser\Node\Expr\FuncCall $funcCall) : ?\PhpParser\Node\Expr

View File

@ -24,6 +24,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class DowngradeArrayIsListRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var \PhpParser\Node\Expr\Closure|null
*/
private $cachedClosure;
/**
* @readonly
* @var \Rector\Core\PhpParser\Parser\InlineCodeParser
@ -95,6 +99,9 @@ CODE_SAMPLE
}
private function createClosure() : \PhpParser\Node\Expr\Closure
{
if ($this->cachedClosure instanceof \PhpParser\Node\Expr\Closure) {
return clone $this->cachedClosure;
}
$stmts = $this->inlineCodeParser->parse(__DIR__ . '/../../snippet/array_is_list_closure.php.inc');
/** @var Expression $expression */
$expression = $stmts[0];
@ -102,6 +109,7 @@ CODE_SAMPLE
if (!$expr instanceof \PhpParser\Node\Expr\Closure) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
$this->cachedClosure = $expr;
return $expr;
}
private function shouldSkip(\PhpParser\Node\Expr\FuncCall $funcCall) : bool

View File

@ -81,7 +81,7 @@ CODE_SAMPLE
// StaticCall
$class = $node->class instanceof \PhpParser\Node\Name ? new \PhpParser\Node\Expr\ClassConstFetch($node->class, 'class') : $node->class;
$method = $node->name instanceof \PhpParser\Node\Identifier ? new \PhpParser\Node\Scalar\String_($node->name->toString()) : $node->name;
return new \PhpParser\Node\Expr\Array_([new \PhpParser\Node\Expr\ArrayItem($class), new \PhpParser\Node\Expr\ArrayItem($method)]);
return $this->nodeFactory->createArray([$class, $method]);
}
private function createClosureFromCallableCall(\PhpParser\Node\Expr $expr) : \PhpParser\Node\Expr\StaticCall
{

View File

@ -6,17 +6,14 @@ namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\EarlyReturn\NodeFactory\InvertedIfFactory;
@ -96,16 +93,15 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Function_::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Expr\Closure::class, \PhpParser\Node\Stmt\Foreach_::class, \PhpParser\Node\Stmt\If_::class, \PhpParser\Node\Stmt\Else_::class, \PhpParser\Node\Stmt\ElseIf_::class];
return [\Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface::class];
}
/**
* @param Function_|ClassMethod|Closure|Foreach_|If_|Else_|ElseIf_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
/** @var Stmt[]|null $stmts */
$stmts = $node->stmts;
if ($stmts === null) {
$stmts = (array) $node->stmts;
if ($stmts === []) {
return null;
}
$newStmts = [];
@ -183,7 +179,7 @@ CODE_SAMPLE
}
return \array_merge($result, [$ifNextReturnClone]);
}
private function shouldSkip(\PhpParser\Node $parentNode, \PhpParser\Node\Stmt\If_ $if, ?\PhpParser\Node\Stmt $nexStmt) : bool
private function shouldSkip(\Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface $stmtsAware, \PhpParser\Node\Stmt\If_ $if, ?\PhpParser\Node\Stmt $nexStmt) : bool
{
if (!$this->ifManipulator->isIfWithOnlyOneStmt($if)) {
return \true;
@ -194,10 +190,10 @@ CODE_SAMPLE
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($if)) {
return \true;
}
if ($this->isParentIfReturnsVoidOrParentIfHasNextNode($parentNode)) {
if ($this->isParentIfReturnsVoidOrParentIfHasNextNode($stmtsAware)) {
return \true;
}
if ($this->isNestedIfInLoop($if, $parentNode)) {
if ($this->isNestedIfInLoop($if, $stmtsAware)) {
return \true;
}
return !$this->isLastIfOrBeforeLastReturn($if, $nexStmt);
@ -218,20 +214,20 @@ CODE_SAMPLE
}
return \false;
}
private function isParentIfReturnsVoidOrParentIfHasNextNode(\PhpParser\Node $parentNode) : bool
private function isParentIfReturnsVoidOrParentIfHasNextNode(\Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface $stmtsAware) : bool
{
if (!$parentNode instanceof \PhpParser\Node\Stmt\If_) {
if (!$stmtsAware instanceof \PhpParser\Node\Stmt\If_) {
return \false;
}
$nextParent = $parentNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE);
$nextParent = $stmtsAware->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE);
return $nextParent instanceof \PhpParser\Node;
}
private function isNestedIfInLoop(\PhpParser\Node\Stmt\If_ $if, ?\PhpParser\Node $parentNode) : bool
private function isNestedIfInLoop(\PhpParser\Node\Stmt\If_ $if, \Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface $stmtsAware) : bool
{
if (!$this->contextAnalyzer->isInLoop($if)) {
return \false;
}
return $parentNode instanceof \PhpParser\Node\Stmt\If_ || $parentNode instanceof \PhpParser\Node\Stmt\Else_ || $parentNode instanceof \PhpParser\Node\Stmt\ElseIf_;
return $stmtsAware instanceof \PhpParser\Node\Stmt\If_ || $stmtsAware instanceof \PhpParser\Node\Stmt\Else_ || $stmtsAware instanceof \PhpParser\Node\Stmt\ElseIf_;
}
private function isLastIfOrBeforeLastReturn(\PhpParser\Node\Stmt\If_ $if, ?\PhpParser\Node\Stmt $nextStmt) : bool
{

View File

@ -108,11 +108,11 @@ final class NonVariableToVariableOnFunctionCallRector extends \Rector\Core\Recto
continue;
}
$replacements = $this->getReplacementsFor($argument, $currentScope, $scopeNode);
$currentStatement = $this->betterNodeFinder->resolveCurrentStatement($node);
if (!$currentStatement instanceof \PhpParser\Node\Stmt) {
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($node);
if (!$currentStmt instanceof \PhpParser\Node\Stmt) {
continue;
}
$this->nodesToAddCollector->addNodeBeforeNode($replacements->getAssign(), $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode($replacements->getAssign(), $currentStmt);
$node->args[$key]->value = $replacements->getVariable();
// add variable name to scope, so we prevent duplication of new variable of the same name
$currentScope = $currentScope->assignExpression($replacements->getVariable(), $currentScope->getType($replacements->getVariable()));

View File

@ -5,6 +5,7 @@ namespace Rector\Php81\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Enum_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
@ -67,7 +68,7 @@ CODE_SAMPLE
/**
* @param Class_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node\Stmt\Enum_
{
if (!$this->isObjectType($node, new \PHPStan\Type\ObjectType('Spatie\\Enum\\Enum'))) {
return null;

View File

@ -41,6 +41,10 @@ final class ClassRenamer
* @var string[]
*/
private $alreadyProcessedClasses = [];
/**
* @var array<string, OldToNewType[]>
*/
private $oldToNewTypesByCacheKey = [];
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
@ -115,10 +119,7 @@ final class ClassRenamer
*/
public function renameNode(\PhpParser\Node $node, array $oldToNewClasses) : ?\PhpParser\Node
{
$oldToNewTypes = [];
foreach ($oldToNewClasses as $oldClass => $newClass) {
$oldToNewTypes[] = new \Rector\NodeTypeResolver\ValueObject\OldToNewType(new \PHPStan\Type\ObjectType($oldClass), new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($newClass));
}
$oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses);
$this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses);
if ($node instanceof \PhpParser\Node\Name) {
return $this->refactorName($node, $oldToNewClasses);
@ -402,4 +403,23 @@ final class ClassRenamer
{
return $last === $newNameLastName && $importNames;
}
/**
* @param array<string, string> $oldToNewClasses
* @return OldToNewType[]
*/
private function createOldToNewTypes(array $oldToNewClasses) : array
{
$cacheKey = \md5(\serialize($oldToNewClasses));
if (isset($this->oldToNewTypesByCacheKey[$cacheKey])) {
return $this->oldToNewTypesByCacheKey[$cacheKey];
}
$oldToNewTypes = [];
foreach ($oldToNewClasses as $oldClass => $newClass) {
$oldObjectType = new \PHPStan\Type\ObjectType($oldClass);
$newObjectType = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($newClass);
$oldToNewTypes[] = new \Rector\NodeTypeResolver\ValueObject\OldToNewType($oldObjectType, $newObjectType);
}
$this->oldToNewTypesByCacheKey[$cacheKey] = $oldToNewTypes;
return $oldToNewTypes;
}
}

View File

@ -82,9 +82,7 @@ CODE_SAMPLE
$node->stmts = $stmts;
// add a new namespace?
if ($this->newNamespace !== null) {
$namespace = new \PhpParser\Node\Stmt\Namespace_(new \PhpParser\Node\Name($this->newNamespace));
$namespace->stmts = $stmts;
return $namespace;
return new \PhpParser\Node\Stmt\Namespace_(new \PhpParser\Node\Name($this->newNamespace), $stmts);
}
}
if ($node instanceof \PhpParser\Node\Stmt\Namespace_) {

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '17d200dfa6825602e214098e7d6637cae1d153f6';
public const PACKAGE_VERSION = '55be345ca9e2b0f29330656a1921dd82053981b2';
/**
* @var string
*/
public const RELEASE_DATE = '2022-05-11 16:45:25';
public const RELEASE_DATE = '2022-05-11 17:47:37';
/**
* @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 ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0::getLoader();
return ComposerAutoloaderInit062427c17b655f7df0906f86736056ae::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0
class ComposerAutoloaderInit062427c17b655f7df0906f86736056ae
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit062427c17b655f7df0906f86736056ae', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit062427c17b655f7df0906f86736056ae', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit062427c17b655f7df0906f86736056ae::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit062427c17b655f7df0906f86736056ae::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire93beddbe64bbb5e5f67d79aabdd8b9a0($fileIdentifier, $file);
composerRequire062427c17b655f7df0906f86736056ae($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0
* @param string $file
* @return void
*/
function composerRequire93beddbe64bbb5e5f67d79aabdd8b9a0($fileIdentifier, $file)
function composerRequire062427c17b655f7df0906f86736056ae($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 ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0
class ComposerStaticInit062427c17b655f7df0906f86736056ae
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -3888,9 +3888,9 @@ class ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit93beddbe64bbb5e5f67d79aabdd8b9a0::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit062427c17b655f7df0906f86736056ae::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit062427c17b655f7df0906f86736056ae::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit062427c17b655f7df0906f86736056ae::$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('ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0', false) && !interface_exists('ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0', false) && !trait_exists('ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0', false)) {
spl_autoload_call('RectorPrefix20220511\ComposerAutoloaderInit93beddbe64bbb5e5f67d79aabdd8b9a0');
if (!class_exists('ComposerAutoloaderInit062427c17b655f7df0906f86736056ae', false) && !interface_exists('ComposerAutoloaderInit062427c17b655f7df0906f86736056ae', false) && !trait_exists('ComposerAutoloaderInit062427c17b655f7df0906f86736056ae', false)) {
spl_autoload_call('RectorPrefix20220511\ComposerAutoloaderInit062427c17b655f7df0906f86736056ae');
}
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('composerRequire93beddbe64bbb5e5f67d79aabdd8b9a0')) {
function composerRequire93beddbe64bbb5e5f67d79aabdd8b9a0() {
return \RectorPrefix20220511\composerRequire93beddbe64bbb5e5f67d79aabdd8b9a0(...func_get_args());
if (!function_exists('composerRequire062427c17b655f7df0906f86736056ae')) {
function composerRequire062427c17b655f7df0906f86736056ae() {
return \RectorPrefix20220511\composerRequire062427c17b655f7df0906f86736056ae(...func_get_args());
}
}
if (!function_exists('scanPath')) {