Skip to content

Commit

Permalink
[Core] Use ReflectionResolver to get ClassReflection when possible (#…
Browse files Browse the repository at this point in the history
…2226)

* [Core] Use ReflectionResolver to get ClassReflection when possible

* on ClassDependencyManipulator

* on ParentClassMethodTypeOverrideGuard

* on AddPropertyTypeDeclarationRector

* on ReturnTypeFromReturnNewRector

* on ParamTypeByMethodCallTypeRector

* [ci-review] Rector Rectify

* on ParamTypeByParentCallTypeRector

* on ClassMethodParamVendorLockResolver

* on AddVoidReturnTypeWhereNoReturnRector

* on PropertyTypeOverrideGuard

* on AddInterfaceByTraitRector

* LocallyCalledStaticMethodToNonStaticRector

* on MakePropertyTypedGuard

* on FinalizeClassesWithoutChildrenRector

* on ClassMethodReturnTypeOverrideGuard

* on PrivatizeFinalClassMethodRector

* scope fix

* [ci-review] Rector Rectify

* class

* [ci-review] Rector Rectify

* on ClassMethodReturnVendorLockResolver

* on CurrentAndParentClassMethodComparator

* on CurrentAndParentClassMethodComparator

* on DowngradeStringReturnTypeOnToStringRector

* on DowngradeStaticTypeDeclarationRector

* on DowngradeContravariantArgumentTypeRector

* on DowngradeSelfTypeDeclarationRector

* [ci-review] Rector Rectify

* on RemoveUnusedPrivateClassConstantRector

* [ci-review] Rector Rectify

* on SetterNodeReturnTypeInfererTypeInferer

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fix

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user authored May 4, 2022
1 parent 6327904 commit 129ce26
Show file tree
Hide file tree
Showing 28 changed files with 125 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace Rector\VendorLocker\NodeVendorLocker;

use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\SmartFileSystem\Normalizer\PathNormalizer;

final class ClassMethodParamVendorLockResolver
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly PathNormalizer $pathNormalizer,
private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer
private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand All @@ -30,7 +30,7 @@ public function isSoftLocked(ClassMethod $classMethod): bool
return true;
}

$classReflection = $this->resolveClassReflection($classMethod);
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand All @@ -46,7 +46,7 @@ public function isVendorLocked(ClassMethod $classMethod): bool
return true;
}

$classReflection = $this->resolveClassReflection($classMethod);
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand Down Expand Up @@ -84,16 +84,6 @@ private function hasTraitMethodVendorLock(ClassReflection $classReflection, stri
return false;
}

private function resolveClassReflection(ClassMethod $classMethod): ClassReflection | null
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

return $scope->getClassReflection();
}

