Skip to content

Commit

Permalink
[TypeDeclaration] Use existing MakePropertyTypedGuard on TypedPropert…
Browse files Browse the repository at this point in the history
…yFromStrictConstructorRector (#3131)

* [TypeDeclaration] Use existing MakePropertyTypedGuard on TypedPropertyFromStrictConstructorRector

* clean up

* [ci-review] Rector Rectify

* phpstan

* phpstan

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 30, 2022
1 parent 02bdae0 commit 7331d04
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 45 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,4 @@ parameters:

# on purpose, as rule it about to be removed
- '#Register "Rector\\Php74\\Rector\\Property\\TypedPropertyRector" service to "php74\.php" config set#'
- '#Cognitive complexity for "Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector\:\:refactor\(\)" is 13, keep it under 10#'
- '#Cognitive complexity for "Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector\:\:refactor\(\)" is 12, keep it under 10#'
10 changes: 8 additions & 2 deletions rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php74\Guard\MakePropertyTypedGuard;

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

Expand All @@ -22,10 +24,14 @@ public function isLegal(Property $property): bool
$classReflection = $this->reflectionResolver->resolveClassReflection($property);

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

$propertyName = $this->nodeNameResolver->getName($property);
if (! $this->makePropertyTypedGuard->isLegal($property)) {
return false;
}

foreach ($classReflection->getParents() as $parentClassReflection) {
$nativeReflectionClass = $parentClassReflection->getNativeReflection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
Expand Down Expand Up @@ -36,8 +35,7 @@ public function __construct(
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly ConstructorAssignDetector $constructorAssignDetector,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly PropertyTypeOverrideGuard $propertyTypeOverrideGuard,
private readonly ReflectionProvider $reflectionProvider,
private readonly PropertyTypeOverrideGuard $propertyTypeOverrideGuard
) {
}

Expand Down Expand Up @@ -94,10 +92,6 @@ public function refactor(Node $node): ?Node
}

foreach ($node->getProperties() as $property) {
if ($this->shouldSkipProperty($property, $node)) {
continue;
}

$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty(
$property,
$constructClassMethod
Expand Down Expand Up @@ -159,41 +153,6 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::TYPED_PROPERTIES;
}

/**
* @return string[]
*/
private function resolveTraitPropertyNames(Class_ $class): array
{
$traitPropertyNames = [];

foreach ($class->getTraitUses() as $traitUse) {
foreach ($traitUse->traits as $traitName) {
$traitNameString = $this->getName($traitName);
if (! $this->reflectionProvider->hasClass($traitNameString)) {
continue;
}

$traitClassReflection = $this->reflectionProvider->getClass($traitNameString);
$nativeReflection = $traitClassReflection->getNativeReflection();
foreach ($nativeReflection->getProperties() as $property) {
$traitPropertyNames[] = $property->getName();
}
}
}

return $traitPropertyNames;
}

private function shouldSkipProperty(Property $property, Class_ $class): bool
{
if ($property->type !== null) {
return true;
}

$traitPropertyNames = $this->resolveTraitPropertyNames($class);
return $this->isNames($property, $traitPropertyNames);
}

private function isVarDocPreffered(Property $property): bool
{
if ($property->isPublic()) {
Expand Down
5 changes: 5 additions & 0 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Type\ObjectType;
use Rector\Core\Enum\ObjectReference;
Expand Down Expand Up @@ -107,6 +108,10 @@ public function countLocalPropertyFetchName(Class_ $class, string $propertyName)

public function containsLocalPropertyFetchName(Trait_ $trait, string $propertyName): bool
{
if ($trait->getProperty($propertyName) instanceof Property) {
return true;
}

return (bool) $this->betterNodeFinder->findFirst(
$trait,
fn (Node $node): bool => $this->isLocalPropertyFetchName($node, $propertyName)
Expand Down

0 comments on commit 7331d04

Please sign in to comment.