mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-14 04:22:17 +02:00
Fix anonymous class method return type resolving [closes #1566]
This commit is contained in:
parent
18585ae5a3
commit
1509b720ad
@ -3,7 +3,9 @@
|
||||
namespace Rector\DeadCode\Rector\Concat;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\Cast\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -56,8 +58,8 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function removeStringCast(Node\Expr $expr): Node
|
||||
private function removeStringCast(Expr $expr): Expr
|
||||
{
|
||||
return $expr instanceof Node\Expr\Cast\String_ ? $expr->expr : $expr;
|
||||
return $expr instanceof String_ ? $expr->expr : $expr;
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\StringType;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\NodeTypeResolver\Contract\NodeTypeResolverAwareInterface;
|
||||
use Rector\NodeTypeResolver\Contract\PerNodeTypeResolver\PerNodeTypeResolverInterface;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -378,7 +377,8 @@ final class NodeTypeResolver
|
||||
{
|
||||
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classNode === null) {
|
||||
throw new ShouldNotHappenException();
|
||||
// anonymous class
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->resolve($classNode);
|
||||
|
@ -10,10 +10,7 @@ final class AddMethodParentCallRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/skip_already_has.php.inc',
|
||||
]);
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/skip_already_has.php.inc']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\MethodCall\RenameMethodRector\Fixture;
|
||||
|
||||
class UnderAnonymousClass
|
||||
{
|
||||
public function testErrorHandler(): void
|
||||
{
|
||||
$this->expectException(\ErrorException::class);
|
||||
|
||||
$class = new class {
|
||||
public function triggerError(): void
|
||||
{
|
||||
$this->startErrorHandling();
|
||||
trigger_error('an error has occurred');
|
||||
$this->stopErrorHandling();
|
||||
}
|
||||
};
|
||||
|
||||
$class->triggerError();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -19,6 +19,7 @@ final class RenameMethodRectorTest extends AbstractRectorTestCase
|
||||
__DIR__ . '/Fixture/fixture4.php.inc',
|
||||
__DIR__ . '/Fixture/fixture5.php.inc',
|
||||
__DIR__ . '/Fixture/fixture6.php.inc',
|
||||
__DIR__ . '/Fixture/under_anonymous_class.php.inc',
|
||||
__DIR__ . '/Fixture/SomeClass.php',
|
||||
__DIR__ . '/Fixture/nette_to_symfony_presenter.php.inc',
|
||||
]);
|
||||
@ -48,6 +49,10 @@ final class RenameMethodRectorTest extends AbstractRectorTestCase
|
||||
'*Presenter' => [
|
||||
'run' => '__invoke',
|
||||
],
|
||||
'PHPUnit\Framework\TestClass' => [
|
||||
'setExpectedException' => 'expectedException',
|
||||
'setExpectedExceptionRegExp' => 'expectedException',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
Loading…
x
Reference in New Issue
Block a user