Skip to content

Commit

Permalink
[ci-review] Rector Rectify
Browse files Browse the repository at this point in the history
  • Loading branch information
rector-bot authored and TomasVotruba committed Jan 28, 2021
1 parent 6113a01 commit 74e2131
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 73 deletions.
Expand Up @@ -30,7 +30,7 @@ public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change $service->arg(...) to $service->call(...)', [
new ConfiguredCodeSample(
<<<'PHP'
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
Expand All @@ -39,9 +39,9 @@ public function getRuleDefinition(): RuleDefinition
$services->set(SomeClass::class)
->arg('$key', 'value');
}
PHP
CODE_SAMPLE
,
<<<'PHP'
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
Expand All @@ -52,7 +52,7 @@ public function getRuleDefinition(): RuleDefinition
'$key' => 'value'
]]);
}
PHP
CODE_SAMPLE
,
[self::CLASS_TYPE_TO_METHOD_NAME => ['SomeClass' => 'configure']]
)
Expand Down
Expand Up @@ -19,26 +19,26 @@ public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Description', [
new CodeSample(
<<<'PHP'
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$this->something();
}
}
PHP
CODE_SAMPLE

,
<<<'PHP'
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$this->somethingElse();
}
}
PHP
CODE_SAMPLE

)
]);
Expand Down
Expand Up @@ -149,6 +149,38 @@ public function createFromServiceDefinitionsAndEventsToMethods(
return $getSubscribersClassMethod;
}

private function createClassMethod(): ClassMethod
{
$classMethod = $this->nodeFactory->createPublicMethod(self::GET_SUBSCRIBED_EVENTS_METHOD_NAME);
$this->visibilityManipulator->makeStatic($classMethod);

return $classMethod;
}

private function createArrayItemFromMethodAndPriority(?int $priority, string $methodName, Expr $expr): ArrayItem
{
if ($priority !== null && $priority !== 0) {
$methodNameWithPriorityArray = new Array_();
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($methodName));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber((int) $priority));

return new ArrayItem($methodNameWithPriorityArray, $expr);
}

return new ArrayItem(new String_($methodName), $expr);
}

private function decorateClassMethodWithReturnType(ClassMethod $classMethod): void
{
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
$classMethod->returnType = new Identifier('array');
}

$returnType = new ArrayType(new StringType(), new MixedType(true));
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $returnType);
}

/**
* @param ClassConstFetch|String_ $expr
* @param ServiceDefinition[] $methodNamesWithPriorities
Expand Down Expand Up @@ -200,42 +232,6 @@ private function createMultipleMethods(
$eventsToMethodsArray->items[] = new ArrayItem($multipleMethodsArray, $expr);
}

private function decorateClassMethodWithReturnType(ClassMethod $classMethod): void
{
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
$classMethod->returnType = new Identifier('array');
}

$returnType = new ArrayType(new StringType(), new MixedType(true));
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $returnType);
}

/**
* @param TagInterface[] $alreadyUsedTags
*/
private function shouldSkip(string $eventName, EventListenerTag $eventListenerTag, array $alreadyUsedTags): bool
{
if ($eventName !== $eventListenerTag->getEvent()) {
return true;
}

return in_array($eventListenerTag, $alreadyUsedTags, true);
}

private function createEventItem(EventListenerTag $eventListenerTag): ArrayItem
{
if ($eventListenerTag->getPriority() !== 0) {
$methodNameWithPriorityArray = new Array_();
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($eventListenerTag->getMethod()));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber($eventListenerTag->getPriority()));

return new ArrayItem($methodNameWithPriorityArray);
}

return new ArrayItem(new String_($eventListenerTag->getMethod()));
}

private function resolveMethodName(ServiceDefinition $serviceDefinition, string $eventName): ?string
{
/** @var EventListenerTag[]|Tag[] $eventTags */
Expand All @@ -262,24 +258,28 @@ private function resolvePriority(ServiceDefinition $serviceDefinition, string $e
return null;
}

private function createClassMethod(): ClassMethod
/**
* @param TagInterface[] $alreadyUsedTags
*/
private function shouldSkip(string $eventName, EventListenerTag $eventListenerTag, array $alreadyUsedTags): bool
{
$classMethod = $this->nodeFactory->createPublicMethod(self::GET_SUBSCRIBED_EVENTS_METHOD_NAME);
$this->visibilityManipulator->makeStatic($classMethod);
if ($eventName !== $eventListenerTag->getEvent()) {
return true;
}

return $classMethod;
return in_array($eventListenerTag, $alreadyUsedTags, true);
}

private function createArrayItemFromMethodAndPriority(?int $priority, string $methodName, Expr $expr): ArrayItem
private function createEventItem(EventListenerTag $eventListenerTag): ArrayItem
{
if ($priority !== null && $priority !== 0) {
if ($eventListenerTag->getPriority() !== 0) {
$methodNameWithPriorityArray = new Array_();
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($methodName));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber((int) $priority));
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($eventListenerTag->getMethod()));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber($eventListenerTag->getPriority()));

return new ArrayItem($methodNameWithPriorityArray, $expr);
return new ArrayItem($methodNameWithPriorityArray);
}

return new ArrayItem(new String_($methodName), $expr);
return new ArrayItem(new String_($eventListenerTag->getMethod()));
}
}
Expand Up @@ -60,7 +60,7 @@ final class EventListenerToEventSubscriberRector extends AbstractRector

