Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transform] Make ParentClassToTraitsRector match only exact parent, no its child #1774

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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