Skip to content

Commit

Permalink
[TypeDeclaration] Skip property names in TypedPropertyFromStrictConst…
Browse files Browse the repository at this point in the history
…ructorRector (#3128)

* add trait failing fixture in TypedPropertyFromStrictConstructorRector

* [TypeDeclaration] Skip property names in TypedPropertyFromStrictConstructorRector
  • Loading branch information
TomasVotruba committed Nov 29, 2022
1 parent 571a1e6 commit e8e3bd5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source\SomeSkipMeTrait;

final class SkipIfTraitHasSameProperty
{
use SomeSkipMeTrait;

private $skipMe;

public function __construct(string $nullableString = null)
{
$this->skipMe = $nullableString;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source;

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

Expand Down Expand Up @@ -83,7 +85,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->type !== null) {
if ($this->shouldSkip($node)) {
return null;
}

Expand Down Expand Up @@ -138,4 +140,46 @@ 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 shouldSkip(Property $property): bool
{
if ($property->type !== null) {
return true;
}

$class = $this->betterNodeFinder->findParentType($property, Class_::class);
if ($class instanceof Class_) {
$traitPropertyNames = $this->resolveTraitPropertyNames($class);
if ($this->isNames($property, $traitPropertyNames)) {
return true;
}
}

return false;
}
}

0 comments on commit e8e3bd5

Please sign in to comment.