diff --git a/src/Contract/Helper/Database/Refactorings/DatabaseConnectionToDbalRefactoring.php b/src/Contract/Helper/Database/Refactorings/DatabaseConnectionToDbalRefactoring.php index f1d4979d5..c120da415 100644 --- a/src/Contract/Helper/Database/Refactorings/DatabaseConnectionToDbalRefactoring.php +++ b/src/Contract/Helper/Database/Refactorings/DatabaseConnectionToDbalRefactoring.php @@ -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; } diff --git a/src/Helper/Database/Refactorings/ConnectionCallFactory.php b/src/Helper/Database/Refactorings/ConnectionCallFactory.php index 88f1a9403..2396f0d6e 100644 --- a/src/Helper/Database/Refactorings/ConnectionCallFactory.php +++ b/src/Helper/Database/Refactorings/ConnectionCallFactory.php @@ -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; @@ -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); } } diff --git a/src/Helper/Database/Refactorings/DatabaseConnectionExecInsertQueryRefactoring.php b/src/Helper/Database/Refactorings/DatabaseConnectionExecInsertQueryRefactoring.php index de0a72c29..85091f069 100644 --- a/src/Helper/Database/Refactorings/DatabaseConnectionExecInsertQueryRefactoring.php +++ b/src/Helper/Database/Refactorings/DatabaseConnectionExecInsertQueryRefactoring.php @@ -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; @@ -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 diff --git a/src/Helper/Database/Refactorings/DatabaseConnectionExecTruncateTableRefactoring.php b/src/Helper/Database/Refactorings/DatabaseConnectionExecTruncateTableRefactoring.php index fbd390bb0..53d1cb8d4 100644 --- a/src/Helper/Database/Refactorings/DatabaseConnectionExecTruncateTableRefactoring.php +++ b/src/Helper/Database/Refactorings/DatabaseConnectionExecTruncateTableRefactoring.php @@ -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; @@ -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 diff --git a/src/Rector/v9/v0/DatabaseConnectionToDbalRector.php b/src/Rector/v9/v0/DatabaseConnectionToDbalRector.php index 315998f4d..ab78deb34 100644 --- a/src/Rector/v9/v0/DatabaseConnectionToDbalRector.php +++ b/src/Rector/v9/v0/DatabaseConnectionToDbalRector.php @@ -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; @@ -20,11 +19,6 @@ */ final class DatabaseConnectionToDbalRector extends AbstractRector { - /** - * @readonly - */ - public NodesToAddCollector $nodesToAddCollector; - /** * @readonly */ @@ -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; } /** @@ -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); } } @@ -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, diff --git a/tests/Rector/Core/Database/Fixture/exec_insert_query.php.inc b/tests/Rector/Core/Database/Fixture/exec_insert_query.php.inc index 613f4e5ac..58607f762 100644 --- a/tests/Rector/Core/Database/Fixture/exec_insert_query.php.inc +++ b/tests/Rector/Core/Database/Fixture/exec_insert_query.php.inc @@ -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', ]); diff --git a/tests/Rector/Core/Database/Fixture/exec_truncate_query.php.inc b/tests/Rector/Core/Database/Fixture/exec_truncate_query.php.inc index 2214411f3..96f2e7464 100644 --- a/tests/Rector/Core/Database/Fixture/exec_truncate_query.php.inc +++ b/tests/Rector/Core/Database/Fixture/exec_truncate_query.php.inc @@ -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'); } }