1
0
mirror of https://github.com/rectorphp/rector.git synced 2025-03-14 12:29:43 +01:00

make use of isObjectTypes()

This commit is contained in:
TomasVotruba 2021-04-09 23:23:15 +02:00
parent 4b71eaa53d
commit d55b6ea8e0
2 changed files with 11 additions and 15 deletions
rules-tests/LeagueEvent/Rector/MethodCall/DispatchStringToObjectRector
rules/LeagueEvent/Rector/MethodCall

@ -29,4 +29,3 @@ final class DispatchStringToObjectRectorTest extends AbstractRectorTestCase
return __DIR__ . '/config/configured_rule.php'; return __DIR__ . '/config/configured_rule.php';
} }
} }

@ -25,7 +25,9 @@ final class DispatchStringToObjectRector extends AbstractRector
{ {
public function getRuleDefinition(): RuleDefinition public function getRuleDefinition(): RuleDefinition
{ {
return new RuleDefinition('Change string events to anonymous class which implement \League\Event\HasEventName', [ return new RuleDefinition(
'Change string events to anonymous class which implement \League\Event\HasEventName',
[
new CodeSample( new CodeSample(
<<<'CODE_SAMPLE' <<<'CODE_SAMPLE'
final class SomeClass final class SomeClass
@ -58,7 +60,8 @@ final class SomeClass
} }
} }
CODE_SAMPLE CODE_SAMPLE
) ),
]); ]);
} }
@ -85,11 +88,10 @@ CODE_SAMPLE
return true; return true;
} }
if ($this->isObjectType($methodCall->var, new ObjectType('League\Event\EventDispatcher'))) { if ($this->nodeTypeResolver->isObjectTypes($methodCall->var, [
return false; new ObjectType('League\Event\EventDispatcher'),
} new ObjectType('League\Event\Emitter'),
])) {
if ($this->isObjectType($methodCall->var, new ObjectType('League\Event\Emitter'))) {
return false; return false;
} }
@ -108,9 +110,7 @@ CODE_SAMPLE
private function createNewAnonymousEventClass(Expr $eventName): New_ private function createNewAnonymousEventClass(Expr $eventName): New_
{ {
$implements = [ $implements = [new FullyQualified('League\Event\HasEventName')];
new FullyQualified('League\Event\HasEventName')
];
return new New_(new Class_(null, [ return new New_(new Class_(null, [
'implements' => $implements, 'implements' => $implements,
@ -119,7 +119,6 @@ CODE_SAMPLE
} }
/** /**
* @param Expr $eventName
* @return Stmt[] * @return Stmt[]
*/ */
private function createAnonymousEventClassBody(Expr $eventName): array private function createAnonymousEventClassBody(Expr $eventName): array
@ -128,9 +127,7 @@ CODE_SAMPLE
new ClassMethod('eventName', [ new ClassMethod('eventName', [
'flags' => Class_::MODIFIER_PUBLIC, 'flags' => Class_::MODIFIER_PUBLIC,
'returnType' => 'string', 'returnType' => 'string',
'stmts' => [ 'stmts' => [new Return_($eventName)],
new Return_($eventName),
],
]), ]),
]; ];
} }