fix for anonymous classes

This commit is contained in:
Tomas Votruba 2019-02-18 20:30:10 +01:00
parent 3a2a754c2a
commit 4bb41138f0
8 changed files with 62 additions and 11 deletions

View File

@ -75,6 +75,6 @@ CODE_SAMPLE
$this->removeNode($node);
}
return null;
return $node;
}
}

View File

@ -9,10 +9,7 @@ final class RemoveUnusedPrivateConstantRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/keep_constant.php.inc',
]);
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/keep_constant.php.inc']);
}
protected function getRectorClass(): string

View File

@ -0,0 +1,17 @@
<?php
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;
final class KeepAnonymous
{
public function createContainerFromKernelClass(string $kernelClass)
{
$kernel = $this->createKernelFromKernelClass($kernelClass);
$anonymous = new class {};
}
private function createKernelFromKernelClass($kernelClass)
{
return $kernelClass;
}
}

View File

@ -9,7 +9,11 @@ final class RemoveUnusedPrivateMethodRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/static_method.php.inc']);
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/static_method.php.inc',
__DIR__ . '/Fixture/keep_anonymous.php.inc',
]);
}
protected function getRectorClass(): string

View File

@ -5,7 +5,6 @@ namespace Rector\NodeTypeResolver\Application;
use Nette\Utils\Strings;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\PhpParser\Node\Resolver\NameResolver;
use ReflectionClass;
@ -35,8 +34,8 @@ final class FunctionLikeNodeCollector
public function addMethod(ClassMethod $classMethodNode): void
{
$className = $classMethodNode->getAttribute(Attribute::CLASS_NAME);
if ($className === null) {
throw new ShouldNotHappenException();
if ($className === null) { // anonymous
return;
}
$methodName = $this->nameResolver->resolve($classMethodNode);

View File

@ -2,6 +2,7 @@
namespace Rector\NodeTypeResolver\NodeVisitor;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
@ -51,7 +52,7 @@ final class ClassAndMethodNodeVisitor extends NodeVisitorAbstract
*/
public function enterNode(Node $node)
{
if ($node instanceof Class_ && $node->isAnonymous()) {
if ($node instanceof Class_ && $this->isClassAnonymous($node)) {
return null;
}
@ -100,4 +101,18 @@ final class ClassAndMethodNodeVisitor extends NodeVisitorAbstract
$node->setAttribute(Attribute::PARENT_CLASS_NAME, $parentClassResolvedName);
}
private function isClassAnonymous(Class_ $classNode): bool
{
if ($classNode->isAnonymous()) {
return true;
}
if ($classNode->name === null) {
return false;
}
// PHPStan polution
return Strings::startsWith($classNode->name->toString(), 'AnonymousClass');
}
}

View File

@ -2,6 +2,7 @@
namespace Rector\NodeTypeResolver\NodeVisitor;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
@ -46,7 +47,7 @@ final class NodeCollectorNodeVisitor extends NodeVisitorAbstract
*/
public function enterNode(Node $node)
{
if ($node instanceof Class_ && $node->isAnonymous() === false) {
if ($node instanceof Class_ && $this->isClassAnonymous($node) === false) {
$this->classLikeNodeCollector->addClass($node);
return;
}
@ -76,4 +77,18 @@ final class NodeCollectorNodeVisitor extends NodeVisitorAbstract
return;
}
}
private function isClassAnonymous(Class_ $classNode): bool
{
if ($classNode->isAnonymous()) {
return true;
}
if ($classNode->name === null) {
return false;
}
// PHPStan polution
return Strings::startsWith($classNode->name->toString(), 'AnonymousClass');
}
}

View File

@ -169,6 +169,10 @@ CODE_SAMPLE
/** @var string $className */
$className = $node->getAttribute(Attribute::CLASS_NAME);
// anonymous class
if ($className === null) {
return;
}
$childrenClassLikes = $this->classLikeNodeCollector->findClassesAndInterfacesByType($className);