diff --git a/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/removed_anonymous_class_construct_without_parent.php.inc b/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/removed_anonymous_class_construct_without_parent.php.inc new file mode 100644 index 00000000000..1013f3123b6 --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/removed_anonymous_class_construct_without_parent.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/skip_anonymous_class_construct_with_parent.php.inc b/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/skip_anonymous_class_construct_with_parent.php.inc new file mode 100644 index 00000000000..f5dbe64e0b5 --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture/skip_anonymous_class_construct_with_parent.php.inc @@ -0,0 +1,16 @@ + diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php index 6557fd579c4..d7ef32dab1f 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php @@ -6,6 +6,7 @@ use PhpParser\Comment\Doc; use PhpParser\Node; +use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; @@ -142,6 +143,15 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod): return true; } + // anonymous class extending a parent uses empty constructor on purpose, + // to avoid parent constructor being invoked + if ($class->isAnonymous() && $class->extends instanceof Name && $this->isName( + $classMethod, + MethodName::CONSTRUCT + )) { + return true; + } + if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $classMethod->name->toString())) { return true; } diff --git a/src/FileSystem/FilePathHelper.php b/src/FileSystem/FilePathHelper.php index 656784226ad..d4d01ce3a5a 100644 --- a/src/FileSystem/FilePathHelper.php +++ b/src/FileSystem/FilePathHelper.php @@ -56,7 +56,7 @@ public function normalizePathAndSchema(string $originalPath): string $path = $originalPath; } - $normalizedPath = PathNormalizer::normalize((string) $path); + $normalizedPath = PathNormalizer::normalize($path); $path = Strings::replace($normalizedPath, self::TWO_AND_MORE_SLASHES_REGEX, '/'); $pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';