Skip to content

Commit

Permalink
Updated Rector to commit ea0c6c15d9a937b1aebd4991815c7a0bab7f30c0
Browse files Browse the repository at this point in the history
rectorphp/rector-src@ea0c6c1 [Core] Refactor AstResolver to locate node with SimpleCallableNodeTraverser (#3494)
  • Loading branch information
TomasVotruba committed Mar 20, 2023
1 parent 9186224 commit b17ced8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '172d354a23c7b437f255709c7b85dcf667f41adb';
public const PACKAGE_VERSION = 'ea0c6c15d9a937b1aebd4991815c7a0bab7f30c0';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-03-20 17:22:54';
public const RELEASE_DATE = '2023-03-20 18:25:00';
/**
* @var int
*/
Expand Down
158 changes: 93 additions & 65 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\PhpParser\SmartPhpParser;
/**
* The nodes provided by this resolver is for read-only analysis only!
Expand All @@ -60,9 +60,9 @@ final class AstResolver
private $nodeScopeAndMetadataDecorator;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $betterNodeFinder;
private $simpleCallableNodeTraverser;
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
Expand All @@ -88,11 +88,11 @@ final class AstResolver
* @var \Rector\Core\PhpParser\ClassLikeAstResolver
*/
private $classLikeAstResolver;
public function __construct(SmartPhpParser $smartPhpParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, BetterNodeFinder $betterNodeFinder, NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver, NodeTypeResolver $nodeTypeResolver, \Rector\Core\PhpParser\ClassLikeAstResolver $classLikeAstResolver)
public function __construct(SmartPhpParser $smartPhpParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver, NodeTypeResolver $nodeTypeResolver, \Rector\Core\PhpParser\ClassLikeAstResolver $classLikeAstResolver)
{
$this->smartPhpParser = $smartPhpParser;
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->betterNodeFinder = $betterNodeFinder;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->nodeNameResolver = $nodeNameResolver;
$this->reflectionProvider = $reflectionProvider;
$this->reflectionResolver = $reflectionResolver;
Expand Down Expand Up @@ -124,20 +124,22 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR
if ($nodes === null) {
return null;
}
/** @var ClassMethod|null $classMethod */
$classMethod = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($classLikeName, $methodName) : bool {
if (!$node instanceof ClassMethod) {
return \false;
$classMethod = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($classLikeName, $methodName, &$classMethod) : ?int {
if (!$node instanceof ClassLike) {
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof ClassLike) {
return \false;
if (!$this->nodeNameResolver->isName($node, $classLikeName)) {
return null;
}
if (!$this->nodeNameResolver->isName($parentNode, $classLikeName)) {
return \false;
$method = $node->getMethod($methodName);
if ($method instanceof ClassMethod) {
$classMethod = $method;
return NodeTraverser::STOP_TRAVERSAL;
}
return $parentNode->getMethod($methodName) === $node;
return null;
});
/** @var ClassMethod|null $classMethod */
return $classMethod;
}
/**
Expand All @@ -162,11 +164,19 @@ public function resolveFunctionFromFunctionReflection(FunctionReflection $functi
if ($nodes === null) {
return null;
}
/** @var Function_|null $function */
$function = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($functionName) : bool {
return $node instanceof Function_ && $this->nodeNameResolver->isName($node, $functionName);
$functionNode = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($functionName, &$functionNode) : ?int {
if (!$node instanceof Function_) {
return null;
}
if (!$this->nodeNameResolver->isName($node, $functionName)) {
return null;
}
$functionNode = $node;
return NodeTraverser::STOP_TRAVERSAL;
});
return $function;
/** @var Function_|null $functionNode */
return $functionNode;
}
/**
* @param class-string $className
Expand All @@ -188,11 +198,7 @@ public function resolveClassMethod(string $className, string $methodName) : ?Cla
*/
public function resolveClassMethodFromCall($call) : ?ClassMethod
{
if ($call instanceof MethodCall) {
$callerStaticType = $this->nodeTypeResolver->getType($call->var);
} else {
$callerStaticType = $this->nodeTypeResolver->getType($call->class);
}
$callerStaticType = $call instanceof MethodCall ? $this->nodeTypeResolver->getType($call->var) : $this->nodeTypeResolver->getType($call->class);
if (!$callerStaticType instanceof TypeWithClassName) {
return null;
}
Expand Down Expand Up @@ -226,14 +232,22 @@ public function parseClassReflectionTraits(ClassReflection $classReflection) : a
if ($nodes === null) {
continue;
}
/** @var Trait_|null $trait */
$trait = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($classLike) : bool {
return $node instanceof Trait_ && $this->nodeNameResolver->isName($node, $classLike->getName());
$traitName = $classLike->getName();
$traitNode = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($traitName, &$traitNode) : ?int {
if (!$node instanceof Trait_) {
return null;
}
if (!$this->nodeNameResolver->isName($node, $traitName)) {
return null;
}
$traitNode = $node;
return NodeTraverser::STOP_TRAVERSAL;
});
if (!$trait instanceof Trait_) {
if (!$traitNode instanceof Trait_) {
continue;
}
$traits[] = $trait;
$traits[] = $traitNode;
}
return $traits;
}
Expand All @@ -254,38 +268,45 @@ public function resolvePropertyFromPropertyReflection(PhpPropertyReflection $php
$nativeReflectionProperty = $phpPropertyReflection->getNativeReflection();
$desiredClassName = $classReflection->getName();
$desiredPropertyName = $nativeReflectionProperty->getName();
/** @var Property|null $property */
$property = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($desiredClassName, $desiredPropertyName) : bool {
if (!$node instanceof Property) {
return \false;
/** @var Property|null $propertyNode */
$propertyNode = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($desiredClassName, $desiredPropertyName, &$propertyNode) : ?int {
if (!$node instanceof ClassLike) {
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof ClassLike) {
return \false;
if (!$this->nodeNameResolver->isName($node, $desiredClassName)) {
return null;
}
if (!$this->nodeNameResolver->isName($parentNode, $desiredClassName)) {
return \false;
$property = $node->getProperty($desiredPropertyName);
if ($property instanceof Property) {
$propertyNode = $property;
return NodeTraverser::STOP_TRAVERSAL;
}
return $parentNode->getProperty($desiredPropertyName) === $node;
return null;
});
if ($property instanceof Property) {
return $property;
if ($propertyNode instanceof Property) {
return $propertyNode;
}
// promoted property
return $this->findPromotedPropertyByName($nodes, $desiredPropertyName);
return $this->findPromotedPropertyByName($nodes, $desiredClassName, $desiredPropertyName);
}
private function locateClassMethodInTrait(string $methodName, MethodReflection $methodReflection) : ?ClassMethod
{
$classReflection = $methodReflection->getDeclaringClass();
$traits = $this->parseClassReflectionTraits($classReflection);
/** @var ClassMethod|null $classMethod */
$classMethod = $this->betterNodeFinder->findFirst($traits, function (Node $node) use($methodName) : bool {
return $node instanceof ClassMethod && $this->nodeNameResolver->isName($node, $methodName);
$classMethod = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($traits, function (Node $node) use($methodName, &$classMethod) : ?int {
if (!$node instanceof ClassMethod) {
return null;
}
if (!$this->nodeNameResolver->isName($node, $methodName)) {
return null;
}
$classMethod = $node;
return NodeTraverser::STOP_TRAVERSAL;
});
if ($classMethod instanceof ClassMethod) {
return $classMethod;
}
return null;
return $classMethod;
}
/**
* @return Stmt[]|null
Expand All @@ -305,25 +326,32 @@ private function parseFileNameToDecoratedNodes(string $fileName) : ?array
/**
* @param Stmt[] $stmts
*/
private function findPromotedPropertyByName(array $stmts, string $desiredPropertyName) : ?Param
private function findPromotedPropertyByName(array $stmts, string $desiredClassName, string $desiredPropertyName) : ?Param
{
$class = $this->betterNodeFinder->findFirstInstanceOf($stmts, Class_::class);
if (!$class instanceof Class_) {
return null;
}
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) {
return null;
}
foreach ($constructClassMethod->getParams() as $param) {
if ($param->flags === 0) {
continue;
/** @var Param|null $paramNode */
$paramNode = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use($desiredClassName, $desiredPropertyName, &$paramNode) {
if (!$node instanceof Class_) {
return null;
}
if ($this->nodeNameResolver->isName($param, $desiredPropertyName)) {
return $param;
if (!$this->nodeNameResolver->isName($node, $desiredClassName)) {
return null;
}
}
return null;
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) {
return null;
}
foreach ($constructClassMethod->getParams() as $param) {
if ($param->flags === 0) {
continue;
}
if ($this->nodeNameResolver->isName($param, $desiredPropertyName)) {
$paramNode = $param;
return NodeTraverser::STOP_TRAVERSAL;
}
}
});
return $paramNode;
}
private function resolveFunctionFromFuncCall(FuncCall $funcCall, Scope $scope) : ?Function_
{
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit5768fe3ac9bf02789dddb40517996133::getLoader();
return ComposerAutoloaderInitb72065d4cc1300ced72cf3a803bbfa95::getLoader();
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit5768fe3ac9bf02789dddb40517996133
class ComposerAutoloaderInitb72065d4cc1300ced72cf3a803bbfa95
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInit5768fe3ac9bf02789dddb40517996133', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitb72065d4cc1300ced72cf3a803bbfa95', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit5768fe3ac9bf02789dddb40517996133', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitb72065d4cc1300ced72cf3a803bbfa95', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit5768fe3ac9bf02789dddb40517996133::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit5768fe3ac9bf02789dddb40517996133::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit5768fe3ac9bf02789dddb40517996133
class ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -3127,9 +3127,9 @@ class ComposerStaticInit5768fe3ac9bf02789dddb40517996133
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit5768fe3ac9bf02789dddb40517996133::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5768fe3ac9bf02789dddb40517996133::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5768fe3ac9bf02789dddb40517996133::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb72065d4cc1300ced72cf3a803bbfa95::$classMap;

}, null, ClassLoader::class);
}
Expand Down

0 comments on commit b17ced8

Please sign in to comment.