From bd0db33fe9fbae04b978e23a22fdb9680d386e0f Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 29 May 2020 12:41:25 +0200 Subject: [PATCH 1/2] update docs --- docs/rector_rules_overview.md | 122 +++++++++++++++++- ...bscriberWithEventClassSubscriberRector.php | 3 +- .../Fixture/fixture.php.inc | 3 +- .../Source/Tomato.php | 4 +- 4 files changed, 122 insertions(+), 10 deletions(-) diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 8effba601ac5..399a14589490 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# All 490 Rectors Overview +# All 494 Rectors Overview - [Projects](#projects) - [General](#general) @@ -11,7 +11,7 @@ - [CakePHP](#cakephp) (5) - [Celebrity](#celebrity) (3) - [CodeQuality](#codequality) (53) -- [CodingStyle](#codingstyle) (30) +- [CodingStyle](#codingstyle) (31) - [DeadCode](#deadcode) (40) - [Doctrine](#doctrine) (16) - [DoctrineCodeQuality](#doctrinecodequality) (2) @@ -26,7 +26,7 @@ - [MysqlToMysqli](#mysqltomysqli) (4) - [Naming](#naming) (1) - [Nette](#nette) (12) -- [NetteKdyby](#nettekdyby) (2) +- [NetteKdyby](#nettekdyby) (4) - [NetteTesterToPHPUnit](#nettetestertophpunit) (3) - [NetteToSymfony](#nettetosymfony) (9) - [Order](#order) (3) @@ -51,7 +51,7 @@ - [PhpDeglobalize](#phpdeglobalize) (1) - [PhpSpecToPHPUnit](#phpspectophpunit) (7) - [Polyfill](#polyfill) (2) -- [Privatization](#privatization) (6) +- [Privatization](#privatization) (7) - [Refactoring](#refactoring) (2) - [RemovingStatic](#removingstatic) (4) - [Renaming](#renaming) (10) @@ -2116,6 +2116,30 @@ Prefer quote that are not inside the string
+### `UnderscoreToPascalCaseVariableAndPropertyNameRector` + +- class: [`Rector\CodingStyle\Rector\Variable\UnderscoreToPascalCaseVariableAndPropertyNameRector`](/../master/rules/coding-style/src/Rector/Variable/UnderscoreToPascalCaseVariableAndPropertyNameRector.php) +- [test fixtures](/../master/rules/coding-style/tests/Rector/Variable/UnderscoreToPascalCaseVariableAndPropertyNameRector/Fixture) + +Change under_score names to pascalCase + +```diff + final class SomeClass + { +- public function run($a_b) ++ public function run($aB) + { +- $some_value = 5; ++ $someValue = 5; + +- $this->run($a_b); ++ $this->run($aB); + } + } +``` + +
+ ### `UseIncrementAssignRector` - class: [`Rector\CodingStyle\Rector\Assign\UseIncrementAssignRector`](/../master/rules/coding-style/src/Rector/Assign/UseIncrementAssignRector.php) @@ -4606,7 +4630,6 @@ Change EventSubscriber from Kdyby to Contributte return [ - Application::class . '::onShutdown', + ShutdownEvent::class => 'onShutdown', - CustomService::class . '::onCopy' => 'onCustomCopy', ]; } @@ -4617,9 +4640,69 @@ Change EventSubscriber from Kdyby to Contributte $presenterName = $presenter->getName(); // ... } + } +``` + +
+ +### `ReplaceEventManagerWithEventSubscriberRector` + +- class: [`Rector\NetteKdyby\Rector\MethodCall\ReplaceEventManagerWithEventSubscriberRector`](/../master/rules/nette-kdyby/src/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector.php) +- [test fixtures](/../master/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Fixture) + +Change Kdyby EventManager to EventDispatcher + +```diff + use Kdyby\Events\EventManager; + + final class SomeClass + { + /** + * @var EventManager + */ + private $eventManager; + + public function __construct(EventManager $eventManager) + { + $this->eventManager = eventManager; + } + + public function run() + { + $key = '2000'; +- $this->eventManager->dispatchEvent(static::class . '::onCopy', new EventArgsList([$this, $key])); ++ $this->eventManager->dispatch(new SomeClassCopyEvent($this, $key)); + } + } +``` + +
- public function onCustomCopy() +### `ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector` + +- class: [`Rector\NetteKdyby\Rector\ClassMethod\ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector`](/../master/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php) +- [test fixtures](/../master/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture) + +Change getSubscribedEvents() from on magic property, to Event class + +```diff + use Kdyby\Events\Subscriber; + + final class ActionLogEventSubscriber implements Subscriber + { + public function getSubscribedEvents(): array + { + return [ +- AlbumService::class . '::onApprove' => 'onAlbumApprove', ++ AlbumServiceApproveEvent::class => 'onAlbumApprove', + ]; + } + +- public function onAlbumApprove(Album $album, int $adminId): void ++ public function onAlbumApprove(AlbumServiceApproveEventAlbum $albumServiceApproveEventAlbum): void { ++ $album = $albumServiceApproveEventAlbum->getAlbum(); + $album->play(); } } ``` @@ -4637,7 +4720,13 @@ Change $onProperty magic call with event disptacher and class dispatch final class FileManager { - public $onUpload; -- ++ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; + ++ public function __construct(EventDispatcherInterface $eventDispatcher) ++ { ++ $this->eventDispatcher = $eventDispatcher; ++ } ++ public function run(User $user) { - $this->onUpload($user); @@ -8345,6 +8434,25 @@ Change local property used in single method to local variable
+### `PrivatizeFinalClassMethodRector` + +- class: [`Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector`](/../master/rules/privatization/src/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php) +- [test fixtures](/../master/rules/privatization/tests/Rector/ClassMethod/PrivatizeFinalClassMethodRector/Fixture) + +Change protected class method to private if possible + +```diff + final class SomeClass + { +- protected function someMethod() ++ private function someMethod() + { + } + } +``` + +
+ ### `PrivatizeFinalClassPropertyRector` - class: [`Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector`](/../master/rules/privatization/src/Rector/Property/PrivatizeFinalClassPropertyRector.php) diff --git a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php index 2317f8da4331..d8f6b5762945 100644 --- a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php +++ b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php @@ -66,6 +66,7 @@ public function getSubscribedEvents(): array public function onAlbumApprove(Album $album, int $adminId): void { + $album->play(); } } PHP @@ -85,7 +86,7 @@ public function getSubscribedEvents(): array public function onAlbumApprove(AlbumServiceApproveEventAlbum $albumServiceApproveEventAlbum): void { $album = $albumServiceApproveEventAlbum->getAlbum(); - $adminId = $albumServiceApproveEventAlbum->getAdminId(); + $album->play(); } } PHP diff --git a/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture/fixture.php.inc b/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture/fixture.php.inc index 05a8db9f9402..079dc77746e1 100644 --- a/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture/fixture.php.inc +++ b/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture/fixture.php.inc @@ -17,6 +17,7 @@ final class ActionLogEventSubscriber implements Subscriber public function onTomatoBuy(Tomato $tomato, int $adminId): void { + $tomato->unwrap(); } } @@ -41,8 +42,8 @@ final class ActionLogEventSubscriber implements Subscriber public function onTomatoBuy(\Rector\NetteKdyby\Tests\Rector\ClassMethod\ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector\Source\Event\VegetableMarketTomatoBuyEvent $vegetableMarketTomatoBuyEvent): void { - $adminId = $vegetableMarketTomatoBuyEvent->getAdminId(); $tomato = $vegetableMarketTomatoBuyEvent->getTomato(); + $tomato->unwrap(); } } diff --git a/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Source/Tomato.php b/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Source/Tomato.php index db5167d91a4c..151764b85a3a 100644 --- a/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Source/Tomato.php +++ b/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Source/Tomato.php @@ -6,5 +6,7 @@ final class Tomato { - + public function unwrap() + { + } } From 87f7b345e5e830ca0b1b37b5d8685a2240557aa6 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 29 May 2020 12:51:59 +0200 Subject: [PATCH 2/2] do not add getter for unused param in listener method --- ...entToContributteEventObjectManipulator.php | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/rules/nette-kdyby/src/NodeManipulator/SubscriberMethodArgumentToContributteEventObjectManipulator.php b/rules/nette-kdyby/src/NodeManipulator/SubscriberMethodArgumentToContributteEventObjectManipulator.php index 010357aabb87..45ab7dddf1bd 100644 --- a/rules/nette-kdyby/src/NodeManipulator/SubscriberMethodArgumentToContributteEventObjectManipulator.php +++ b/rules/nette-kdyby/src/NodeManipulator/SubscriberMethodArgumentToContributteEventObjectManipulator.php @@ -4,6 +4,7 @@ namespace Rector\NetteKdyby\NodeManipulator; +use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; @@ -12,6 +13,8 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use Rector\CodingStyle\Naming\ClassNaming; +use Rector\Core\PhpParser\Node\BetterNodeFinder; +use Rector\Core\PhpParser\Printer\BetterStandardPrinter; use Rector\NetteKdyby\ContributeEventClassResolver; final class SubscriberMethodArgumentToContributteEventObjectManipulator @@ -26,12 +29,26 @@ final class SubscriberMethodArgumentToContributteEventObjectManipulator */ private $contributeEventClassResolver; + /** + * @var BetterNodeFinder + */ + private $betterNodeFinder; + + /** + * @var BetterStandardPrinter + */ + private $betterStandardPrinter; + public function __construct( + BetterNodeFinder $betterNodeFinder, ClassNaming $classNaming, - ContributeEventClassResolver $contributeEventClassResolver + ContributeEventClassResolver $contributeEventClassResolver, + BetterStandardPrinter $betterStandardPrinter ) { $this->classNaming = $classNaming; $this->contributeEventClassResolver = $contributeEventClassResolver; + $this->betterNodeFinder = $betterNodeFinder; + $this->betterStandardPrinter = $betterStandardPrinter; } /** @@ -44,8 +61,12 @@ public function change(array $classMethodsByEventClass): void $this->changeClassParamToEventClass($eventClass, $classMethod); - // move params + // move params to getter on event foreach ($oldParams as $oldParam) { + if (! $this->isParamUsedInClassMethodBody($classMethod, $oldParam)) { + continue; + } + $eventGetterToVariableAssign = $this->createEventGetterToVariableMethodCall($eventClass, $oldParam); $expression = new Expression($eventGetterToVariableAssign); @@ -54,6 +75,19 @@ public function change(array $classMethodsByEventClass): void } } + private function isParamUsedInClassMethodBody(ClassMethod $classMethod, Param $param): bool + { + return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use ( + $param + ) { + if (! $node instanceof Variable) { + return false; + } + + return $this->betterStandardPrinter->areNodesEqual($node, $param->var); + }); + } + private function changeClassParamToEventClass(string $eventClass, ClassMethod $classMethod): void { /** @var ClassMethod $classMethod */