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;

function createPlain()
{
return new class {
public function __construct()
{
}
};
}

?>
-----
<?php

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

function createPlain()
{
return new class
{
};
}

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

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Source\ParentWithoutConstructor;

function createStub()
{
return new class extends ParentWithoutConstructor {
public function __construct()
{
}
};
}

?>
10 changes: 10 additions & 0 deletions rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystem/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '';
Expand Down
Loading