Skip to content

Commit

Permalink
[DeadCode] Skip implements interface __construct() on RemoveUnusedPro…
Browse files Browse the repository at this point in the history
…motedPropertyRector (#5669)

* [DeadCode] Skip implements interface __construct() on RemoveUnusedPromotedPropertyRector

* fix

* fix
  • Loading branch information
samsonasik committed Feb 28, 2024
1 parent 04e62d3 commit 8b1a181
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Fixture;

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Source\SomeInterfaceWithConstruct;

final class SkipImplementsInterfaceConstruct implements SomeInterfaceWithConstruct
{
public function __construct(private string $a, private string $b)
{
echo $a;
}
}
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Source;

interface SomeInterfaceWithConstruct
{
public function __construct(string $a, string $b);
}
Expand Up @@ -10,11 +10,13 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\Visibility;
Expand All @@ -32,6 +34,7 @@ public function __construct(
private readonly VisibilityManipulator $visibilityManipulator,
private readonly PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -157,6 +160,16 @@ private function shouldSkipClass(Class_ $class): bool
}
}

$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if ($classReflection instanceof ClassReflection) {
$interfaces = $classReflection->getInterfaces();
foreach ($interfaces as $interface) {
if ($interface->hasNativeMethod(MethodName::CONSTRUCT)) {
return true;
}
}
}

return false;
}
}

0 comments on commit 8b1a181

Please sign in to comment.