/**
* Has interface even in our project?
* Better skip it, as PHPStan has access only to just analyzed classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\ClassMethod;
//use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -38,7 +36,8 @@ public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly AstResolver $astResolver
private readonly AstResolver $astResolver,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand All @@ -54,14 +53,9 @@ public function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
return true;
}

$childrenClassReflections = $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection);
Expand All @@ -73,7 +67,7 @@ public function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

if ($this->shouldSkipHasChildNoReturn($childrenClassReflections, $classMethod, $scope)) {
if ($this->shouldSkipHasChildNoReturn($childrenClassReflections, $classMethod)) {
return true;
}

Expand All @@ -98,12 +92,11 @@ public function shouldSkipClassMethodOldTypeWithNewType(Type $oldType, Type $new
/**
* @param ClassReflection[] $childrenClassReflections
*/
private function shouldSkipHasChildNoReturn(
array $childrenClassReflections,
ClassMethod $classMethod,
Scope $scope
): bool {
private function shouldSkipHasChildNoReturn(array $childrenClassReflections, ClassMethod $classMethod): bool
{
$methodName = $this->nodeNameResolver->getName($classMethod);
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);

foreach ($childrenClassReflections as $childClassReflection) {
if (! $childClassReflection->hasMethod($methodName)) {
continue;
Expand All @@ -126,14 +119,9 @@ private function shouldSkipHasChildNoReturn(

private function shouldSkipChaoticClassMethods(ClassMethod $classMethod): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
return true;
}

foreach (self::CHAOTIC_CLASS_METHOD_NAMES as $chaoticClass => $chaoticMethodNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@
namespace Rector\VendorLocker\NodeVendorLocker;

use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Type\MixedType;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ClassMethodReturnVendorLockResolver
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver
private readonly NodeNameResolver $nodeNameResolver,
private readonly ReflectionResolver $reflectionResolver
) {
}

public function isVendorLocked(ClassMethod $classMethod): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand Down
21 changes: 7 additions & 14 deletions packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
namespace Rector\VendorLocker;

use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
use Symplify\SmartFileSystem\Normalizer\PathNormalizer;

Expand All @@ -25,7 +24,8 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly PathNormalizer $pathNormalizer,
private readonly AstResolver $astResolver,
private readonly ParamTypeInferer $paramTypeInferer
private readonly ParamTypeInferer $paramTypeInferer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -68,10 +68,8 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
* - one of them in /vendor/ -> not allowed
* - both not in /vendor/ -> allowed
*/
/** @var Scope $scope */
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
/** @var ClassReflection $currentClassReflection */
$currentClassReflection = $scope->getClassReflection();
$currentClassReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
/** @var string $currentFileName */
$currentFileName = $currentClassReflection->getFileName();

Expand Down Expand Up @@ -121,20 +119,15 @@ public function hasParentClassMethodDifferentType(ClassMethod $classMethod, int

public function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return null;
}

/** @var string $methodName */
$methodName = $this->nodeNameResolver->getName($classMethod);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

$parentClassReflections = array_merge($classReflection->getParents(), $classReflection->getInterfaces());

foreach ($parentClassReflections as $parentClassReflection) {
if (! $parentClassReflection->hasNativeMethod($methodName)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\DeadCode\Comparator\Parameter\ParameterDefaultsComparator;
use Rector\DeadCode\Comparator\Parameter\ParameterTypeComparator;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -27,7 +28,8 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly ParameterDefaultsComparator $parameterDefaultsComparator,
private readonly ParameterTypeComparator $parameterTypeComparator,
private readonly NodeComparator $nodeComparator
private readonly NodeComparator $nodeComparator,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -94,12 +96,7 @@ private function isParentClassMethodVisibilityOrDefaultOverride(
ClassMethod $classMethod,
StaticCall $staticCall
): bool {
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand Down
13 changes: 4 additions & 9 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeCollector\ValueObject\ArrayCallableDynamicMethod;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class IsClassMethodUsedAnalyzer
{
Expand All @@ -30,7 +29,8 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ValueResolver $valueResolver,
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
private readonly CallCollectionAnalyzer $callCollectionAnalyzer
private readonly CallCollectionAnalyzer $callCollectionAnalyzer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -149,12 +149,7 @@ private function shouldSkipArrayCallable(Class_ $class, null | ArrayCallable $ar

private function doesMethodExistInTrait(ClassMethod $classMethod, string $classMethodName): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\NodeAnalyzer\EnumAnalyzer;
use Rector\Core\NodeManipulator\ClassConstManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -23,6 +22,7 @@ final class RemoveUnusedPrivateClassConstantRector extends AbstractRector
public function __construct(
private readonly ClassConstManipulator $classConstManipulator,
private readonly EnumAnalyzer $enumAnalyzer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -70,12 +70,7 @@ public function refactor(Node $node): ?Node
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -24,7 +23,8 @@
final class RemoveUnusedPrivateMethodRector extends AbstractRector
{
public function __construct(
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -92,12 +92,7 @@ public function refactor(Node $node): ?Node

private function shouldSkip(ClassMethod $classMethod): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return true;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return true;
}
Expand Down
Loading

0 comments on commit 129ce26

Please sign in to comment.