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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Symfony\NodeAnalyzer\ClassAnalyzer;
use Rector\Symfony\Enum\SymfonyClass;
use Rector\Symfony\NodeFactory\GetSubscribedEventsClassMethodFactory;
use Rector\Symfony\NodeFactory\OnLogoutClassMethodFactory;
use Rector\Symfony\ValueObject\EventReferenceToMethodName;
Expand All @@ -30,11 +29,8 @@ final class LogoutHandlerToLogoutEventSubscriberRector extends AbstractRector
public function __construct(
private readonly OnLogoutClassMethodFactory $onLogoutClassMethodFactory,
private readonly GetSubscribedEventsClassMethodFactory $getSubscribedEventsClassMethodFactory,
private readonly ClassAnalyzer $classAnalyzer,
) {
$this->logoutHandlerObjectType = new ObjectType(
'Symfony\Component\Security\Http\Logout\LogoutHandlerInterface'
);
$this->logoutHandlerObjectType = new ObjectType(SymfonyClass::LOGOUT_HANDLER_INTERFACE);
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -100,44 +96,41 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->classAnalyzer->hasImplements(
$node,
'Symfony\Component\Security\Http\Logout\LogoutHandlerInterface'
)) {
return null;
}

// 1. replace LogoutHandlerInterface with EventSubscriberInterface
foreach ($node->implements as $key => $implement) {
if ($this->isName($implement, $this->logoutHandlerObjectType->getClassName())) {
unset($node->implements[$key]);
}
}

$node->implements[] = new FullyQualified('Symfony\Component\EventDispatcher\EventSubscriberInterface');
$node->implements[] = new FullyQualified(SymfonyClass::EVENT_SUBSCRIBER_INTERFACE);

// 2. refactor logout() class method to onLogout()
foreach ($node->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
}

$logoutClassMethod = $node->getMethod('logout');
if (! $logoutClassMethod instanceof ClassMethod) {
return null;
if (! $this->isName($classStmt, 'logout')) {
continue;
}

// replace logout() with onLogout() method
$node->stmts[$key] = $this->onLogoutClassMethodFactory->createFromLogoutClassMethod($classStmt);
}

$node->stmts[] = $this->onLogoutClassMethodFactory->createFromLogoutClassMethod($logoutClassMethod);
// 3. add getSubscribedEvents() class method
$node->stmts[] = $this->createGetSubsribersClassMethod();

$classMethodStmtKey = $logoutClassMethod->getAttribute(AttributeKey::STMT_KEY);
unset($node->stmts[$classMethodStmtKey]);
return $node;
}

// 3. add getSubscribedEvents() class method
$classConstFetch = $this->nodeFactory->createClassConstReference(
'Symfony\Component\Security\Http\Event\LogoutEvent'
);
private function createGetSubsribersClassMethod(): ClassMethod
{
$classConstFetch = $this->nodeFactory->createClassConstReference(SymfonyClass::LOGOUT_EVENT);

$eventReferencesToMethodNames = [new EventReferenceToMethodName($classConstFetch, 'onLogout')];
$getSubscribedEventsClassMethod = $this->getSubscribedEventsClassMethodFactory->create(
$eventReferencesToMethodNames
);
$node->stmts[] = $getSubscribedEventsClassMethod;

return $node;
return $this->getSubscribedEventsClassMethodFactory->create($eventReferencesToMethodNames);
}
}
10 changes: 10 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ final class SymfonyClass
*/
public const SYMFONY_STYLE = 'Symfony\Component\Console\Style\SymfonyStyle';

/**
* @var string
*/
public const LOGOUT_EVENT = 'Symfony\Component\Security\Http\Event\LogoutEvent';

/**
* @var string
*/
public const LOGOUT_HANDLER_INTERFACE = 'Symfony\Component\Security\Http\Logout\LogoutHandlerInterface';

/**
* @var string
*/
Expand Down