From 004759e08e746555d525ba46754821db346c7857 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 28 May 2020 20:15:11 +0200 Subject: [PATCH 1/3] add undescore support in dim_fetch --- rules/nette-kdyby/src/Naming/VariableNaming.php | 5 ++++- .../Fixture/duplicated_event_params.php.inc | 4 ++-- .../ExpectedDuplicatedEventParamsUploadEvent.php | 10 +++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/rules/nette-kdyby/src/Naming/VariableNaming.php b/rules/nette-kdyby/src/Naming/VariableNaming.php index 4f13cf4d4ce2..21b36110210e 100644 --- a/rules/nette-kdyby/src/Naming/VariableNaming.php +++ b/rules/nette-kdyby/src/Naming/VariableNaming.php @@ -15,6 +15,7 @@ use PhpParser\Node\Scalar\String_; use Rector\Core\Exception\NotImplementedException; use Rector\Core\PhpParser\Node\Value\ValueResolver; +use Rector\Core\Util\StaticRectorStrings; use Rector\NodeNameResolver\NodeNameResolver; final class VariableNaming @@ -114,7 +115,9 @@ private function resolveParamNameFromArrayDimFetch(ArrayDimFetch $arrayDimFetch) $valueName = $this->nodeNameResolver->getName($arrayDimFetch->var); $dimName = $this->valueResolver->getValue($arrayDimFetch->dim); - return $valueName . ucfirst($dimName); + $dimName = StaticRectorStrings::underscoreToCamelCase($dimName); + + return $valueName . $dimName; } $arrayDimFetch = $arrayDimFetch->var; diff --git a/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Fixture/duplicated_event_params.php.inc b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Fixture/duplicated_event_params.php.inc index 6f129aeb2fe4..6fb5f7b1140e 100644 --- a/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Fixture/duplicated_event_params.php.inc +++ b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Fixture/duplicated_event_params.php.inc @@ -10,7 +10,7 @@ final class DuplicatedEventParams public function run(SomeUser $user) { - $this->onUpload($user['id'], $user['name']); + $this->onUpload($user['owner_id'], $user['name']); } } @@ -34,7 +34,7 @@ final class DuplicatedEventParams } public function run(SomeUser $user) { - $duplicatedEventParamsUploadEvent = new \Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector\Fixture\Event\DuplicatedEventParamsUploadEvent($user['id'], $user['name']); + $duplicatedEventParamsUploadEvent = new \Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector\Fixture\Event\DuplicatedEventParamsUploadEvent($user['owner_id'], $user['name']); $this->eventDispatcher->dispatch($duplicatedEventParamsUploadEvent); } } diff --git a/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source/ExpectedDuplicatedEventParamsUploadEvent.php b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source/ExpectedDuplicatedEventParamsUploadEvent.php index 96c6f55dd64b..7a26f64ddb20 100644 --- a/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source/ExpectedDuplicatedEventParamsUploadEvent.php +++ b/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source/ExpectedDuplicatedEventParamsUploadEvent.php @@ -7,19 +7,19 @@ final class DuplicatedEventParamsUploadEvent extends \Symfony\Contracts\EventDis /** * @var mixed */ - private $userId; + private $userOwnerId; /** * @var mixed */ private $userName; - public function __construct($userId, $userName) + public function __construct($userOwnerId, $userName) { - $this->userId = $userId; + $this->userOwnerId = $userOwnerId; $this->userName = $userName; } - public function getUserId() + public function getUserOwnerId() { - return $this->userId; + return $this->userOwnerId; } public function getUserName() { From fa5b37b4e11b7c6ee430451347766cdde2ed4ef3 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 28 May 2020 20:49:32 +0200 Subject: [PATCH 2/3] separate contribute and custom event autocomplete to subscriber methods --- .../ListeningMethodsCollector.php | 21 +++++++++++++++---- ...bscriberWithEventClassSubscriberRector.php | 3 ++- ...iberToContributteEventSubscriberRector.php | 13 ++---------- .../Fixture/application_on_shutdown.php.inc | 10 --------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php b/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php index f5bbdae7990d..91fc3c120953 100644 --- a/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php +++ b/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php @@ -16,6 +16,11 @@ final class ListeningMethodsCollector { + /** + * @var string + */ + public const EVENT_TYPE_CONTRIBUTTE = 'contributte'; + /** * @var CallableNodeTraverser */ @@ -44,13 +49,17 @@ public function __construct( /** * @return array */ - public function collectFromClassAndGetSubscribedEventClassMethod(Class_ $class, ClassMethod $classMethod): array - { + public function collectFromClassAndGetSubscribedEventClassMethod( + Class_ $class, + ClassMethod $classMethod, + string $type + ): array { $classMethodsByEventClass = []; $this->callableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use ( $class, - &$classMethodsByEventClass + &$classMethodsByEventClass, + $type ) { if (! $node instanceof ArrayItem) { return null; @@ -73,7 +82,11 @@ public function collectFromClassAndGetSubscribedEventClassMethod(Class_ $class, $eventClass = $this->valueResolver->getValue($node->key); $contributeEventClasses = NetteEventToContributeEventClass::PROPERTY_TO_EVENT_CLASS; - if (! in_array($eventClass, $contributeEventClasses, true)) { + if ($type === self::EVENT_TYPE_CONTRIBUTTE) { + if (! in_array($eventClass, $contributeEventClasses, true)) { + return null; + } + } else { [$classMethod, $eventClass] = $this->resolveCustomClassMethodAndEventClass($node, $class, $eventClass); } diff --git a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php index ee37230f60d2..810af0275ae9 100644 --- a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php +++ b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php @@ -117,7 +117,8 @@ public function refactor(Node $node): ?Node $listeningClassMethods = $this->listeningMethodsCollector->collectFromClassAndGetSubscribedEventClassMethod( $class, - $node + $node, + 'custom' ); $this->subscriberMethodArgumentToContributteEventObjectManipulator->change($listeningClassMethods); diff --git a/rules/nette-kdyby/src/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector.php b/rules/nette-kdyby/src/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector.php index 3bad79c472ea..4862619d174f 100644 --- a/rules/nette-kdyby/src/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector.php +++ b/rules/nette-kdyby/src/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector.php @@ -69,7 +69,6 @@ public function getSubscribedEvents() { return [ Application::class . '::onShutdown', - CustomService::class . '::onCopy' => 'onCustomCopy', ]; } @@ -78,10 +77,6 @@ public function onShutdown(Presenter $presenter) $presenterName = $presenter->getName(); // ... } - - public function onCustomCopy() - { - } } PHP , @@ -96,7 +91,6 @@ public static function getSubscribedEvents() { return [ ShutdownEvent::class => 'onShutdown', - CustomService::class . '::onCopy' => 'onCustomCopy', ]; } @@ -106,10 +100,6 @@ public function onShutdown(ShutdownEvent $shutdownEvent) $presenterName = $presenter->getName(); // ... } - - public function onCustomCopy() - { - } } PHP ), @@ -146,7 +136,8 @@ public function refactor(Node $node): ?Node $listeningClassMethods = $this->listeningMethodsCollector->collectFromClassAndGetSubscribedEventClassMethod( $class, - $node + $node, + ListeningMethodsCollector::EVENT_TYPE_CONTRIBUTTE ); $this->subscriberMethodArgumentToContributteEventObjectManipulator->change($listeningClassMethods); diff --git a/rules/nette-kdyby/tests/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector/Fixture/application_on_shutdown.php.inc b/rules/nette-kdyby/tests/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector/Fixture/application_on_shutdown.php.inc index d09abb3640f6..9a7e88a39e07 100644 --- a/rules/nette-kdyby/tests/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector/Fixture/application_on_shutdown.php.inc +++ b/rules/nette-kdyby/tests/Rector/Class_/KdybyEventSubscriberToContributteEventSubscriberRector/Fixture/application_on_shutdown.php.inc @@ -11,7 +11,6 @@ class GetApplesSubscriber implements Subscriber { return [ Application::class . '::onShutdown', - CustomService::class . '::onCopy' => 'onCustomCopy', ]; } @@ -19,10 +18,6 @@ class GetApplesSubscriber implements Subscriber { $presenter = $application->getPresenter(); } - - public function onCustomCopy() - { - } } ?> @@ -40,7 +35,6 @@ class GetApplesSubscriber implements \Symfony\Component\EventDispatcher\EventSub { return [ \Contributte\Events\Extra\Event\Application\ShutdownEvent::class => 'onShutdown', - CustomService::class . '::onCopy' => 'onCustomCopy', ]; } @@ -49,10 +43,6 @@ class GetApplesSubscriber implements \Symfony\Component\EventDispatcher\EventSub $application = $shutdownEvent->getApplication(); $presenter = $application->getPresenter(); } - - public function onCustomCopy(\Rector\NetteKdyby\Tests\Rector\Class_\KdybyEventSubscriberToContributteEventSubscriberRector\Fixture\Event\CustomServiceCopyEvent $customServiceCopyEvent) - { - } } ?> From fa3ab03c12f89f7073af00276ce566aefd5f5de0 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 28 May 2020 21:03:01 +0200 Subject: [PATCH 3/3] improve complexity --- .../ListeningMethodsCollector.php | 60 +++++++++++++------ ...bscriberWithEventClassSubscriberRector.php | 2 +- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php b/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php index 91fc3c120953..44e14ff0dac6 100644 --- a/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php +++ b/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php @@ -6,6 +6,7 @@ use Nette\Utils\Strings; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; @@ -21,6 +22,11 @@ final class ListeningMethodsCollector */ public const EVENT_TYPE_CONTRIBUTTE = 'contributte'; + /** + * @var string + */ + public const EVENT_TYPE_CUSTOM = 'custom'; + /** * @var CallableNodeTraverser */ @@ -36,6 +42,11 @@ final class ListeningMethodsCollector */ private $eventClassNaming; + /** + * @var array + */ + private $classMethodsByEventClass = []; + public function __construct( CallableNodeTraverser $callableNodeTraverser, ValueResolver $valueResolver, @@ -54,42 +65,34 @@ public function collectFromClassAndGetSubscribedEventClassMethod( ClassMethod $classMethod, string $type ): array { - $classMethodsByEventClass = []; + $this->classMethodsByEventClass = []; $this->callableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use ( $class, - &$classMethodsByEventClass, $type ) { if (! $node instanceof ArrayItem) { return null; } - $possibleMethodName = $this->valueResolver->getValue($node->value); - if (! is_string($possibleMethodName)) { + if ($node->key === null) { return null; } - $classMethod = $class->getMethod($possibleMethodName); + $classMethod = $this->matchClassMethodByNodeValue($class, $node->value); if ($classMethod === null) { return null; } - if ($node->key === null) { - return null; - } - $eventClass = $this->valueResolver->getValue($node->key); - $contributeEventClasses = NetteEventToContributeEventClass::PROPERTY_TO_EVENT_CLASS; if ($type === self::EVENT_TYPE_CONTRIBUTTE) { - if (! in_array($eventClass, $contributeEventClasses, true)) { - return null; - } - } else { - [$classMethod, $eventClass] = $this->resolveCustomClassMethodAndEventClass($node, $class, $eventClass); + /** @var string $eventClass */ + $this->resolveContributeEventClassAndSubscribedClassMethod($eventClass, $classMethod); + return; } + [$classMethod, $eventClass] = $this->resolveCustomClassMethodAndEventClass($node, $class, $eventClass); if ($classMethod === null) { return null; } @@ -98,10 +101,10 @@ public function collectFromClassAndGetSubscribedEventClassMethod( return null; } - $classMethodsByEventClass[$eventClass] = $classMethod; + $this->classMethodsByEventClass[$eventClass] = $classMethod; }); - return $classMethodsByEventClass; + return $this->classMethodsByEventClass; } private function resolveCustomClassMethodAndEventClass( @@ -123,4 +126,27 @@ private function resolveCustomClassMethodAndEventClass( return [$classMethod, $eventClass]; } + + private function matchClassMethodByNodeValue(Class_ $class, Expr $expr): ?ClassMethod + { + $possibleMethodName = $this->valueResolver->getValue($expr); + if (! is_string($possibleMethodName)) { + return null; + } + + return $class->getMethod($possibleMethodName); + } + + private function resolveContributeEventClassAndSubscribedClassMethod( + string $eventClass, + ClassMethod $classMethod + ): void { + $contributeEventClasses = NetteEventToContributeEventClass::PROPERTY_TO_EVENT_CLASS; + + if (! in_array($eventClass, $contributeEventClasses, true)) { + return; + } + + $this->classMethodsByEventClass[$eventClass] = $classMethod; + } } diff --git a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php index 810af0275ae9..1a6a998d0a20 100644 --- a/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php +++ b/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php @@ -118,7 +118,7 @@ public function refactor(Node $node): ?Node $listeningClassMethods = $this->listeningMethodsCollector->collectFromClassAndGetSubscribedEventClassMethod( $class, $node, - 'custom' + ListeningMethodsCollector::EVENT_TYPE_CUSTOM ); $this->subscriberMethodArgumentToContributteEventObjectManipulator->change($listeningClassMethods);