diff --git a/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php b/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php index 5f3319fc..03fa99c3 100644 --- a/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php +++ b/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php @@ -10,8 +10,8 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\Expression; +use PhpParser\NodeVisitor; use PHPStan\Type\ObjectType; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -40,18 +40,18 @@ public function getRuleDefinition(): RuleDefinition public function getNodeTypes(): array { - return [MethodCall::class, Assign::class]; + return [Expression::class, MethodCall::class]; } - public function refactor(Node $node): ?Node + public function refactor(Node $node): int|Node|null { - if ($node instanceof Assign) { - if ($node->expr instanceof New_ && $this->isName($node->expr->class, 'ConnectionHelper')) { - // Remove the instantiation statement - $parent = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parent instanceof Expression) { - $this->removeNode($parent); - } + if ($node instanceof Expression) { + if ( + $node->expr instanceof Assign && + $node->expr->expr instanceof New_ && + $this->isName($node->expr->expr->class, ConnectionHelper::class) + ) { + return NodeVisitor::REMOVE_NODE; } return null; diff --git a/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/Fixture/fixture.php.inc b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..15307b6b --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/Fixture/fixture.php.inc @@ -0,0 +1,32 @@ +getConnection('default'); + + return $connection; + } +} + +?> +----- + diff --git a/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/StaticConnectionHelperRectorTest.php b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/StaticConnectionHelperRectorTest.php new file mode 100644 index 00000000..18747be4 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/StaticConnectionHelperRectorTest.php @@ -0,0 +1,29 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/config/configured_rule.php b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/config/configured_rule.php new file mode 100644 index 00000000..a5e94ffe --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/StaticConnectionHelperRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(StaticConnectionHelperRector::class); +}; diff --git a/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php b/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php index 5906488e..32955fd9 100644 --- a/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php +++ b/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php @@ -27,7 +27,6 @@ public function testSomething() public function testConnectionHelper() { - $connectionHelper = new ConnectionHelper(); $connection = ConnectionManager::get('test'); \Cake\TestSuite\ConnectionHelper::runWithoutConstraints($connection, function ($connection) { $connection->execute('SELECT * FROM table');