Skip to content

Commit

Permalink
Merge pull request #1959 from sabbelasichon/issue-1955
Browse files Browse the repository at this point in the history
[TASK] Enhance OptionalConstructorToHardRequirementRector
  • Loading branch information
sabbelasichon authored Feb 9, 2021
2 parents d28d975 + 85e8c7f commit 37e3cc8
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 11 deletions.
8 changes: 1 addition & 7 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::SETS, [
SetList::PSR_12,
SetList::PHP_70,
SetList::PHP_71,
SetList::COMMON,
SetList::CLEAN_CODE,
]);
$parameters->set(Option::SETS, [SetList::PSR_12, SetList::COMMON, SetList::CLEAN_CODE]);

$parameters->set(Option::PATHS, [
__DIR__ . '/src',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
Expand Down Expand Up @@ -67,9 +68,10 @@ public function refactor(Node $node): ?Node
}

$param->default = null;
$paramsToCheck[] = $paramName;
$paramsToCheck[$paramName] = $param;
}

$potentialStmtsToRemove = [];
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
Expand All @@ -79,25 +81,65 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $stmt->expr->var instanceof PropertyFetch) {
if (! $stmt->expr->expr instanceof Coalesce) {
continue;
}

if (! $stmt->expr->expr instanceof Coalesce) {
if ($stmt->expr->var instanceof Variable && $variableName = $this->getName($stmt->expr->var)) {
$potentialStmtsToRemove[$variableName] = $stmt;
}

if (! $stmt->expr->var instanceof PropertyFetch) {
continue;
}

if (! $stmt->expr->expr->left instanceof Variable) {
continue;
}

if (! $this->isNames($stmt->expr->expr->left, $paramsToCheck)) {
if (! $this->isNames($stmt->expr->expr->left, array_keys($paramsToCheck))) {
continue;
}

if ($stmt->expr->expr->right instanceof Coalesce) {
// Reset param default value
if (null === $this->getName($stmt->expr->expr->left)) {
continue;
}

$paramsToCheck[$this->getName($stmt->expr->expr->left)]->default = $this->nodeFactory->createNull();
continue;
}

$stmt->expr->expr = $stmt->expr->expr->left;
}

foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

if (! $stmt->expr->expr instanceof MethodCall) {
continue;
}

if (! $this->isNames($stmt->expr->expr->var, array_keys($potentialStmtsToRemove))) {
continue;
}

$variableName = $this->getName($stmt->expr->expr->var);

if (null === $variableName) {
continue;
}

$this->removeNode($potentialStmtsToRemove[$variableName]);
}

return $node;
}

Expand Down
14 changes: 14 additions & 0 deletions stubs/Core/Database/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace TYPO3\CMS\Core\Database;

if (class_exists(Connection::class)) {
return;
}

final class Connection
{

}
5 changes: 5 additions & 0 deletions stubs/Core/Database/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

final class ConnectionPool
{
public function getConnectionForTable(string $table): Connection
{
return new Connection();
}

public function getQueryBuilderForTable($table): void
{
}
Expand Down
16 changes: 16 additions & 0 deletions stubs/Core/Log/LogManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace TYPO3\CMS\Core\Log;

if(class_exists(LogManager::class)) {
return null;
}

final class LogManager
{
public function getLogger(string $class): Logger
{
return new Logger();
}
}
14 changes: 14 additions & 0 deletions stubs/Core/Log/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);


namespace TYPO3\CMS\Core\Log;

if(class_exists(Logger::class)) {
return null;
}

final class Logger
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v10\v1\OptionalConstructorToHardRequirement\Fixture;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MyClassWithLogManager
{
/**
* @var Logger
*/
private $logger;

/**
* @var Connection
*/
private $connection;

/**
* @var mixed|BackendUserAuthentication|null
*/
private $backendUser;

public function __construct(LogManager $logManager = null, ConnectionPool $connectionPool = null, BackendUserAuthentication $backendUser = null)
{
$this->backendUser = $backendUser ?? $GLOBALS['BE_USER'] ?? null;
$connectionPool = $connectionPool ?? GeneralUtility::makeInstance(ConnectionPool::class);
$this->connection = $connectionPool->getConnectionForTable('sys_language');
$logManager = $logManager ?? GeneralUtility::makeInstance(LogManager::class);
$this->logger = $logManager->getLogger(__CLASS__);
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v10\v1\OptionalConstructorToHardRequirement\Fixture;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MyClassWithLogManager
{
/**
* @var Logger
*/
private $logger;

/**
* @var Connection
*/
private $connection;

/**
* @var mixed|BackendUserAuthentication|null
*/
private $backendUser;

public function __construct(LogManager $logManager, ConnectionPool $connectionPool, BackendUserAuthentication $backendUser = null)
{
$this->backendUser = $backendUser ?? $GLOBALS['BE_USER'] ?? null;
$this->connection = $connectionPool->getConnectionForTable('sys_language');
$this->logger = $logManager->getLogger(__CLASS__);
}
}

?>

0 comments on commit 37e3cc8

Please sign in to comment.