diff --git a/config/level/coding-style/coding-style.yaml b/config/level/coding-style/coding-style.yaml index e2bb1c88c68c..5c1b8ca97fd7 100644 --- a/config/level/coding-style/coding-style.yaml +++ b/config/level/coding-style/coding-style.yaml @@ -13,3 +13,4 @@ services: Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector: ~ Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector: ~ Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector: ~ + Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~ diff --git a/ecs.yaml b/ecs.yaml index a99d1dc5d784..5686f0378518 100644 --- a/ecs.yaml +++ b/ecs.yaml @@ -111,6 +111,8 @@ parameters: - 'packages/Laravel/src/Rector/FuncCall/HelperFunctionToConstructorInjectionRector.php' - 'packages/PhpSpecToPHPUnit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php' - 'packages/NetteTesterToPHPUnit/src/AssertManipulator.php' + # aliases + - 'packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php' # copied 3rd party logic - 'packages/Php/src/EregToPcreTransformer.php' diff --git a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php index fc6851a980d2..d4e0c4d6828f 100644 --- a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php +++ b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php @@ -3,6 +3,15 @@ namespace Rector\CodeQuality\Rector\Array_; use PhpParser\Node; +use PhpParser\Node\Arg; +use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Expr\ClosureUse; +use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Return_; use Rector\NodeContainer\ParsedNodesByType; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -75,7 +84,7 @@ private function compareSize($first, $second) */ public function getNodeTypes(): array { - return [Node\Expr\Array_::class]; + return [Array_::class]; } /** @@ -95,21 +104,21 @@ public function refactor(Node $node): ?Node return null; } - $anonymousFunction = new Node\Expr\Closure(); + $anonymousFunction = new Closure(); $anonymousFunction->params = $classMethod->params; - $innerMethodCall = new Node\Expr\MethodCall($objectVariable, $classMethod->name); + $innerMethodCall = new MethodCall($objectVariable, $classMethod->name); $innerMethodCall->args = $this->convertParamsToArgs($classMethod->params); if ($classMethod->returnType) { $anonymousFunction->returnType = $classMethod->returnType; } - $anonymousFunction->stmts[] = new Node\Stmt\Return_($innerMethodCall); + $anonymousFunction->stmts[] = new Return_($innerMethodCall); - if ($objectVariable instanceof Node\Expr\Variable) { + if ($objectVariable instanceof Variable) { if (! $this->isName($objectVariable, 'this')) { - $anonymousFunction->uses[] = new Node\Expr\ClosureUse($objectVariable); + $anonymousFunction->uses[] = new ClosureUse($objectVariable); } } @@ -124,13 +133,13 @@ private function convertParamsToArgs(array $params): array { $args = []; foreach ($params as $key => $param) { - $args[$key] = new Node\Arg($param->var); + $args[$key] = new Arg($param->var); } return $args; } - private function matchCallableMethod(Node\Expr $objectExpr, Node\Expr $methodExpr): ?Node\Stmt\ClassMethod + private function matchCallableMethod(Expr $objectExpr, Expr $methodExpr): ?ClassMethod { $methodName = $this->getValue($methodExpr); diff --git a/packages/CodeQuality/src/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php b/packages/CodeQuality/src/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php index 3ac37944bd34..69a4661639fc 100644 --- a/packages/CodeQuality/src/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php +++ b/packages/CodeQuality/src/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php @@ -4,6 +4,8 @@ use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Identical; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -58,7 +60,7 @@ public function run() */ public function getNodeTypes(): array { - return [Identical::class, Node\Expr\BooleanNot::class]; + return [Identical::class, BooleanNot::class]; } /** @@ -80,13 +82,13 @@ public function refactor(Node $node): ?Node return null; } - return new Node\Expr\BinaryOp\NotIdentical($identical->left, $identical->right); + return new NotIdentical($identical->left, $identical->right); } return null; } - private function processIdentical(Identical $identical): ?Node\Expr\BinaryOp\NotIdentical + private function processIdentical(Identical $identical): ?NotIdentical { if (! $this->isBoolType($identical->left)) { return null; @@ -96,8 +98,8 @@ private function processIdentical(Identical $identical): ?Node\Expr\BinaryOp\Not return null; } - if ($identical->left instanceof Node\Expr\BooleanNot) { - return new Node\Expr\BinaryOp\NotIdentical($identical->left->expr, $identical->right); + if ($identical->left instanceof BooleanNot) { + return new NotIdentical($identical->left->expr, $identical->right); } return null; diff --git a/packages/CodeQuality/src/Rector/Identical/SimplifyBoolIdenticalTrueRector.php b/packages/CodeQuality/src/Rector/Identical/SimplifyBoolIdenticalTrueRector.php index f077891eeb77..85ada89644e6 100644 --- a/packages/CodeQuality/src/Rector/Identical/SimplifyBoolIdenticalTrueRector.php +++ b/packages/CodeQuality/src/Rector/Identical/SimplifyBoolIdenticalTrueRector.php @@ -3,6 +3,7 @@ namespace Rector\CodeQuality\Rector\Identical; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\BooleanNot; @@ -65,7 +66,7 @@ public function refactor(Node $node): ?Node return null; } - private function processBoolTypeToNotBool(Node $node, Node\Expr $leftExpr, Node\Expr $rightExpr): ?Node\Expr + private function processBoolTypeToNotBool(Node $node, Expr $leftExpr, Expr $rightExpr): ?Expr { if ($node instanceof Identical) { if ($this->isTrue($rightExpr)) { diff --git a/packages/CodingStyle/src/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php b/packages/CodingStyle/src/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php index f2be0d63f6ee..c6b8fe20d6da 100644 --- a/packages/CodingStyle/src/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php +++ b/packages/CodingStyle/src/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php @@ -61,7 +61,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node instanceof Node\Stmt\ClassConst) { + if ($node instanceof ClassConst) { if (count($node->consts) < 2) { return null; } @@ -73,9 +73,7 @@ public function refactor(Node $node): ?Node $node->consts = [$firstConstant]; foreach ($allConstants as $anotherConstant) { - $nextClassConst = new Node\Stmt\ClassConst([ - $anotherConstant, - ], $node->flags, $node->getAttributes()); + $nextClassConst = new ClassConst([$anotherConstant], $node->flags, $node->getAttributes()); $this->addNodeAfterNode($nextClassConst, $node); } @@ -92,7 +90,7 @@ public function refactor(Node $node): ?Node $node->props = [$firstProperty]; foreach ($allProperties as $anotherProperty) { - $nextProperty = new Node\Stmt\Property($node->flags, [$anotherProperty], $node->getAttributes()); + $nextProperty = new Property($node->flags, [$anotherProperty], $node->getAttributes()); $this->addNodeAfterNode($nextProperty, $node); } diff --git a/packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php b/packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php new file mode 100644 index 000000000000..ec92bb7207e9 --- /dev/null +++ b/packages/CodingStyle/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php @@ -0,0 +1,227 @@ +callableNodeTraverser = $callableNodeTraverser; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Import fully qualified names to use statements', [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function create() + { + return SomeAnother\AnotherClass; + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use SomeAnother\AnotherClass; + +class SomeClass +{ + public function create() + { + return AnotherClass; + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Namespace_::class]; + } + + /** + * @param Node\Stmt\Namespace_ $node + */ + public function refactor(Node $node): ?Node + { + $this->alreadyImportedUses = []; + $this->newUseStatements = []; + + /** @var Node\Stmt\Class_|null $class */ + $class = $this->betterNodeFinder->findFirstInstanceOf($node, Class_::class); + if ($class === null) { + return null; + } + + $this->resolveAlreadyImportedUses($node); + $newUseStatements = $this->importNamesAndCollectNewUseStatements($class); + + $this->addNewUseStatements($node, $newUseStatements); + + return $node; + } + + private function resolveAlreadyImportedUses(Namespace_ $namespace): void + { + /** @var Node\Stmt\Use_[] $uses */ + $uses = $this->betterNodeFinder->find($namespace->stmts, function (Node $node) { + if (! $node instanceof Use_) { + return false; + } + + // only import uses + return $node->type === Use_::TYPE_NORMAL; + }); + + foreach ($uses as $use) { + foreach ($use->uses as $useUse) { + $name = $this->getName($useUse); + if ($name === null) { + throw new ShouldNotHappenException(); + } + + if ($useUse->alias) { + // alias workaround + $this->aliasedUses[] = $name; + } + + $this->alreadyImportedUses[] = $name; + } + } + } + + /** + * @param string[] $newUseStatements + */ + private function addNewUseStatements(Namespace_ $namespace, array $newUseStatements): void + { + if ($newUseStatements === []) { + return; + } + + $newUses = []; + + foreach ($newUseStatements as $newUseStatement) { + // already imported in previous cycle + if (in_array($newUseStatement, $this->alreadyImportedUses, true)) { + continue; + } + + $useUse = new UseUse(new Name($newUseStatement)); + $newUses[] = new Use_([$useUse]); + + $this->alreadyImportedUses[] = $newUseStatement; + } + + $namespace->stmts = array_merge($newUses, $namespace->stmts); + } + + /** + * @return string[] + */ + private function importNamesAndCollectNewUseStatements(Class_ $class): array + { + $this->newUseStatements = []; + + $this->callableNodeTraverser->traverseNodesWithCallable([$class], function (Node $node) { + if (! $node instanceof Name) { + return null; + } + + $name = $node->getAttribute('originalName'); + if ($name instanceof Name) { + // already short + if (! Strings::contains($name->toString(), '\\')) { + return null; + } + } + + // 1. name is fully qualified → import it + if ($this->getName($node) === $node->toString()) { + $fullyQualifiedName = $this->getName($node); + if (! Strings::contains($fullyQualifiedName, '\\')) { + return null; + } + + $shortName = $this->getShortName($fullyQualifiedName); + + // nothing to change + if ($shortName === $fullyQualifiedName) { + return null; + } + + // the similar end is already imported → skip + foreach ($this->alreadyImportedUses as $alreadyImportedUse) { + if ($this->getShortName( + $alreadyImportedUse + ) === $shortName && $alreadyImportedUse !== $fullyQualifiedName) { + return null; + } + } + + if (! in_array($fullyQualifiedName, $this->alreadyImportedUses, true)) { + $this->newUseStatements[] = $fullyQualifiedName; + } + + // @todo detect aliases somehow, look like overrided by name resolver node traverser + // possibly aliased + if (in_array($fullyQualifiedName, $this->aliasedUses, true)) { + return null; + } + + return new Name($shortName); + } + }); + + return $this->newUseStatements; + } + + private function getShortName(string $fullyQualifiedName): string + { + if (! Strings::contains($fullyQualifiedName, '\\')) { + return $fullyQualifiedName; + } + + return Strings::after($fullyQualifiedName, '\\', -1); + } +} diff --git a/packages/CodingStyle/src/Rector/String_/SplitStringClassConstantToClassConstFetchRector.php b/packages/CodingStyle/src/Rector/String_/SplitStringClassConstantToClassConstFetchRector.php index 3be880ba5d42..88807407f36b 100644 --- a/packages/CodingStyle/src/Rector/String_/SplitStringClassConstantToClassConstFetchRector.php +++ b/packages/CodingStyle/src/Rector/String_/SplitStringClassConstantToClassConstFetchRector.php @@ -4,6 +4,9 @@ use Nette\Utils\Strings; use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp\Concat; +use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -72,8 +75,8 @@ public function refactor(Node $node): ?Node return null; } - $classConstFetch = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified($possibleClass), 'class'); + $classConstFetch = new ClassConstFetch(new FullyQualified($possibleClass), 'class'); - return new Node\Expr\BinaryOp\Concat($classConstFetch, new Node\Scalar\String_('::' . $secondPart)); + return new Concat($classConstFetch, new String_('::' . $secondPart)); } } diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/already_with_use.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/already_with_use.php.inc new file mode 100644 index 000000000000..73f9bc04d2e3 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/already_with_use.php.inc @@ -0,0 +1,32 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import.php.inc new file mode 100644 index 000000000000..4bd6e331adcc --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import.php.inc @@ -0,0 +1,53 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import_with_existing.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import_with_existing.php.inc new file mode 100644 index 000000000000..bfda21d546ed --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/double_import_with_existing.php.inc @@ -0,0 +1,54 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/fixture.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..93a46ac62844 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/fixture.php.inc @@ -0,0 +1,28 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep.php.inc new file mode 100644 index 000000000000..a976883fc0f8 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep.php.inc @@ -0,0 +1,18 @@ +phpStanScopeFactory = $phpStanScopeFactory; + } +} diff --git a/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep_same_end.php.inc b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep_same_end.php.inc new file mode 100644 index 000000000000..d2ccfe690780 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/keep_same_end.php.inc @@ -0,0 +1,12 @@ +doTestFiles([ + __DIR__ . '/Fixture/fixture.php.inc', + __DIR__ . '/Fixture/double_import.php.inc', + __DIR__ . '/Fixture/double_import_with_existing.php.inc', + __DIR__ . '/Fixture/already_with_use.php.inc', + // keep + __DIR__ . '/Fixture/keep.php.inc', + __DIR__ . '/Fixture/keep_same_end.php.inc', + __DIR__ . '/Fixture/keep_trait_use.php.inc', + ]); + } + + protected function getRectorClass(): string + { + return ImportFullyQualifiedNamesRector::class; + } +} diff --git a/packages/ContributorTools/src/Command/CreateRectorCommand.php b/packages/ContributorTools/src/Command/CreateRectorCommand.php index 8b8ff879f129..65d2a955ae5a 100644 --- a/packages/ContributorTools/src/Command/CreateRectorCommand.php +++ b/packages/ContributorTools/src/Command/CreateRectorCommand.php @@ -91,7 +91,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($this->findTemplateFileInfos() as $smartFileInfo) { $destination = $this->resolveDestination($smartFileInfo, $templateVariables, $configuration); - // @todo for core package $content = $this->resolveContent($smartFileInfo, $templateVariables); if ($configuration->getPackage() === 'Rector') { diff --git a/packages/ContributorTools/src/Configuration/ConfigurationFactory.php b/packages/ContributorTools/src/Configuration/ConfigurationFactory.php index 9e93cc646a4a..a109c777803b 100644 --- a/packages/ContributorTools/src/Configuration/ConfigurationFactory.php +++ b/packages/ContributorTools/src/Configuration/ConfigurationFactory.php @@ -160,6 +160,11 @@ private function resolveLevelConfig(string $level): ?string private function isNodeClassMatch(string $nodeClass, string $nodeType): bool { + // skip "magic", they're used less than rarely + if (Strings::contains($nodeClass, 'MagicConst')) { + return false; + } + if (Strings::endsWith($nodeClass, '\\' . $nodeType)) { return true; } diff --git a/packages/DeadCode/src/Rector/Assign/RemoveDoubleAssignRector.php b/packages/DeadCode/src/Rector/Assign/RemoveDoubleAssignRector.php index 19c1e7c6acaa..d4a154e26d15 100644 --- a/packages/DeadCode/src/Rector/Assign/RemoveDoubleAssignRector.php +++ b/packages/DeadCode/src/Rector/Assign/RemoveDoubleAssignRector.php @@ -9,6 +9,12 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt\Do_; +use PhpParser\Node\Stmt\Else_; +use PhpParser\Node\Stmt\ElseIf_; +use PhpParser\Node\Stmt\Foreach_; +use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\While_; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -92,15 +98,15 @@ public function refactor(Node $node): ?Node private function shouldSkipDueToForeachOverride(Assign $assign, Node $node): bool { // is nested in a foreach and the previous expression is not? - $nodePreviousForeach = $this->betterNodeFinder->findFirstParentInstanceOf($assign, Node\Stmt\Foreach_::class); + $nodePreviousForeach = $this->betterNodeFinder->findFirstParentInstanceOf($assign, Foreach_::class); $previousExpressionPreviousForeach = $this->betterNodeFinder->findFirstParentInstanceOf( $node, - Node\Stmt\Foreach_::class + Foreach_::class ); if ($nodePreviousForeach !== $previousExpressionPreviousForeach) { - if ($nodePreviousForeach instanceof Node\Stmt\Foreach_ && $assign->var instanceof Variable) { + if ($nodePreviousForeach instanceof Foreach_ && $assign->var instanceof Variable) { // is value changed inside the foreach? $variableAssigns = $this->betterNodeFinder->findAssignsOfVariable($nodePreviousForeach, $assign->var); @@ -117,26 +123,12 @@ private function shouldSkipForDifferenceParent(Node $firstNode, Node $secondNode { $firstNodeParent = $this->betterNodeFinder->findFirstParentInstanceOf( $firstNode, - [ - Node\Stmt\Foreach_::class, - Node\Stmt\If_::class, - Node\Stmt\While_::class, - Node\Stmt\Do_::class, - Node\Stmt\Else_::class, - Node\Stmt\ElseIf_::class, - ] + [Foreach_::class, If_::class, While_::class, Do_::class, Else_::class, ElseIf_::class] ); $secondNodeParent = $this->betterNodeFinder->findFirstParentInstanceOf( $secondNode, - [ - Node\Stmt\Foreach_::class, - Node\Stmt\If_::class, - Node\Stmt\While_::class, - Node\Stmt\Do_::class, - Node\Stmt\If_::class, - Node\Stmt\ElseIf_::class, - ] + [Foreach_::class, If_::class, While_::class, Do_::class, If_::class, ElseIf_::class] ); if ($firstNodeParent === null || $secondNodeParent === null) { diff --git a/packages/Nette/src/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector.php b/packages/Nette/src/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector.php index ef8a7e5d0b7a..7ed07a3c3405 100644 --- a/packages/Nette/src/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector.php +++ b/packages/Nette/src/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector.php @@ -3,7 +3,11 @@ namespace Rector\Nette\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; +use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Name; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -75,10 +79,10 @@ public function refactor(Node $node): ?Node $matchStaticCall = $this->createMatchStaticCall($node, $methodName); // skip assigns, might be used with differnt return value - if ($node->getAttribute(AttributeKey::PARENT_NODE) instanceof Node\Expr\Assign) { + if ($node->getAttribute(AttributeKey::PARENT_NODE) instanceof Assign) { if ($methodName === 'matchAll') { // use count - return new FuncCall(new Node\Name('count'), [new Node\Arg($matchStaticCall)]); + return new FuncCall(new Name('count'), [new Arg($matchStaticCall)]); } if ($methodName === 'split') { @@ -94,13 +98,13 @@ public function refactor(Node $node): ?Node // assign if (isset($node->args[2])) { - return new Node\Expr\Assign($node->args[2]->value, $matchStaticCall); + return new Assign($node->args[2]->value, $matchStaticCall); } return $matchStaticCall; } - private function createMatchStaticCall(FuncCall $funcCall, string $methodName): Node\Expr\StaticCall + private function createMatchStaticCall(FuncCall $funcCall, string $methodName): StaticCall { $args = []; @@ -116,7 +120,7 @@ private function createMatchStaticCall(FuncCall $funcCall, string $methodName): return $this->createStaticCall('Nette\Utils\Strings', $methodName, $args); } - private function processSplit(FuncCall $funcCall, Node\Expr\StaticCall $matchStaticCall): Node + private function processSplit(FuncCall $funcCall, StaticCall $matchStaticCall): Node { if (isset($funcCall->args[2])) { if ($this->isValue($funcCall->args[2]->value, -1)) { diff --git a/packages/Nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php b/packages/Nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php index 080000eaa06b..58335566b8fd 100644 --- a/packages/Nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php +++ b/packages/Nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php @@ -3,8 +3,13 @@ namespace Rector\Nette\Rector\Identical; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\UnaryMinus; +use PhpParser\Node\Expr\Variable; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -62,11 +67,11 @@ public function refactor(Node $node): ?Node { $contentAndNeedle = null; - if ($node->left instanceof Node\Expr\Variable) { + if ($node->left instanceof Variable) { $contentAndNeedle = $this->matchContentAndNeedleOfSubstrOfVariableLength($node->right, $node->left); } - if ($node->right instanceof Node\Expr\Variable) { + if ($node->right instanceof Variable) { $contentAndNeedle = $this->matchContentAndNeedleOfSubstrOfVariableLength($node->left, $node->right); } @@ -78,12 +83,12 @@ public function refactor(Node $node): ?Node // starts with $startsWithStaticCall = $this->createStaticCall('Nette\Utils\Strings', 'endsWith', [ - new Node\Arg($contentNode), - new Node\Arg($needleNode), + new Arg($contentNode), + new Arg($needleNode), ]); - if ($node instanceof Node\Expr\BinaryOp\NotIdentical) { - return new Node\Expr\BooleanNot($startsWithStaticCall); + if ($node instanceof NotIdentical) { + return new BooleanNot($startsWithStaticCall); } return $startsWithStaticCall; @@ -92,9 +97,9 @@ public function refactor(Node $node): ?Node /** * @return Node\Expr[]|null */ - private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Node\Expr\Variable $variable): ?array + private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Variable $variable): ?array { - if (! $node instanceof Node\Expr\FuncCall) { + if (! $node instanceof FuncCall) { return null; } @@ -102,14 +107,14 @@ private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Node\ return null; } - if (! $node->args[1]->value instanceof Node\Expr\UnaryMinus) { + if (! $node->args[1]->value instanceof UnaryMinus) { return null; } /** @var Node\Expr\UnaryMinus $unaryMinus */ $unaryMinus = $node->args[1]->value; - if (! $unaryMinus->expr instanceof Node\Expr\FuncCall) { + if (! $unaryMinus->expr instanceof FuncCall) { return null; } diff --git a/packages/Nette/src/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector.php b/packages/Nette/src/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector.php index f1fe5a8190bb..c17713690d70 100644 --- a/packages/Nette/src/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector.php +++ b/packages/Nette/src/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector.php @@ -3,8 +3,12 @@ namespace Rector\Nette\Rector\Identical; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\Variable; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -62,11 +66,11 @@ public function refactor(Node $node): ?Node { $contentAndNeedle = null; - if ($node->left instanceof Node\Expr\Variable) { + if ($node->left instanceof Variable) { $contentAndNeedle = $this->matchContentAndNeedleOfSubstrOfVariableLength($node->right, $node->left); } - if ($node->right instanceof Node\Expr\Variable) { + if ($node->right instanceof Variable) { $contentAndNeedle = $this->matchContentAndNeedleOfSubstrOfVariableLength($node->left, $node->right); } @@ -78,12 +82,12 @@ public function refactor(Node $node): ?Node // starts with $startsWithStaticCall = $this->createStaticCall('Nette\Utils\Strings', 'startsWith', [ - new Node\Arg($contentNode), - new Node\Arg($needleNode), + new Arg($contentNode), + new Arg($needleNode), ]); - if ($node instanceof Node\Expr\BinaryOp\NotIdentical) { - return new Node\Expr\BooleanNot($startsWithStaticCall); + if ($node instanceof NotIdentical) { + return new BooleanNot($startsWithStaticCall); } return $startsWithStaticCall; @@ -92,9 +96,9 @@ public function refactor(Node $node): ?Node /** * @return Node\Expr[]|null */ - private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Node\Expr\Variable $variable): ?array + private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Variable $variable): ?array { - if (! $node instanceof Node\Expr\FuncCall) { + if (! $node instanceof FuncCall) { return null; } @@ -109,7 +113,7 @@ private function matchContentAndNeedleOfSubstrOfVariableLength(Node $node, Node\ return null; } - if (! $node->args[2]->value instanceof Node\Expr\FuncCall) { + if (! $node->args[2]->value instanceof FuncCall) { return null; } diff --git a/packages/Nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php b/packages/Nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php index 455acc2d0d81..1035f4454088 100644 --- a/packages/Nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php +++ b/packages/Nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php @@ -3,6 +3,13 @@ namespace Rector\Nette\Rector\NotIdentical; use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp; +use PhpParser\Node\Expr\BinaryOp\Identical; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Name\FullyQualified; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -47,7 +54,7 @@ public function run() */ public function getNodeTypes(): array { - return [Node\Expr\BinaryOp\NotIdentical::class, Node\Expr\BinaryOp\Identical::class]; + return [NotIdentical::class, Identical::class]; } /** @@ -64,21 +71,21 @@ public function refactor(Node $node): ?Node return null; } - $containsStaticCall = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Nette\Utils\Strings'), 'contains'); + $containsStaticCall = new StaticCall(new FullyQualified('Nette\Utils\Strings'), 'contains'); $containsStaticCall->args[0] = $strpos->args[0]; $containsStaticCall->args[1] = $strpos->args[1]; - if ($node instanceof Node\Expr\BinaryOp\Identical) { - return new Node\Expr\BooleanNot($containsStaticCall); + if ($node instanceof Identical) { + return new BooleanNot($containsStaticCall); } return $containsStaticCall; } - private function matchStrposInComparisonToFalse(Node\Expr\BinaryOp $binaryOp): ?Node\Expr\FuncCall + private function matchStrposInComparisonToFalse(BinaryOp $binaryOp): ?FuncCall { if ($this->isFalse($binaryOp->left)) { - if (! $binaryOp->right instanceof Node\Expr\FuncCall) { + if (! $binaryOp->right instanceof FuncCall) { return null; } @@ -88,7 +95,7 @@ private function matchStrposInComparisonToFalse(Node\Expr\BinaryOp $binaryOp): ? } if ($this->isFalse($binaryOp->right)) { - if (! $binaryOp->left instanceof Node\Expr\FuncCall) { + if (! $binaryOp->left instanceof FuncCall) { return null; } diff --git a/packages/PHPUnit/src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php b/packages/PHPUnit/src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php index f29967a7e059..7e8638a95b9a 100644 --- a/packages/PHPUnit/src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php +++ b/packages/PHPUnit/src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php @@ -77,7 +77,7 @@ public function refactor(Node $node): ?Node $onlyArgument = $node->args[0]->value; // @todo add match sequence method, ref https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/be664e0c9f3cca94d0bbefa06a731848144e4a22/src/Tokenizer/Tokens.php#L776 - if (! $onlyArgument instanceof Node\Expr\MethodCall) { + if (! $onlyArgument instanceof MethodCall) { return null; } diff --git a/packages/Php/src/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php b/packages/Php/src/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php index 1b3148c54c4e..3595844dad7a 100644 --- a/packages/Php/src/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php +++ b/packages/Php/src/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php @@ -4,6 +4,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Break_; +use PhpParser\Node\Stmt\Return_; use Rector\Context\ContextAnalyzer; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -83,7 +84,7 @@ public function refactor(Node $node): ?Node } if ($this->contextAnalyzer->isInIf($node)) { - return new Node\Stmt\Return_(); + return new Return_(); } $this->removeNode($node); diff --git a/packages/SOLID/src/Rector/Class_/FinalizeClassesWithoutChildrenRector.php b/packages/SOLID/src/Rector/Class_/FinalizeClassesWithoutChildrenRector.php index 35fbe8e7c798..70c057d62fd0 100644 --- a/packages/SOLID/src/Rector/Class_/FinalizeClassesWithoutChildrenRector.php +++ b/packages/SOLID/src/Rector/Class_/FinalizeClassesWithoutChildrenRector.php @@ -4,6 +4,7 @@ use Nette\Utils\Strings; use PhpParser\Node; +use PhpParser\Node\Stmt\Class_; use Rector\NodeContainer\ParsedNodesByType; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -61,7 +62,7 @@ final class ThirdClass extends SecondClass */ public function getNodeTypes(): array { - return [Node\Stmt\Class_::class]; + return [Class_::class]; } /** @@ -83,7 +84,7 @@ public function refactor(Node $node): ?Node return null; } - $node->flags |= Node\Stmt\Class_::MODIFIER_FINAL; + $node->flags |= Class_::MODIFIER_FINAL; return $node; } diff --git a/packages/Symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php b/packages/Symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php index 85ced7dbc6dd..985b08ea42a4 100644 --- a/packages/Symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php +++ b/packages/Symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php @@ -3,7 +3,13 @@ namespace Rector\Symfony\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\PropertyFetch; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Stmt\Expression; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -29,7 +35,7 @@ public function __construct(string $webTestCaseClass = 'Symfony\Bundle\Framework { $this->webTestCaseClass = $webTestCaseClass; - $clientGetResponse = new MethodCall(new Node\Expr\Variable('client'), 'getResponse'); + $clientGetResponse = new MethodCall(new Variable('client'), 'getResponse'); $this->getStatusCodeMethodCall = new MethodCall($clientGetResponse, 'getStatusCode'); } @@ -104,11 +110,11 @@ public function refactor(Node $node): ?Node // assertResponseIsSuccessful $args = []; - $args[] = new Node\Arg(new Node\Scalar\LNumber(200)); - $args[] = new Node\Arg($this->getStatusCodeMethodCall); - $match = new MethodCall(new Node\Expr\Variable('this'), 'assertSame', $args); + $args[] = new Arg(new LNumber(200)); + $args[] = new Arg($this->getStatusCodeMethodCall); + $match = new MethodCall(new Variable('this'), 'assertSame', $args); if ($this->areNodesEqual($node, $match)) { - return new MethodCall(new Node\Expr\Variable('this'), 'assertResponseIsSuccessful'); + return new MethodCall(new Variable('this'), 'assertResponseIsSuccessful'); } // assertResponseStatusCodeSame @@ -120,7 +126,7 @@ public function refactor(Node $node): ?Node // assertSelectorTextContains $args = $this->matchAssertContainsCrawlerArg($node); if ($args !== null) { - return new MethodCall(new Node\Expr\Variable('this'), 'assertSelectorTextContains', $args); + return new MethodCall(new Variable('this'), 'assertSelectorTextContains', $args); } // 3. assertResponseRedirects @@ -155,7 +161,7 @@ private function matchAssertContainsCrawlerArg(MethodCall $methodCall): ?array return null; } - if (! $comparedNode->var->var instanceof Node\Expr\Variable) { + if (! $comparedNode->var->var instanceof Variable) { return null; } @@ -178,7 +184,7 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node { /** @var Node\Stmt\Expression|null $previousNode */ $previousExpression = $methodCall->getAttribute(AttributeKey::PREVIOUS_EXPRESSION); - if (! $previousExpression instanceof Node\Stmt\Expression) { + if (! $previousExpression instanceof Expression) { return null; } @@ -188,16 +194,16 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node } $args = []; - $args[] = new Node\Arg(new Node\Scalar\LNumber(301)); - $args[] = new Node\Arg($this->getStatusCodeMethodCall); + $args[] = new Arg(new LNumber(301)); + $args[] = new Arg($this->getStatusCodeMethodCall); - $match = new MethodCall(new Node\Expr\Variable('this'), 'assertSame', $args); + $match = new MethodCall(new Variable('this'), 'assertSame', $args); if ($this->areNodesEqual($previousNode, $match)) { - $clientGetLocation = new MethodCall(new Node\Expr\PropertyFetch(new MethodCall( - new Node\Expr\Variable('client'), + $clientGetLocation = new MethodCall(new PropertyFetch(new MethodCall( + new Variable('client'), 'getResponse' - ), 'headers'), 'get', [new Node\Arg(new Node\Scalar\String_('Location'))]); + ), 'headers'), 'get', [new Arg(new String_('Location'))]); if (! isset($methodCall->args[1])) { return null; @@ -210,7 +216,7 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node $this->removeNode($previousNode); - return new MethodCall(new Node\Expr\Variable('this'), 'assertResponseRedirects', $args); + return new MethodCall(new Variable('this'), 'assertResponseRedirects', $args); } } @@ -238,6 +244,6 @@ private function processAssertResponseStatusCodeSame(Node $node): ?MethodCall return null; } - return new MethodCall(new Node\Expr\Variable('this'), 'assertResponseStatusCodeSame', [$node->args[0]]); + return new MethodCall(new Variable('this'), 'assertResponseStatusCodeSame', [$node->args[0]]); } } diff --git a/phpstan.neon b/phpstan.neon index 8d46c1be2411..5f24d50aa860 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -151,3 +151,4 @@ parameters: - '#Method Rector\\NodeContainer\\ParsedNodesByType\:\:(.*?)\(\) should return PhpParser\\Node\\Stmt\\(.*?)\|null but returns PhpParser\\Node\|null#' - '#Method Rector\\NodeContainer\\ParsedNodesByType\:\:findImplementersOfInterface\(\) should return array but returns array#' - '#PHPDoc tag @param for parameter \$classLike with type PhpParser\\Builder\\Trait_\|PhpParser\\Node\\Stmt\\Interface_ is not subtype of native type PhpParser\\Node\\Stmt\\ClassLike#' + - '#Method Rector\\CodingStyle\\Rector\\Namespace_\\ImportFullyQualifiedNamesRector\:\:getShortName\(\) should return string but returns string\|false#' diff --git a/rector.yaml b/rector.yaml index b9fcab42d1eb..732eb416d8b4 100644 --- a/rector.yaml +++ b/rector.yaml @@ -7,3 +7,6 @@ parameters: # so Rector code is still PHP 7.1 compatible php_version_features: '7.1' + +services: + Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~ diff --git a/src/Context/ContextAnalyzer.php b/src/Context/ContextAnalyzer.php index 8e70bc10738c..a8180a4c3aff 100644 --- a/src/Context/ContextAnalyzer.php +++ b/src/Context/ContextAnalyzer.php @@ -4,10 +4,13 @@ use PhpParser\Node; use PhpParser\Node\FunctionLike; +use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; +use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\While_; use Rector\PhpParser\Node\BetterNodeFinder; @@ -22,7 +25,7 @@ final class ContextAnalyzer /** * @var string[] */ - private const LOOP_NODES = [For_::class, Foreach_::class, While_::class, Do_::class, Node\Stmt\Switch_::class]; + private const LOOP_NODES = [For_::class, Foreach_::class, While_::class, Do_::class, Switch_::class]; /** * @var BetterNodeFinder @@ -48,18 +51,18 @@ public function isInLoop(Node $node): bool return $this->isTypes($firstParent, self::LOOP_NODES); } - public function isInIf(Node\Stmt\Break_ $node): bool + public function isInIf(Break_ $node): bool { $previousNode = $this->betterNodeFinder->findFirstParentInstanceOf( $node, - array_merge([Node\Stmt\If_::class], self::BREAK_NODES) + array_merge([If_::class], self::BREAK_NODES) ); if ($previousNode === null) { return false; } - return $this->isTypes($previousNode, [Node\Stmt\If_::class]); + return $this->isTypes($previousNode, [If_::class]); } /** diff --git a/src/NodeContainer/ParsedNodesByType.php b/src/NodeContainer/ParsedNodesByType.php index 358d4e14bba3..0a3f9c1e0f5c 100644 --- a/src/NodeContainer/ParsedNodesByType.php +++ b/src/NodeContainer/ParsedNodesByType.php @@ -5,6 +5,8 @@ use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassLike; @@ -36,8 +38,8 @@ final class ParsedNodesByType ClassMethod::class, Function_::class, // simply collected - Node\Expr\New_::class, - Node\Expr\StaticCall::class, + New_::class, + StaticCall::class, ]; /** @@ -101,7 +103,7 @@ public function getClasses(): array */ public function getNewNodes(): array { - return $this->simpleParsedNodesByType[Node\Expr\New_::class] ?? []; + return $this->simpleParsedNodesByType[New_::class] ?? []; } /** @@ -109,7 +111,7 @@ public function getNewNodes(): array */ public function getStaticCallNodes(): array { - return $this->simpleParsedNodesByType[Node\Expr\StaticCall::class] ?? []; + return $this->simpleParsedNodesByType[StaticCall::class] ?? []; } /** diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 292f42a56062..914df20b5c45 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -3,6 +3,9 @@ namespace Rector\PhpParser\Node; use PhpParser\Node; +use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\NodeFinder; @@ -142,16 +145,16 @@ public function findFirstPrevious(Node $node, callable $filter): ?Node /** * @return Node\Expr\Assign[] */ - public function findAssignsOfVariable(Node $node, Node\Expr\Variable $variable): array + public function findAssignsOfVariable(Node $node, Variable $variable): array { - $assignNodes = $this->findInstanceOf($node, Node\Expr\Assign::class); + $assignNodes = $this->findInstanceOf($node, Assign::class); - return array_filter($assignNodes, function (Node\Expr\Assign $assign) use ($variable) { + return array_filter($assignNodes, function (Assign $assign) use ($variable) { if ($this->betterStandardPrinter->areNodesEqual($assign->var, $variable)) { return true; } - if ($assign->var instanceof Node\Expr\ArrayDimFetch) { + if ($assign->var instanceof ArrayDimFetch) { return $this->betterStandardPrinter->areNodesEqual($assign->var->var, $variable); } diff --git a/src/PhpParser/Node/Commander/NodeAddingCommander.php b/src/PhpParser/Node/Commander/NodeAddingCommander.php index a1a32419ffc4..42eeebbc8af1 100644 --- a/src/PhpParser/Node/Commander/NodeAddingCommander.php +++ b/src/PhpParser/Node/Commander/NodeAddingCommander.php @@ -5,6 +5,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Expression; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor; @@ -79,7 +80,7 @@ private function resolveNearestExpressionPosition(Node $node): string $foundNode = $this->betterNodeFinder->findFirstAncestorInstanceOf($node, Expression::class); if ($foundNode === null) { $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Stmt\ClassLike) { + if ($parentNode instanceof ClassLike) { $foundNode = $node; } else { throw new ShouldNotHappenException(); diff --git a/src/PhpParser/Node/Manipulator/ClassManipulator.php b/src/PhpParser/Node/Manipulator/ClassManipulator.php index fa28bab056b0..119b7e0a4536 100644 --- a/src/PhpParser/Node/Manipulator/ClassManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassManipulator.php @@ -4,6 +4,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Name; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Class_; @@ -240,7 +241,7 @@ public function getMethods(Class_ $class): array }); } - public function hasPropertyFetchAsProperty(Class_ $class, Node\Expr\PropertyFetch $propertyFetch): bool + public function hasPropertyFetchAsProperty(Class_ $class, PropertyFetch $propertyFetch): bool { if (! $this->nameResolver->isName($propertyFetch->var, 'this')) { return false; diff --git a/src/PhpParser/Node/Resolver/NameResolver.php b/src/PhpParser/Node/Resolver/NameResolver.php index f3c63fa6981e..d59cf6ee03e8 100644 --- a/src/PhpParser/Node/Resolver/NameResolver.php +++ b/src/PhpParser/Node/Resolver/NameResolver.php @@ -15,6 +15,7 @@ use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; +use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Use_; @@ -197,7 +198,7 @@ public function areNamesEqual(Node $firstNode, Node $secondNode): bool /** * @param Interface_|Trait_ $classLike */ - private function resolveNamespacedNameAwareNode(Node\Stmt\ClassLike $classLike): ?string + private function resolveNamespacedNameAwareNode(ClassLike $classLike): ?string { if (isset($classLike->namespacedName)) { return $classLike->namespacedName->toString();