Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Return node for DatabaseConnectionToDbalRector #4003

Merged
merged 1 commit into from
Jan 9, 2024
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 @@ -4,15 +4,12 @@

namespace Ssch\TYPO3Rector\Contract\Helper\Database\Refactorings;

use PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;

interface DatabaseConnectionToDbalRefactoring
{
/**
* @return Expr[]
*/
public function refactor(MethodCall $oldMethodCall): array;
public function refactor(MethodCall $oldMethodCall): ?Node;

public function canHandle(string $methodName): bool;
}
9 changes: 3 additions & 6 deletions src/Helper/Database/Refactorings/ConnectionCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Ssch\TYPO3Rector\Helper\Database\Refactorings;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\PhpParser\Node\NodeFactory;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -23,16 +22,14 @@ public function __construct(NodeFactory $nodeFactory)
$this->nodeFactory = $nodeFactory;
}

public function createConnectionCall(Arg $firstArgument): Assign
public function createConnectionCall(Arg $firstArgument): MethodCall
{
$connection = $this->nodeFactory->createMethodCall(
return $this->nodeFactory->createMethodCall(
$this->nodeFactory->createStaticCall(GeneralUtility::class, 'makeInstance', [
$this->nodeFactory->createClassConstReference(ConnectionPool::class),
]),
'getConnectionForTable',
[$this->nodeFactory->createArg($firstArgument->value)]
);

return new Assign(new Variable('connection'), $connection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Ssch\TYPO3Rector\Helper\Database\Refactorings;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use Rector\Core\PhpParser\Node\NodeFactory;
use Ssch\TYPO3Rector\Contract\Helper\Database\Refactorings\DatabaseConnectionToDbalRefactoring;

Expand All @@ -29,27 +28,20 @@ public function __construct(ConnectionCallFactory $connectionCallFactory, NodeFa
$this->nodeFactory = $nodeFactory;
}

/**
* @return Expr[]
*/
public function refactor(MethodCall $oldMethodCall): array
public function refactor(MethodCall $oldMethodCall): ?Node
{
$tableArgument = array_shift($oldMethodCall->args);
$dataArgument = array_shift($oldMethodCall->args);

if (! $tableArgument instanceof Arg || ! $dataArgument instanceof Arg) {
return [];
return null;
}

$connectionAssignment = $this->connectionCallFactory->createConnectionCall($tableArgument);

$connectionInsertCall = $this->nodeFactory->createMethodCall(
new Variable('connection'),
return $this->nodeFactory->createMethodCall(
$this->connectionCallFactory->createConnectionCall($tableArgument),
'insert',
[$tableArgument->value, $dataArgument->value]
);

return [$connectionAssignment, $connectionInsertCall];
}

public function canHandle(string $methodName): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Ssch\TYPO3Rector\Helper\Database\Refactorings;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use Rector\Core\PhpParser\Node\NodeFactory;
use Ssch\TYPO3Rector\Contract\Helper\Database\Refactorings\DatabaseConnectionToDbalRefactoring;

Expand All @@ -28,23 +28,19 @@ public function __construct(ConnectionCallFactory $connectionCallFactory, NodeFa
$this->nodeFactory = $nodeFactory;
}

public function refactor(MethodCall $oldMethodCall): array
public function refactor(MethodCall $oldMethodCall): ?Node
{
$tableArgument = array_shift($oldMethodCall->args);

if (! $tableArgument instanceof Arg) {
return [];
return null;
}

$connectionAssignment = $this->connectionCallFactory->createConnectionCall($tableArgument);

$connectionInsertCall = $this->nodeFactory->createMethodCall(
new Variable('connection'),
return $this->nodeFactory->createMethodCall(
$this->connectionCallFactory->createConnectionCall($tableArgument),
'truncate',
[$tableArgument->value]
);

return [$connectionAssignment, $connectionInsertCall];
}

public function canHandle(string $methodName): bool
Expand Down
25 changes: 7 additions & 18 deletions src/Rector/v9/v0/DatabaseConnectionToDbalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Ssch\TYPO3Rector\Contract\Helper\Database\Refactorings\DatabaseConnectionToDbalRefactoring;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -20,11 +19,6 @@
*/
final class DatabaseConnectionToDbalRector extends AbstractRector
{
/**
* @readonly
*/
public NodesToAddCollector $nodesToAddCollector;

/**
* @readonly
*/
Expand All @@ -41,12 +35,10 @@ final class DatabaseConnectionToDbalRector extends AbstractRector
*/
public function __construct(
Typo3NodeResolver $typo3NodeResolver,
array $databaseConnectionRefactorings,
NodesToAddCollector $nodesToAddCollector
array $databaseConnectionRefactorings
) {
$this->typo3NodeResolver = $typo3NodeResolver;
$this->databaseConnectionRefactorings = $databaseConnectionRefactorings;
$this->nodesToAddCollector = $nodesToAddCollector;
}

/**
Expand All @@ -73,12 +65,7 @@ public function refactor(Node $node): ?Node

foreach ($this->databaseConnectionRefactorings as $databaseConnectionRefactoring) {
if ($databaseConnectionRefactoring->canHandle($methodName)) {
$nodes = $databaseConnectionRefactoring->refactor($node);
foreach ($nodes as $newNode) {
$this->nodesToAddCollector->addNodeBeforeNode($newNode, $node);
}

$this->removeNode($node);
return $databaseConnectionRefactoring->refactor($node);
}
}

Expand All @@ -103,9 +90,11 @@ public function getRuleDefinition(): RuleDefinition
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$connectionPool = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);
$databaseConnectionForPages = $connectionPool->getConnectionForTable('pages');
$databaseConnectionForPages->insert(

use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \TYPO3\CMS\Core\Database\ConnectionPool;

GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages')->insert(
'pages',
[
'pid' => 0,
Expand Down
3 changes: 1 addition & 2 deletions tests/Rector/Core/Database/Fixture/exec_insert_query.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class LegacyDatabaseConnectionUsage
{
public function insertData()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
$connection->insert('pages', [
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages')->insert('pages', [
'pid' => 0,
'title' => 'Home',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class LegacyDatabaseConnectionTruncateQueryUsage
{
public function insertData()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
$connection->truncate('pages');
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages')->truncate('pages');
}
}
Loading