Skip to content
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,27 @@
<?php

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

#[\Attribute()]
final class NonConstructorAttribute
{
/**
* @param class-string $className
*/
public function run(string $className)
{
}
}

?>
-----
<?php

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

#[\Attribute()]
final class NonConstructorAttribute
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

#[\Attribute()]
final class SkipAttributeMarker
{
/**
* @param class-string $className
*/
public function __construct(string $className)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Rector\DeadCode\Rector\ClassMethod;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\Reflection\ClassReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Configuration\Parameter\FeatureFlags;
Expand Down Expand Up @@ -160,7 +162,16 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
return ! $classMethod->isPublic();
}

return $this->isName($classMethod, MethodName::INVOKE);
if ($this->isName($classMethod, MethodName::INVOKE)) {
return true;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $this->isAttributeMarkerConstructor($classMethod, $classReflection);
}

private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
Expand All @@ -172,4 +183,20 @@ private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool

return $phpDocInfo->hasByType(DeprecatedTagValueNode::class);
}

/**
* Skip constructor in attributes as might be a marker parameter
*/
private function isAttributeMarkerConstructor(ClassMethod $classMethod, ClassReflection $classReflection): bool
{
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
return false;
}

if (! $classReflection->isAttributeClass()) {
return false;
}

return $classMethod->getDocComment() instanceof Doc;
}
}