diff --git a/packages/NodeTypeResolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/PropertyFetchTypeResolverTest.php b/packages/NodeTypeResolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/PropertyFetchTypeResolverTest.php index 571e67830c4..323939bf066 100644 --- a/packages/NodeTypeResolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/PropertyFetchTypeResolverTest.php +++ b/packages/NodeTypeResolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/PropertyFetchTypeResolverTest.php @@ -25,7 +25,7 @@ final class PropertyFetchTypeResolverTest extends AbstractNodeTypeResolverTest $propertyFetchNodes = $this->getNodesForFileOfType($file, PropertyFetch::class); $propertyFetchNode = $propertyFetchNodes[$nodePosition]; - $this->assertSame($propertyName, $propertyFetchNode->name->toString()); + $this->assertSame($propertyName, (string) $propertyFetchNode->name); $this->assertSame($expectedTypes, $this->nodeTypeResolver->resolve($propertyFetchNode)); } diff --git a/phpstan.neon b/phpstan.neon index e98f53a304f..348ab8a7a81 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -19,37 +19,30 @@ parameters: # known values - '#Argument of an invalid type array\|null supplied for foreach, only iterables are supported#' # 1 - '#Access to an undefined property PHPStan\\PhpDocParser\\Ast\\Node::\$name#' # 2 - - '#Calling method getName\(\) on possibly null value of type Rector\\Builder\\Class_\\VariableInfo\|null#' # 1 - - '#Method Rector\\Node\\NodeFactory::create(Null|False)Constant\(\) should return PhpParser\\Node\\Expr\\ConstFetch but returns PhpParser\\Node\\Expr#' # 1 + - '#Method Rector\\Node\\NodeFactory::createNullConstant\(\) should return PhpParser\\Node\\Expr\\ConstFetch but returns PhpParser\\Node\\Expr#' # 1 - '#Method Rector\\Node\\NodeFactory::createNamespace\(\) should return PhpParser\\Node\\Stmt\\Namespace_ but returns PhpParser\\Node#' # 1 - '#Calling method getText\(\) on possibly null value of type PhpParser\\Comment\\Doc\|null#' # 3 - - '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\Identifier will always evaluate to false#' # 8 + - '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\(Identifier|Name) will always evaluate to false#' # 9 - '#Method Rector\\NodeTraverserQueue\\BetterNodeFinder::findFirstAncestorInstanceOf\(\) should return PhpParser\\Node\|null but returns object#' # 1 - - '#Access to an undefined property PhpParser\\Node\\Stmt\\ClassLike::\$namespacedName#' # 2 - - '#Access to an undefined property PhpParser\\Node\\Stmt\\Class_::\$namespacedName#' # 1 + - '#Access to an undefined property PhpParser\\Node\\Stmt\\(Class_|ClassLike)::\$namespacedName#' # 3 - '#Property Rector\\NodeTypeResolver\\NodeVisitor\\NamespaceResolver::\$useNodes \(array\) does not accept array#' # 1 - '#Parameter \#1 \$classMethodNode of method Rector\\NodeTypeResolver\\TypesExtractor\\ConstructorPropertyTypesExtractor::extractPropertiesFromConstructorMethodNode\(\) expects PhpParser\\Node\\Stmt\\ClassMethod, PhpParser\\Node given#' # 1 - - '#Instanceof between PhpParser\\Node\\Expr\|string and PhpParser\\Node\\Name will always evaluate to false#' # 1 - '#Parameter \#1 \$node of method Rector\\NodeTypeResolver\\NodeTypeResolver::resolve\(\) expects PhpParser\\Node, PhpParser\\Node\\Expr|string given#' # 3 - '#Method Rector\\NodeTraverser\\RectorNodeTraverser::getRectors\(\) should return array but returns array#' # 1 - # known value of Name of MethodCall - - '#Cannot call method toString\(\) on PhpParser\\Node\\Expr\|string#' # 4 - # buggy - - '#Access to an undefined property PhpParser\\Node::\$args#' # 8 + - '#Access to an undefined property PhpParser\\Node\\Expr::\$value#' # 2 - '#Parameter \#1 \$classLikeNode of method Rector\\NodeAnalyzer\\ClassLikeAnalyzer::resolveExtendsTypes\(\) expects PhpParser\\Node\\Stmt\\Class_\|PhpParser\\Node\\Stmt\\Interface_, PhpParser\\Node\\Stmt\\ClassLike given#' # 1 - '#Parameter \#1 \$functionLikeNode of method Rector\\NodeTypeResolver\\TypeContext::getFunctionReflection\(\) expects PhpParser\\Node\\Expr\\Closure\|PhpParser\\Node\\Stmt\\ClassMethod\|PhpParser\\Node\\Stmt\\Function_, PhpParser\\Node\\FunctionLike given#' # 1 - '#Access to an undefined property PhpParser\\Node\\Param::\$var#' # 4 - - '#Call to an undefined method PhpParser\\BuilderFactory::(classConstFetch|methodCall|staticCall)\(\)#' # 8 - - '#Call to an undefined method PhpParser\\BuilderFactory::args\(\)#' # 1 + - '#Call to an undefined method PhpParser\\BuilderFactory::(args|classConstFetch|methodCall|staticCall)\(\)#' # 9 - "#Casting to string something that's already string#" # 4 - '#Call to an undefined method PhpParser\\Node\\Stmt\\UseUse::getAlias\(\)#' # 2 - '#Call to an undefined method PhpParser\\PrettyPrinter\\Standard::printFormatPreserving\(\)#' # 1 - - '#Access to an undefined property PhpParser\\Node\\Expr::\$var#' # 1 - - '#Access to an undefined property PhpParser\\Node\\Expr::\$name#' # 2 - - '#Call to an undefined method PhpParser\\Node\\Expr|PhpParser\\Node\\Name::toCodeString\(\).#' # 9 + - '#Access to an undefined property PhpParser\\Node\\Expr::\$(name|var)#' # 2 + - '#Call to an undefined method PhpParser\\Node\\Expr\\BinaryOp::getOperatorSigil\(\).#' # 1 + - '#Call to an undefined method PhpParser\\Node\\Expr\\ArrayItem::getComments\(\).#' # 2 excludes_analyse: # test files diff --git a/src/NodeAnalyzer/ChainMethodCallAnalyzer.php b/src/NodeAnalyzer/ChainMethodCallAnalyzer.php index 6e745b5c845..7b24b8dc4f3 100644 --- a/src/NodeAnalyzer/ChainMethodCallAnalyzer.php +++ b/src/NodeAnalyzer/ChainMethodCallAnalyzer.php @@ -36,20 +36,18 @@ final class ChainMethodCallAnalyzer // node chaining is in reverse order than code $methods = array_reverse($methods); - $currentMethodCall = $node; - foreach ($methods as $method) { - if ((string) $currentMethodCall->name !== $method) { + if ((string) $node->name !== $method) { return false; } - $currentMethodCall = $currentMethodCall->var; - if ($currentMethodCall instanceof MethodCall) { + $node = $node->var; + if ($node instanceof MethodCall) { continue; } } - $variableTypes = $this->nodeTypeResolver->resolve($currentMethodCall); + $variableTypes = $this->nodeTypeResolver->resolve($node); return in_array($type, $variableTypes, true); } diff --git a/src/NodeAnalyzer/MethodArgumentAnalyzer.php b/src/NodeAnalyzer/MethodArgumentAnalyzer.php index 92dc1b74c29..5c87cd33383 100644 --- a/src/NodeAnalyzer/MethodArgumentAnalyzer.php +++ b/src/NodeAnalyzer/MethodArgumentAnalyzer.php @@ -33,16 +33,21 @@ final class MethodArgumentAnalyzer return false; } - return $node->args[$position - 1]->value instanceof String_; + /** @var MethodCall $methodCallNode */ + $methodCallNode = $node; + + return $methodCallNode->args[$position - 1]->value instanceof String_; } - public function isMethodNthArgumentNull(Node $methodCallNode, int $position): bool + public function isMethodNthArgumentNull(Node $node, int $position): bool { - if (! $this->hasMethodNthArgument($methodCallNode, $position)) { + if (! $this->hasMethodNthArgument($node, $position)) { return false; } /** @var MethodCall $methodCallNode */ + $methodCallNode = $node; + $value = $methodCallNode->args[$position - 1]->value; if (! $value instanceof ConstFetch) { return false; diff --git a/src/Rector/Architecture/DependencyInjection/ReplaceVariableByPropertyFetchRector.php b/src/Rector/Architecture/DependencyInjection/ReplaceVariableByPropertyFetchRector.php index 29e4d5f23b8..195250c9f5d 100644 --- a/src/Rector/Architecture/DependencyInjection/ReplaceVariableByPropertyFetchRector.php +++ b/src/Rector/Architecture/DependencyInjection/ReplaceVariableByPropertyFetchRector.php @@ -22,7 +22,7 @@ final class ReplaceVariableByPropertyFetchRector extends AbstractRector private $variablesToPropertyFetchCollection; /** - * @var VariableInfo|null + * @var VariableInfo */ private $activeVariableInfo; @@ -52,7 +52,7 @@ final class SomeController * @var ProductRepository */ private $productRepository; - + public function __construct(ProductRepository $productRepository) { $this->productRepository = $productRepository; @@ -61,7 +61,7 @@ final class SomeController public function default() { $products = $productRepository->fetchAll(); - } + } } CODE_SAMPLE , @@ -72,7 +72,7 @@ final class SomeController * @var ProductRepository */ private $productRepository; - + public function __construct(ProductRepository $productRepository) { $this->productRepository = $productRepository; @@ -81,7 +81,7 @@ final class SomeController public function default() { $products = $this->productRepository->fetchAll(); - } + } } CODE_SAMPLE ), @@ -91,8 +91,6 @@ CODE_SAMPLE public function isCandidate(Node $node): bool { - $this->activeVariableInfo = null; - if (! $node instanceof Variable) { return false; } diff --git a/src/Rector/Architecture/RepositoryAsService/ReplaceParentRepositoryCallsByRepositoryPropertyRector.php b/src/Rector/Architecture/RepositoryAsService/ReplaceParentRepositoryCallsByRepositoryPropertyRector.php index 3ac46e19b2e..b3ec2b9bbb0 100644 --- a/src/Rector/Architecture/RepositoryAsService/ReplaceParentRepositoryCallsByRepositoryPropertyRector.php +++ b/src/Rector/Architecture/RepositoryAsService/ReplaceParentRepositoryCallsByRepositoryPropertyRector.php @@ -64,7 +64,7 @@ final class ReplaceParentRepositoryCallsByRepositoryPropertyRector extends Abstr args) !== 1) { + /** @var MethodCall $methodCallNode */ + $methodCallNode = $node; + + if (count($methodCallNode->args) !== 1) { return false; } - if ($node->args[0]->value instanceof String_) { + if ($methodCallNode->args[0]->value instanceof String_) { /** @var String_ $string */ - $string = $node->args[0]->value; + $string = $methodCallNode->args[0]->value; // is alias if (Strings::contains($string->value, ':')) { diff --git a/src/Rector/Contrib/Doctrine/AliasToClassRector.php b/src/Rector/Contrib/Doctrine/AliasToClassRector.php index f8a85936e62..5f1a152990d 100644 --- a/src/Rector/Contrib/Doctrine/AliasToClassRector.php +++ b/src/Rector/Contrib/Doctrine/AliasToClassRector.php @@ -3,6 +3,7 @@ namespace Rector\Rector\Contrib\Doctrine; use PhpParser\Node; +use PhpParser\Node\Expr\MethodCall; use Rector\Node\NodeFactory; use Rector\NodeAnalyzer\MethodArgumentAnalyzer; use Rector\NodeAnalyzer\MethodCallAnalyzer; @@ -64,16 +65,22 @@ final class AliasToClassRector extends AbstractRector return false; } - return $this->isAliasWithConfiguredEntity($node->args[0]->value->value); + /** @var MethodCall $methodCall */ + $methodCall = $node; + + return $this->isAliasWithConfiguredEntity($methodCall->args[0]->value->value); } - public function refactor(Node $node): ?Node + /** + * @param MethodCall $methodCall + */ + public function refactor(Node $methodCall): ?Node { - $node->args[0]->value = $this->nodeFactory->createClassConstantReference( - $this->convertAliasToFqn($node->args[0]->value->value) + $methodCall->args[0]->value = $this->nodeFactory->createClassConstantReference( + $this->convertAliasToFqn($methodCall->args[0]->value->value) ); - return $node; + return $methodCall; } private function isAlias(string $name): bool diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertCompareToSpecificMethodRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertCompareToSpecificMethodRector.php index f20dae1b333..a5a9825309d 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertCompareToSpecificMethodRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertCompareToSpecificMethodRector.php @@ -96,7 +96,7 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector return false; } - $methodName = $secondArgumentValue->name->toString(); + $methodName = (string) $secondArgumentValue->name; $this->activeFuncCallName = $methodName; return isset($this->defaultOldToNewMethods[$methodName]); diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertIssetToSpecificMethodRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertIssetToSpecificMethodRector.php index 7265d0a21c9..3b7ca0e15ca 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertIssetToSpecificMethodRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertIssetToSpecificMethodRector.php @@ -111,7 +111,7 @@ final class AssertIssetToSpecificMethodRector extends AbstractPHPUnitRector unset($oldArgs[0]); $node->args = array_merge($this->nodeFactory->createArgs([ - $this->nodeFactory->createString($propertyFetchNode->name->toString()), + $this->nodeFactory->createString((string) $propertyFetchNode->name), $propertyFetchNode->var, ]), $oldArgs); } diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertPropertyExistsRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertPropertyExistsRector.php index 42fe4624982..ef76c8d392c 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertPropertyExistsRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertPropertyExistsRector.php @@ -95,7 +95,7 @@ final class AssertPropertyExistsRector extends AbstractPHPUnitRector return false; } - $methodName = $firstArgumentValue->name->toString(); + $methodName = (string) $firstArgumentValue->name; return $methodName === 'property_exists'; } diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertRegExpRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertRegExpRector.php index 79c782ed63c..e28161bfa06 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertRegExpRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertRegExpRector.php @@ -73,7 +73,7 @@ final class AssertRegExpRector extends AbstractPHPUnitRector return false; } - $methodName = $secondArgumentValue->name->toString(); + $methodName = (string) $secondArgumentValue->name; return $methodName === 'preg_match'; } diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseInternalTypeToSpecificMethodRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseInternalTypeToSpecificMethodRector.php index 005e4961c6d..cf02151ea84 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseInternalTypeToSpecificMethodRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseInternalTypeToSpecificMethodRector.php @@ -105,7 +105,7 @@ final class AssertTrueFalseInternalTypeToSpecificMethodRector extends AbstractPH return false; } - $methodName = $firstArgumentValue->name->toString(); + $methodName = (string) $firstArgumentValue->name; return isset($this->oldMethodsToTypes[$methodName]); } @@ -126,7 +126,7 @@ final class AssertTrueFalseInternalTypeToSpecificMethodRector extends AbstractPH /** @var FuncCall $isFunctionNode */ $isFunctionNode = $methodCallNode->args[0]->value; $argument = $isFunctionNode->args[0]->value; - $isFunctionName = $isFunctionNode->name->toString(); + $isFunctionName = (string) $isFunctionNode->name; $oldArguments = $methodCallNode->args; unset($oldArguments[0]); diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php index e27d7b6e268..1e25bf74d20 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php @@ -154,7 +154,7 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractPHPUnitRector { $funcCallOrEmptyNode = $methodCallNode->args[0]->value; if ($funcCallOrEmptyNode instanceof FuncCall) { - $funcCallOrEmptyNodeName = $funcCallOrEmptyNode->name->toString(); + $funcCallOrEmptyNodeName = (string) $funcCallOrEmptyNode->name; $funcCallOrEmptyNodeArgs = $funcCallOrEmptyNode->args; $oldArguments = $methodCallNode->args; unset($oldArguments[0]); diff --git a/src/Rector/Contrib/Symfony/Form/OptionNameRector.php b/src/Rector/Contrib/Symfony/Form/OptionNameRector.php index e280267b88a..87894fff5b2 100644 --- a/src/Rector/Contrib/Symfony/Form/OptionNameRector.php +++ b/src/Rector/Contrib/Symfony/Form/OptionNameRector.php @@ -58,17 +58,18 @@ final class OptionNameRector extends AbstractRector } $arrayParentNode = $arrayItemParentNode->getAttribute(Attribute::PARENT_NODE); - $argParentNode = $arrayParentNode->getAttribute(Attribute::PARENT_NODE); /** @var MethodCall $argParentNode */ - $methodCallNode = $argParentNode->getAttribute(Attribute::PARENT_NODE); + $argParentNode = $arrayParentNode->getAttribute(Attribute::PARENT_NODE); /** @var MethodCall|Node $methodCallNode */ + $methodCallNode = $argParentNode->getAttribute(Attribute::PARENT_NODE); + if (! $methodCallNode instanceof MethodCall) { return false; } - return $methodCallNode->name->toString() === 'add'; + return (string) $methodCallNode->name === 'add'; } /** diff --git a/src/Rector/Dynamic/FluentReplaceRector.php b/src/Rector/Dynamic/FluentReplaceRector.php index 05600b55b5a..ce52307aa09 100644 --- a/src/Rector/Dynamic/FluentReplaceRector.php +++ b/src/Rector/Dynamic/FluentReplaceRector.php @@ -52,13 +52,13 @@ final class FluentReplaceRector extends AbstractRector { return $this; } - + public function otherFunction() { return $this; } } - + $someClass = new SomeClass(); $someClass->someFunction() ->otherFunction(); @@ -70,12 +70,12 @@ CODE_SAMPLE public function someFunction() { } - + public function otherFunction() { } } - + $someClass = new SomeClass(); $someClass->someFunction(); $someClass->otherFunction(); @@ -152,7 +152,7 @@ CODE_SAMPLE { $nextMethodCallNode = $this->methodCallNodeFactory->createWithVariableAndMethodName( $innerMethodCallNode->var, - $outerMethodCallNode->name->toString() + (string) $outerMethodCallNode->name ); $this->addNodeAfterNode($nextMethodCallNode, $innerMethodCallNode);