Skip to content

Commit

Permalink
Updated Rector to commit 2604a47c32dd8606349a1724122977c8b2fa6bf9
Browse files Browse the repository at this point in the history
rectorphp/rector-src@2604a47 Faster AddReturnTypeDeclarationBasedOnParentClassMethodRector (#4804)
  • Loading branch information
TomasVotruba committed Aug 17, 2023
1 parent e7a7a23 commit 7e217a5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 38 deletions.
33 changes: 24 additions & 9 deletions packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector\Source\Method;
use Rector\VendorLocker\Exception\UnresolvableClassException;
final class ParentClassMethodTypeOverrideGuard
{
Expand Down Expand Up @@ -48,7 +49,10 @@ public function __construct(NodeNameResolver $nodeNameResolver, ReflectionResolv
$this->staticTypeMapper = $staticTypeMapper;
$this->classReflectionAnalyzer = $classReflectionAnalyzer;
}
public function hasParentClassMethod(ClassMethod $classMethod) : bool
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PHPStan\Reflection\MethodReflection $classMethod
*/
public function hasParentClassMethod($classMethod) : bool
{
try {
$parentClassMethod = $this->resolveParentClassMethod($classMethod);
Expand All @@ -59,7 +63,10 @@ public function hasParentClassMethod(ClassMethod $classMethod) : bool
return \true;
}
}
public function getParentClassMethod(ClassMethod $classMethod) : ?MethodReflection
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PHPStan\Reflection\MethodReflection $classMethod
*/
public function getParentClassMethod($classMethod) : ?MethodReflection
{
try {
return $this->resolveParentClassMethod($classMethod);
Expand All @@ -78,15 +85,23 @@ public function shouldSkipReturnTypeChange(ClassMethod $classMethod, Type $paren
}
return $this->typeComparator->areTypesEqual($currentReturnType, $parentType);
}
private function resolveParentClassMethod(ClassMethod $classMethod) : ?MethodReflection
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PHPStan\Reflection\MethodReflection $classMethod
*/
private function resolveParentClassMethod($classMethod) : ?MethodReflection
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (!$classReflection instanceof ClassReflection) {
// we can't resolve the class, so we don't know.
throw new UnresolvableClassException();
if ($classMethod instanceof ClassMethod) {
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (!$classReflection instanceof ClassReflection) {
// we can't resolve the class, so we don't know.
throw new UnresolvableClassException();
}
/** @var string $methodName */
$methodName = $this->nodeNameResolver->getName($classMethod);
} else {
$classReflection = $classMethod->getDeclaringClass();
$methodName = $classMethod->getName();
}
/** @var string $methodName */
$methodName = $this->nodeNameResolver->getName($classMethod);
$currentClassReflection = $classReflection;
while ($this->hasClassParent($currentClassReflection)) {
$parentClassReflection = $currentClassReflection->getParentClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand All @@ -31,20 +32,14 @@ final class AddReturnTypeDeclarationBasedOnParentClassMethodRector extends Abstr
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
*/
private $parentClassMethodTypeOverrideGuard;
/**
* @readonly
* @var \Rector\Core\PhpParser\AstResolver
*/
private $astResolver;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, AstResolver $astResolver, PhpVersionProvider $phpVersionProvider)
public function __construct(ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, PhpVersionProvider $phpVersionProvider)
{
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
$this->astResolver = $astResolver;
$this->phpVersionProvider = $phpVersionProvider;
}
public function provideMinPhpVersion() : int
Expand Down Expand Up @@ -118,21 +113,24 @@ public function refactor(Node $node) : ?Node
private function getReturnTypeRecursive(ClassMethod $classMethod) : ?Type
{
$returnType = $classMethod->getReturnType();
if ($returnType === null) {
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($classMethod);
if (!$parentMethodReflection instanceof MethodReflection) {
if ($returnType !== null) {
return $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);
}
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($classMethod);
while ($parentMethodReflection instanceof MethodReflection) {
if ($parentMethodReflection->isPrivate()) {
return null;
}
$parentClassMethod = $this->astResolver->resolveClassMethodFromMethodReflection($parentMethodReflection);
if (!$parentClassMethod instanceof ClassMethod) {
return null;
$parentReturnType = ParametersAcceptorSelector::selectSingle($parentMethodReflection->getVariants())->getReturnType();
if (!$parentReturnType instanceof MixedType) {
return $parentReturnType;
}
if ($parentClassMethod->isPrivate()) {
return null;
if ($parentReturnType->isExplicitMixed()) {
return $parentReturnType;
}
return $this->getReturnTypeRecursive($parentClassMethod);
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($parentMethodReflection);
}
return $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);
return null;
}
private function processClassMethodReturnType(Class_ $class, ClassMethod $classMethod, Type $parentType) : ?ClassMethod
{
Expand Down
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 = '81caf1ea0dd2a204e8e209f3f1adab13448b2f9d';
public const PACKAGE_VERSION = '2604a47c32dd8606349a1724122977c8b2fa6bf9';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-08-17 06:38:05';
public const RELEASE_DATE = '2023-08-17 16:00:02';
/**
* @var int
*/
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 ComposerAutoloaderInit94b3ce07c8a7d6fe3f8bb56be57668e5::getLoader();
return ComposerAutoloaderInit114602c24adaa04bde1454b34b25e313::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 ComposerAutoloaderInit94b3ce07c8a7d6fe3f8bb56be57668e5
class ComposerAutoloaderInit114602c24adaa04bde1454b34b25e313
{
private static $loader;

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

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

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

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

$filesToLoad = \Composer\Autoload\ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit114602c24adaa04bde1454b34b25e313::$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 ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5
class ComposerStaticInit114602c24adaa04bde1454b34b25e313
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -2637,9 +2637,9 @@ class ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit94b3ce07c8a7d6fe3f8bb56be57668e5::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit114602c24adaa04bde1454b34b25e313::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit114602c24adaa04bde1454b34b25e313::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit114602c24adaa04bde1454b34b25e313::$classMap;

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

0 comments on commit 7e217a5

Please sign in to comment.