Skip to content

Commit

Permalink
[Php74] Refactor MakePropertyTypedGuard to check directly on ClassRef…
Browse files Browse the repository at this point in the history
…lection instead of lookup parent to get class (#2197)

* [Php74] Refactor MakePropertyTypedGuard to check directly on ClassReflection instead of lookup parent to get class

* check

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fix

* fix

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 29, 2022
1 parent dbc2638 commit 6063b59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -20,7 +22,8 @@ public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyManipulator $propertyManipulator
private readonly PropertyManipulator $propertyManipulator,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand All @@ -31,8 +34,14 @@ public function resolve(Property $property): ?string
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($property);

if (! $classReflection instanceof ClassReflection) {
return null;
}

$propertyName = $this->nodeNameResolver->getName($property);
if ($this->propertyManipulator->isUsedByTrait($class, $propertyName)) {
if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
return null;
}

Expand Down
14 changes: 5 additions & 9 deletions rules/Php74/Guard/MakePropertyTypedGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

namespace Rector\Php74\Guard;

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\NodeAnalyzer\PropertyAnalyzer;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\Guard\ParentPropertyLookupGuard;

final class MakePropertyTypedGuard
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PropertyAnalyzer $propertyAnalyzer,
private readonly PropertyManipulator $propertyManipulator,
Expand Down Expand Up @@ -50,14 +47,13 @@ public function isLegal(Property $property, bool $inlinePublic = true): bool
* - trait properties are unpredictable based on class context they appear in
* - on interface properties as well, as interface not allowed to have property
*/
$class = $this->betterNodeFinder->findParentType($property, Class_::class);
if (! $class instanceof Class_) {
if (! $classReflection->isClass()) {
return false;
}

$propertyName = $this->nodeNameResolver->getName($property);

if ($this->propertyManipulator->isUsedByTrait($class, $propertyName)) {
if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
return false;
}

Expand All @@ -69,16 +65,16 @@ public function isLegal(Property $property, bool $inlinePublic = true): bool
return ! $this->propertyAnalyzer->hasForbiddenType($property);
}

return $this->isSafeProtectedProperty($property, $class);
return $this->isSafeProtectedProperty($property, $classReflection);
}

private function isSafeProtectedProperty(Property $property, Class_ $class): bool
private function isSafeProtectedProperty(Property $property, ClassReflection $classReflection): bool
{
if (! $property->isProtected()) {
return false;
}

if (! $class->isFinal()) {
if (! $classReflection->isFinal()) {
return false;
}

Expand Down
21 changes: 10 additions & 11 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\Node\Stmt\Unset_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -234,18 +235,16 @@ public function resolveExistingClassPropertyNameByType(Class_ $class, Type $type
return null;
}

public function isUsedByTrait(Class_ $class, string $propertyName): bool
public function isUsedByTrait(ClassReflection $classReflection, string $propertyName): bool
{
foreach ($class->getTraitUses() as $traitUse) {
foreach ($traitUse->traits as $traitName) {
$trait = $this->astResolver->resolveClassFromName($traitName->toString());
if (! $trait instanceof Trait_) {
continue;
}

if ($this->propertyFetchAnalyzer->containsLocalPropertyFetchName($trait, $propertyName)) {
return true;
}
foreach ($classReflection->getTraits() as $traitUse) {
$trait = $this->astResolver->resolveClassFromName($traitUse->getName());
if (! $trait instanceof Trait_) {
continue;
}

if ($this->propertyFetchAnalyzer->containsLocalPropertyFetchName($trait, $propertyName)) {
return true;
}
}

Expand Down

0 comments on commit 6063b59

Please sign in to comment.