mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[League] Remove only League upgrade set from core, give space for community package with whole vendor (#1305)
This commit is contained in:
parent
12ac17392f
commit
14f67de7e9
|
@ -1,4 +1,4 @@
|
|||
# 498 Rules Overview
|
||||
# 501 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
|||
|
||||
- [Composer](#composer) (6)
|
||||
|
||||
- [DeadCode](#deadcode) (49)
|
||||
- [DeadCode](#deadcode) (50)
|
||||
|
||||
- [DependencyInjection](#dependencyinjection) (3)
|
||||
|
||||
|
@ -26,9 +26,9 @@
|
|||
|
||||
- [DowngradePhp54](#downgradephp54) (6)
|
||||
|
||||
- [DowngradePhp55](#downgradephp55) (3)
|
||||
- [DowngradePhp55](#downgradephp55) (4)
|
||||
|
||||
- [DowngradePhp56](#downgradephp56) (3)
|
||||
- [DowngradePhp56](#downgradephp56) (4)
|
||||
|
||||
- [DowngradePhp70](#downgradephp70) (12)
|
||||
|
||||
|
@ -42,14 +42,12 @@
|
|||
|
||||
- [DowngradePhp80](#downgradephp80) (19)
|
||||
|
||||
- [DowngradePhp81](#downgradephp81) (6)
|
||||
- [DowngradePhp81](#downgradephp81) (7)
|
||||
|
||||
- [EarlyReturn](#earlyreturn) (11)
|
||||
|
||||
- [Generics](#generics) (1)
|
||||
|
||||
- [LeagueEvent](#leagueevent) (1)
|
||||
|
||||
- [MockeryToProphecy](#mockerytoprophecy) (2)
|
||||
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
|
@ -2576,7 +2574,7 @@ Use `class` keyword for class name resolution in string instead of hardcoded str
|
|||
|
||||
```diff
|
||||
-$value = 'App\SomeClass::someMethod()';
|
||||
+$value = \App\SomeClass . '::someMethod()';
|
||||
+$value = \App\SomeClass::class . '::someMethod()';
|
||||
```
|
||||
|
||||
<br>
|
||||
|
@ -3686,6 +3684,29 @@ Remove unused if check to non-empty array before foreach of the array
|
|||
|
||||
<br>
|
||||
|
||||
### RemoveUnusedParamInRequiredAutowireRector
|
||||
|
||||
Remove unused parameter in required autowire method
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector`](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedParamInRequiredAutowireRector.php)
|
||||
|
||||
```diff
|
||||
use Symfony\Contracts\Service\Attribute\Required;
|
||||
|
||||
final class SomeService
|
||||
{
|
||||
private $visibilityManipulator;
|
||||
|
||||
#[Required]
|
||||
- public function autowireSomeService(VisibilityManipulator $visibilityManipulator)
|
||||
+ public function autowireSomeService()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveUnusedPrivateClassConstantRector
|
||||
|
||||
Remove unused class constants
|
||||
|
@ -4210,6 +4231,19 @@ Downgrade arbitrary expression arguments to `empty()` and `isset()`
|
|||
|
||||
<br>
|
||||
|
||||
### DowngradeBoolvalRector
|
||||
|
||||
Replace `boolval()` by type casting to boolean
|
||||
|
||||
- class: [`Rector\DowngradePhp55\Rector\FuncCall\DowngradeBoolvalRector`](../rules/DowngradePhp55/Rector/FuncCall/DowngradeBoolvalRector.php)
|
||||
|
||||
```diff
|
||||
-$bool = boolval($value);
|
||||
+$bool = (bool) $value;
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DowngradeClassConstantToStringRector
|
||||
|
||||
Replace <class>::class constant by string class names
|
||||
|
@ -4295,6 +4329,21 @@ Changes ** (exp) operator to pow(val, val2)
|
|||
|
||||
<br>
|
||||
|
||||
### DowngradeUseFunctionRector
|
||||
|
||||
Replace imports of functions and constants
|
||||
|
||||
- class: [`Rector\DowngradePhp56\Rector\Use_\DowngradeUseFunctionRector`](../rules/DowngradePhp56/Rector/Use_/DowngradeUseFunctionRector.php)
|
||||
|
||||
```diff
|
||||
-use function Foo\Bar\baz;
|
||||
-
|
||||
-$var = baz();
|
||||
+$var = \Foo\Bar\baz();
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## DowngradePhp70
|
||||
|
||||
### DowngradeAnonymousClassRector
|
||||
|
@ -5732,6 +5781,24 @@ change instanceof Object to is_resource
|
|||
|
||||
<br>
|
||||
|
||||
### DowngradePureIntersectionTypeRector
|
||||
|
||||
Remove the intersection type params and returns, add `@param/@return` tags instead
|
||||
|
||||
- class: [`Rector\DowngradePhp81\Rector\FunctionLike\DowngradePureIntersectionTypeRector`](../rules/DowngradePhp81/Rector/FunctionLike/DowngradePureIntersectionTypeRector.php)
|
||||
|
||||
```diff
|
||||
-function someFunction(): Foo&Bar
|
||||
+/**
|
||||
+ * @return Foo&Bar
|
||||
+ */
|
||||
+function someFunction()
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DowngradeReadonlyPropertyRector
|
||||
|
||||
Remove "readonly" property type, add a "@readonly" tag instead
|
||||
|
@ -6104,36 +6171,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
<br>
|
||||
|
||||
## LeagueEvent
|
||||
|
||||
### DispatchStringToObjectRector
|
||||
|
||||
Change string events to anonymous class which implement \League\Event\HasEventName
|
||||
|
||||
- class: [`Rector\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector`](../rules/LeagueEvent/Rector/MethodCall/DispatchStringToObjectRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
- $this->dispatcher->dispatch('my-event');
|
||||
+ $this->dispatcher->dispatch(new class implements \League\Event\HasEventName
|
||||
+ {
|
||||
+ public function eventName(): string
|
||||
+ {
|
||||
+ return 'my-event';
|
||||
+ }
|
||||
+ });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## MockeryToProphecy
|
||||
|
||||
### MockeryCloseRemoveRector
|
||||
|
@ -8403,11 +8440,11 @@ Replace property declaration of new state with direct new
|
|||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
private Logger $logger;
|
||||
|
||||
- private Logger $logger;
|
||||
-
|
||||
public function __construct(
|
||||
- ?Logger $logger = null,
|
||||
+ Logger $logger = new NullLogger,
|
||||
+ private Logger $logger = new NullLogger,
|
||||
) {
|
||||
- $this->logger = $logger ?? new NullLogger;
|
||||
}
|
||||
|
|
|
@ -1,103 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPStan\Type\ObjectWithoutClassType;
|
||||
use PHPStan\Type\StringType;
|
||||
use PHPStan\Type\VoidType;
|
||||
use Rector\Composer\Rector\ChangePackageVersionComposerRector;
|
||||
use Rector\Composer\ValueObject\PackageAndVersion;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector;
|
||||
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
|
||||
use Rector\Removing\Rector\Class_\RemoveParentRector;
|
||||
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
|
||||
use Rector\Renaming\Rector\Name\RenameClassRector;
|
||||
use Rector\Renaming\ValueObject\MethodCallRename;
|
||||
use Rector\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
|
||||
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
|
||||
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ChangePackageVersionComposerRector::class)
|
||||
->call('configure', [[
|
||||
ChangePackageVersionComposerRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
|
||||
new PackageAndVersion('league/event', '^3.0'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(RenameMethodRector::class)
|
||||
->call('configure', [[
|
||||
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
|
||||
new MethodCallRename('League\Event\EventInterface', 'getName', 'eventName'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'emit', 'dispatch'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'addListener', 'subscribeTo'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'addOneTimeListener', 'subscribeOneTo'),
|
||||
new MethodCallRename('League\Event\EmitterInterface', 'useListenerProvider', 'subscribeListenersFrom'),
|
||||
new MethodCallRename('League\Event\ListenerInterface', 'handle', '__invoke'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(AddParamTypeDeclarationRector::class)
|
||||
->call('configure', [[
|
||||
AddParamTypeDeclarationRector::PARAMETER_TYPEHINTS => ValueObjectInliner::inline([
|
||||
new AddParamTypeDeclaration(
|
||||
'League\Event\ListenerInterface',
|
||||
'__invoke',
|
||||
0,
|
||||
new ObjectWithoutClassType()
|
||||
),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(AddReturnTypeDeclarationRector::class)
|
||||
->call('configure', [[
|
||||
AddReturnTypeDeclarationRector::METHOD_RETURN_TYPES => ValueObjectInliner::inline([
|
||||
new AddReturnTypeDeclaration('League\Event\EventInterface', 'eventName', new StringType()),
|
||||
new AddReturnTypeDeclaration('League\Event\ListenerInterface', '__invoke', new VoidType()),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(RenameClassRector::class)
|
||||
->call('configure', [[
|
||||
RenameClassRector::OLD_TO_NEW_CLASSES => [
|
||||
'League\Event\Emitter' => 'League\Event\EventDispatcher',
|
||||
'League\Event\ListenerInterface' => 'League\Event\Listener',
|
||||
'League\Event\GeneratorInterface' => 'League\Event\EventGenerator',
|
||||
'League\Event\ListenerProviderInterface' => 'League\Event\ListenerSubscriber',
|
||||
'League\Event\ListenerAcceptorInterface' => 'League\Event\ListenerRegistry',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(AddInterfaceByParentRector::class)
|
||||
->call('configure', [[
|
||||
AddInterfaceByParentRector::INTERFACE_BY_PARENT => [
|
||||
'League\Event\Event' => 'League\Event\HasEventName',
|
||||
'League\Event\AbstractListener' => 'League\Event\Listener',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(RemoveInterfacesRector::class)
|
||||
->call('configure', [[
|
||||
RemoveInterfacesRector::INTERFACES_TO_REMOVE => ['League\Event\EventInterface'],
|
||||
]]);
|
||||
|
||||
$services->set(RemoveParentRector::class)
|
||||
->call('configure', [[
|
||||
RemoveParentRector::PARENT_TYPES_TO_REMOVE => [
|
||||
'League\Event\AbstractEvent',
|
||||
'League\Event\Event',
|
||||
'League\Event\AbstractListener',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(DispatchStringToObjectRector::class);
|
||||
};
|
|
@ -65,11 +65,6 @@ final class SetList implements SetListInterface
|
|||
*/
|
||||
public const GMAGICK_TO_IMAGICK = __DIR__ . '/../../../config/set/gmagick_to_imagick.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const LEAGUE_EVENT_30 = __DIR__ . '/../../../config/set/league-event-30.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DispatchStringToObjectRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
class EventObject
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch(new \League\Event\Event('my-event'));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,44 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch('my-event');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch(new class('my-event') implements \League\Event\HasEventName
|
||||
{
|
||||
private $name;
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
public function eventName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,54 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
final class DispatcherCall
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch($this->createEventName());
|
||||
}
|
||||
|
||||
private function createEventName(): string
|
||||
{
|
||||
return 'my-event';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
final class DispatcherCall
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch(new class($this->createEventName()) implements \League\Event\HasEventName
|
||||
{
|
||||
private $name;
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
public function eventName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function createEventName(): string
|
||||
{
|
||||
return 'my-event';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
final class SomeVariable
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$name = 'my-event';
|
||||
$this->dispatcher->dispatch($name);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;
|
||||
|
||||
final class SomeVariable
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$name = 'my-event';
|
||||
$this->dispatcher->dispatch(new class($name) implements \League\Event\HasEventName
|
||||
{
|
||||
private $name;
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
public function eventName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DispatchStringToObjectRector::class);
|
||||
};
|
|
@ -1,186 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\LeagueEvent\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\PropertyProperty;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\StringType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\DispatchStringToObjectRectorTest
|
||||
*/
|
||||
final class DispatchStringToObjectRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const STMTS = 'stmts';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const NAME = 'name';
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Change string events to anonymous class which implement \League\Event\HasEventName',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch('my-event');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
/** @var \League\Event\EventDispatcher */
|
||||
private $dispatcher;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->dispatcher->dispatch(new class implements \League\Event\HasEventName
|
||||
{
|
||||
public function eventName(): string
|
||||
{
|
||||
return 'my-event';
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Expr>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->updateNode($node);
|
||||
}
|
||||
|
||||
private function shouldSkip(MethodCall $methodCall): bool
|
||||
{
|
||||
if (! $this->isNames($methodCall->name, ['dispatch', 'emit'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->nodeTypeResolver->isObjectTypes($methodCall->var, [
|
||||
new ObjectType('League\Event\EventDispatcher'),
|
||||
new ObjectType('League\Event\Emitter'),
|
||||
])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! isset($methodCall->args[0])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $methodCall->args[0] instanceof Arg) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ! $this->getType($methodCall->args[0]->value) instanceof StringType;
|
||||
}
|
||||
|
||||
private function updateNode(MethodCall $methodCall): MethodCall
|
||||
{
|
||||
/** @var Arg $firstArg */
|
||||
$firstArg = $methodCall->args[0];
|
||||
$methodCall->args[0] = new Arg($this->createNewAnonymousEventClass($firstArg->value));
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
private function createNewAnonymousEventClass(Expr $expr): New_
|
||||
{
|
||||
$implements = [new FullyQualified('League\Event\HasEventName')];
|
||||
|
||||
return new New_(new Class_(null, [
|
||||
'implements' => $implements,
|
||||
self::STMTS => $this->createAnonymousEventClassBody(),
|
||||
]), [new Arg($expr)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Stmt[]
|
||||
*/
|
||||
private function createAnonymousEventClassBody(): array
|
||||
{
|
||||
$return = new Return_(new PropertyFetch(new Variable('this'), 'name'));
|
||||
|
||||
return [
|
||||
new Property(Class_::MODIFIER_PRIVATE, [new PropertyProperty(self::NAME)]),
|
||||
new ClassMethod(MethodName::CONSTRUCT, [
|
||||
'flags' => Class_::MODIFIER_PUBLIC,
|
||||
'params' => $this->createConstructParams(),
|
||||
self::STMTS => [new Expression($this->createConstructAssign())],
|
||||
]),
|
||||
new ClassMethod('eventName', [
|
||||
'flags' => Class_::MODIFIER_PUBLIC,
|
||||
'returnType' => 'string',
|
||||
self::STMTS => [$return],
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Param[]
|
||||
*/
|
||||
private function createConstructParams(): array
|
||||
{
|
||||
return [new Param(new Variable(self::NAME), null, 'string')];
|
||||
}
|
||||
|
||||
private function createConstructAssign(): Assign
|
||||
{
|
||||
$propertyFetch = new PropertyFetch(new Variable('this'), 'name');
|
||||
return new Assign($propertyFetch, new Variable(self::NAME));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user