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/src/NodeResolver/ListeningMethodsCollector.php b/rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php index f5bbdae7990d..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; @@ -16,6 +17,16 @@ final class ListeningMethodsCollector { + /** + * @var string + */ + public const EVENT_TYPE_CONTRIBUTTE = 'contributte'; + + /** + * @var string + */ + public const EVENT_TYPE_CUSTOM = 'custom'; + /** * @var CallableNodeTraverser */ @@ -31,6 +42,11 @@ final class ListeningMethodsCollector */ private $eventClassNaming; + /** + * @var array + */ + private $classMethodsByEventClass = []; + public function __construct( CallableNodeTraverser $callableNodeTraverser, ValueResolver $valueResolver, @@ -44,39 +60,39 @@ public function __construct( /** * @return array */ - public function collectFromClassAndGetSubscribedEventClassMethod(Class_ $class, ClassMethod $classMethod): array - { - $classMethodsByEventClass = []; + public function collectFromClassAndGetSubscribedEventClassMethod( + Class_ $class, + ClassMethod $classMethod, + string $type + ): array { + $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 (! in_array($eventClass, $contributeEventClasses, true)) { - [$classMethod, $eventClass] = $this->resolveCustomClassMethodAndEventClass($node, $class, $eventClass); + if ($type === self::EVENT_TYPE_CONTRIBUTTE) { + /** @var string $eventClass */ + $this->resolveContributeEventClassAndSubscribedClassMethod($eventClass, $classMethod); + return; } + [$classMethod, $eventClass] = $this->resolveCustomClassMethodAndEventClass($node, $class, $eventClass); if ($classMethod === null) { return null; } @@ -85,10 +101,10 @@ public function collectFromClassAndGetSubscribedEventClassMethod(Class_ $class, return null; } - $classMethodsByEventClass[$eventClass] = $classMethod; + $this->classMethodsByEventClass[$eventClass] = $classMethod; }); - return $classMethodsByEventClass; + return $this->classMethodsByEventClass; } private function resolveCustomClassMethodAndEventClass( @@ -110,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 ee37230f60d2..1a6a998d0a20 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, + ListeningMethodsCollector::EVENT_TYPE_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) - { - } } ?> 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() {