Skip to content

Commit

Permalink
[Symfony 5.2] Add LogoutHandlerToLogoutEventSubscriberRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 28, 2021
1 parent f515c8e commit 79c193d
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 82 deletions.
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -65,6 +65,8 @@
"phpstan/phpstan-nette": "^0.12.12",
"phpunit/phpunit": "^9.5",
"sebastian/diff": "^4.0.4",
"symfony/security-core": "^5.2",
"symfony/security-http": "^5.2",
"symplify/changelog-linker": "^9.0.34",
"symplify/coding-standard": "^9.0.34",
"symplify/easy-coding-standard": "^9.0.34",
Expand Down
2 changes: 1 addition & 1 deletion config/set/symfony51.php
Expand Up @@ -147,6 +147,6 @@
// @see https://github.com/symfony/symfony/pull/35858
RenameStringRector::STRING_CHANGES => [
'ROLE_PREVIOUS_ADMIN' => 'IS_IMPERSONATOR',
]
],
]]);
};
2 changes: 1 addition & 1 deletion packages/rector-generator/src/TemplateVariablesFactory.php
Expand Up @@ -127,7 +127,7 @@ private function createCodeForDefinition(string $code): string
{
if (Strings::contains($code, PHP_EOL)) {
// multi lines
return sprintf("<<<'PHP'%s%s%sPHP%s", PHP_EOL, $code, PHP_EOL, PHP_EOL);
return sprintf("<<<'CODE_SAMPLE'%s%s%sCODE_SAMPLE%s", PHP_EOL, $code, PHP_EOL, PHP_EOL);
}

// single line
Expand Down
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 @@ -154,8 +154,11 @@ function (string $className) use ($interfaceName): bool {
);
}

private function removeOrReplaceImplementedInterface(string $implementedInterfaceName, Class_ $class, int $key): void
{
private function removeOrReplaceImplementedInterface(
string $implementedInterfaceName,
Class_ $class,
int $key
): void {
$parentInterface = $this->getParentInterfaceIfFound($implementedInterfaceName);
if ($parentInterface !== null) {
$class->implements[$key] = new FullyQualified($parentInterface);
Expand Down
Expand Up @@ -116,16 +116,7 @@ public function change(array $classMethodsByEventClass, ?EventAndListenerTree $e
}
}

private function changeClassParamToEventClass(string $eventClass, ClassMethod $classMethod): void
{
$paramName = $this->classNaming->getVariableName($eventClass);
$eventVariable = new Variable($paramName);

$param = new Param($eventVariable, null, new FullyQualified($eventClass));
$classMethod->params = [$param];
}

private function isParamUsedInClassMethodBody(ClassMethod $classMethod, Param $param): bool
public function isParamUsedInClassMethodBody(ClassMethod $classMethod, Param $param): bool
{
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use (
$param
Expand All @@ -138,6 +129,15 @@ private function isParamUsedInClassMethodBody(ClassMethod $classMethod, Param $p
});
}

private function changeClassParamToEventClass(string $eventClass, ClassMethod $classMethod): void
{
$paramName = $this->classNaming->getVariableName($eventClass);
$eventVariable = new Variable($paramName);

$param = new Param($eventVariable, null, new FullyQualified($eventClass));
$classMethod->params = [$param];
}

private function createEventGetterToVariableMethodCall(
string $eventClass,
Param $param,
Expand Down
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Rector\SymfonyCodeQuality\NodeFactory;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\SymfonyCodeQuality\ValueObject\EventNameToClassAndConstant;

final class EventReferenceFactory
{
/**
* @var NodeFactory
*/
private $nodeFactory;

public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}

/**
* @param EventNameToClassAndConstant[] $eventNamesToClassConstants
* @return String_|ClassConstFetch
*/
public function createEventName(string $eventName, array $eventNamesToClassConstants): Node
{
if (class_exists($eventName)) {
return $this->nodeFactory->createClassConstReference($eventName);
}

// is string a that could be caught in constant, e.g. KernelEvents?
foreach ($eventNamesToClassConstants as $eventNameToClassConstant) {
if ($eventNameToClassConstant->getEventName() !== $eventName) {
continue;
}

return $this->nodeFactory->createClassConstFetch(
$eventNameToClassConstant->getEventClass(),
$eventNameToClassConstant->getEventConstant()
);
}

return new String_($eventName);
}
}

0 comments on commit 79c193d

Please sign in to comment.