Skip to content

Commit

Permalink
[Transform] Make ParentClassToTraitsRector match only exact parent, n…
Browse files Browse the repository at this point in the history
…o its child (#1774)
  • Loading branch information
TomasVotruba committed Feb 7, 2022
1 parent 2869f42 commit 63425ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Fixture;

use Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source\ClassInTheMiddle;

final class SkipMiddleClass extends ClassInTheMiddle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\Class_\ParentClassToTraitsRector\Source;

class ClassInTheMiddle extends ParentObject
{
}
6 changes: 4 additions & 2 deletions rules/Transform/Rector/Class_/ParentClassToTraitsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Transform\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
Expand Down Expand Up @@ -76,7 +77,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->extends === null) {
$parentExtends = $node->extends;
if (! $parentExtends instanceof Name) {
return null;
}

Expand All @@ -85,7 +87,7 @@ public function refactor(Node $node): ?Node
}

foreach ($this->parentClassToTraits as $parentClassToTrait) {
if (! $this->isObjectType($node, $parentClassToTrait->getParentObjectType())) {
if (! $this->isName($parentExtends, $parentClassToTrait->getParentType())) {
continue;
}

Expand Down
6 changes: 2 additions & 4 deletions rules/Transform/ValueObject/ParentClassToTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;

final class ParentClassToTraits
{
/**
Expand All @@ -17,9 +15,9 @@ public function __construct(
) {
}

public function getParentObjectType(): ObjectType
public function getParentType(): string
{
return new ObjectType($this->parentType);
return $this->parentType;
}

/**
Expand Down

0 comments on commit 63425ba

Please sign in to comment.