Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rules/nette-kdyby/src/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
71 changes: 55 additions & 16 deletions rules/nette-kdyby/src/NodeResolver/ListeningMethodsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand All @@ -31,6 +42,11 @@ final class ListeningMethodsCollector
*/
private $eventClassNaming;

/**
* @var array<string, ClassMethod>
*/
private $classMethodsByEventClass = [];

public function __construct(
CallableNodeTraverser $callableNodeTraverser,
ValueResolver $valueResolver,
Expand All @@ -44,39 +60,39 @@ public function __construct(
/**
* @return array<string, ClassMethod>
*/
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;
}
Expand All @@ -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(
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function getSubscribedEvents()
{
return [
Application::class . '::onShutdown',
CustomService::class . '::onCopy' => 'onCustomCopy',
];
}

Expand All @@ -78,10 +77,6 @@ public function onShutdown(Presenter $presenter)
$presenterName = $presenter->getName();
// ...
}

public function onCustomCopy()
{
}
}
PHP
,
Expand All @@ -96,7 +91,6 @@ public static function getSubscribedEvents()
{
return [
ShutdownEvent::class => 'onShutdown',
CustomService::class . '::onCopy' => 'onCustomCopy',
];
}

Expand All @@ -106,10 +100,6 @@ public function onShutdown(ShutdownEvent $shutdownEvent)
$presenterName = $presenter->getName();
// ...
}

public function onCustomCopy()
{
}
}
PHP
),
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ class GetApplesSubscriber implements Subscriber
{
return [
Application::class . '::onShutdown',
CustomService::class . '::onCopy' => 'onCustomCopy',
];
}

public function onShutdown(Application $application): void
{
$presenter = $application->getPresenter();
}

public function onCustomCopy()
{
}
}

?>
Expand All @@ -40,7 +35,6 @@ class GetApplesSubscriber implements \Symfony\Component\EventDispatcher\EventSub
{
return [
\Contributte\Events\Extra\Event\Application\ShutdownEvent::class => 'onShutdown',
CustomService::class . '::onCopy' => 'onCustomCopy',
];
}

Expand All @@ -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)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down