Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster AddReturnTypeDeclarationBasedOnParentClassMethodRector #4804

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 All @@ -26,7 +27,7 @@ public function __construct(
) {
}

public function hasParentClassMethod(ClassMethod $classMethod): bool
public function hasParentClassMethod(ClassMethod|MethodReflection $classMethod): bool
{
try {
$parentClassMethod = $this->resolveParentClassMethod($classMethod);
Expand All @@ -39,7 +40,7 @@ public function hasParentClassMethod(ClassMethod $classMethod): bool
}
}

public function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
public function getParentClassMethod(ClassMethod|MethodReflection $classMethod): ?MethodReflection
{
try {
return $this->resolveParentClassMethod($classMethod);
Expand All @@ -63,16 +64,22 @@ public function shouldSkipReturnTypeChange(ClassMethod $classMethod, Type $paren
return $this->typeComparator->areTypesEqual($currentReturnType, $parentType);
}

private function resolveParentClassMethod(ClassMethod $classMethod): ?MethodReflection
private function resolveParentClassMethod(ClassMethod|MethodReflection $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();
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
}

/** @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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithoutReturnType;

class MyClass extends SomeClassWithoutReturnType
{
public function run()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 Down Expand Up @@ -124,26 +125,28 @@ public function refactor(Node $node): ?Node
private function getReturnTypeRecursive(ClassMethod $classMethod): ?Type
{
$returnType = $classMethod->getReturnType();
if ($returnType !== null) {
return $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);
}

if ($returnType === null) {
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($classMethod);
if (! $parentMethodReflection instanceof MethodReflection) {
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($classMethod);
while ($parentMethodReflection instanceof MethodReflection) {
if ($parentMethodReflection->isPrivate()) {
return null;
}

$parentClassMethod = $this->astResolver->resolveClassMethodFromMethodReflection($parentMethodReflection);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

astResolver->resolveClassMethodFromMethodReflection is super in-efficient because it needs to parse the whole file into nodes

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(
Expand Down
Loading