public function __construct(
ListenerServiceDefinitionProvider $listenerServiceDefinitionProvider,
GetSubscribedEventsClassMethodFactory $getSubscriberEventsClassMethodFactory
GetSubscribedEventsClassMethodFactory $getSubscribedEventsClassMethodFactory
) {
$this->eventNamesToClassConstants = [
// kernel events
Expand All @@ -82,7 +82,7 @@ public function __construct(
new EventNameToClassAndConstant('console.error', self::CONSOLE_EVENTS_CLASS, 'ERROR'),
];
$this->listenerServiceDefinitionProvider = $listenerServiceDefinitionProvider;
$this->getSubscribedEventsClassMethodFactory = $getSubscriberEventsClassMethodFactory;
$this->getSubscribedEventsClassMethodFactory = $getSubscribedEventsClassMethodFactory;
}

public function getRuleDefinition(): RuleDefinition
Expand Down
Expand Up @@ -37,7 +37,7 @@ class MultipleMethodsEventSubscriber implements \Symfony\Component\EventDispatch
{
}
/**
* @return mixed[]
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
Expand Down
Expand Up @@ -29,7 +29,7 @@ class MultipleListenersCalledOnceEventSubscriber implements \Symfony\Component\E
{
}
/**
* @return mixed[]
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
Expand Down
Expand Up @@ -29,7 +29,7 @@ class MultipleMethodsWithNonKernelEventSubscriber implements \Symfony\Component\
{
}
/**
* @return mixed[]
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
Expand Down
Expand Up @@ -21,7 +21,7 @@ class SomeEventSubscriber implements \Symfony\Component\EventDispatcher\EventSub
{
}
/**
* @return mixed[]
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
Expand Down
Expand Up @@ -21,7 +21,7 @@ class WithPriorityEventSubscriber implements \Symfony\Component\EventDispatcher\
{
}
/**
* @return mixed[]
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
Expand Down
Expand Up @@ -35,14 +35,14 @@ final class LogoutHandlerToLogoutEventSubscriberRector extends AbstractRector
/**
* @var GetSubscribedEventsClassMethodFactory
*/
private $getSubscriberEventsClassMethodFactory;
private $getSubscribedEventsClassMethodFactory;

public function __construct(
OnLogoutClassMethodFactory $onLogoutClassMethodFactory,
GetSubscribedEventsClassMethodFactory $getSubscriberEventsClassMethodFactory
GetSubscribedEventsClassMethodFactory $getSubscribedEventsClassMethodFactory
) {
$this->onLogoutClassMethodFactory = $onLogoutClassMethodFactory;
$this->getSubscriberEventsClassMethodFactory = $getSubscriberEventsClassMethodFactory;
$this->getSubscribedEventsClassMethodFactory = $getSubscribedEventsClassMethodFactory;
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node
$classConstFetch = $this->createClassConstReference('Symfony\Component\Security\Http\Event\LogoutEvent');

$eventReferencesToMethodNames = [new EventReferenceToMethodName($classConstFetch, 'onLogout')];
$getSubscribedEventsClassMethod = $this->getSubscriberEventsClassMethodFactory->create(
$getSubscribedEventsClassMethod = $this->getSubscribedEventsClassMethodFactory->create(
$eventReferencesToMethodNames
);
$node->stmts[] = $getSubscribedEventsClassMethod;
Expand Down
Expand Up @@ -108,7 +108,7 @@ public function refactor(Node $node): ?Node

private function shouldSkip(MethodCall $methodCall): bool
{
if (! $this->isObjectType($methodCall, 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor')) {
if (! $this->isObjectType($methodCall->var, 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor')) {
return true;
}

Expand Down Expand Up @@ -155,16 +155,17 @@ private function getContextOptionValue(MethodCall $methodCall): ?bool

private function prepareEnableMagicMethodsExtractionFlags(bool $enableMagicCallExtractionValue): BitwiseOr
{
$classConstFetch = $this->createClassConstFetch(
$magicGetClassConstFetch = $this->createClassConstFetch(
'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor',
'MAGIC_GET'
);
$magicSet = $this->createClassConstFetch(
$magicSetClassConstFetch = $this->createClassConstFetch(
'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor',
'MAGIC_SET'
);

if (! $enableMagicCallExtractionValue) {
return new BitwiseOr($classConstFetch, $magicSet);
return new BitwiseOr($magicGetClassConstFetch, $magicSetClassConstFetch);
}

return new BitwiseOr(
Expand All @@ -173,9 +174,9 @@ private function prepareEnableMagicMethodsExtractionFlags(bool $enableMagicCallE
'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor',
'MAGIC_CALL'
),
$classConstFetch,
$magicGetClassConstFetch,
),
$magicSet,
$magicSetClassConstFetch,
);
}
}
Expand Up @@ -4,20 +4,22 @@

namespace Rector\Symfony5\Tests\Rector\Class_\LogoutHandlerToLogoutEventSubscriberRector;

use Iterator;
use Rector\Symfony5\Rector\Class_\LogoutHandlerToLogoutEventSubscriberRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class LogoutHandlerToLogoutEventSubscriberRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo): void
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): \Iterator
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
Expand Down

0 comments on commit 74e2131

Please sign in to comment.