-
Notifications
You must be signed in to change notification settings - Fork 60
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
add 5.1 rector tests + connectionhelper rector #298
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
||
use Cake\TestSuite\ConnectionHelper; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class StaticConnectionHelperRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Transform ConnectionHelper instance method calls to static calls', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$connectionHelper = new ConnectionHelper(); | ||
$connectionHelper->runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
ConnectionHelper::runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, Assign::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
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); | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
// Ensure the node is a method call on the ConnectionHelper instance | ||
if (! $this->isObjectType($node->var, new ObjectType(ConnectionHelper::class))) { | ||
return null; | ||
} | ||
|
||
// Replace with a static method call | ||
return new StaticCall( | ||
new Node\Name\FullyQualified(ConnectionHelper::class), | ||
$node->name, | ||
$node->args | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyPlugin; | ||
|
||
use Cake\Datasource\ConnectionManager; | ||
use Cake\TestSuite\ConnectionHelper; | ||
use Cake\TestSuite\IntegrationTestTrait; | ||
use Cake\TestSuite\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
use IntegrationTestTrait; | ||
|
||
public function testSomething() | ||
{ | ||
$config = [ | ||
'_cake_core_' => [ | ||
'className' => 'FileEngine', | ||
'prefix' => 'myapp_cake_core_', | ||
'path' => 'persistent', | ||
'serialize' => true, | ||
'duration' => '+1 years', | ||
], | ||
]; | ||
} | ||
|
||
public function testConnectionHelper() | ||
{ | ||
$connectionHelper = new ConnectionHelper(); | ||
$connection = ConnectionManager::get('test'); | ||
$connectionHelper->runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
$connectionHelper->dropTables('test', ['table']); | ||
$connectionHelper->enableQueryLogging(['test']); | ||
$connectionHelper->truncateTables('test', ['table']); | ||
$connectionHelper->addTestAliases(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyPlugin; | ||
|
||
use Cake\Datasource\ConnectionManager; | ||
use Cake\TestSuite\ConnectionHelper; | ||
use Cake\TestSuite\IntegrationTestTrait; | ||
use Cake\TestSuite\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
use IntegrationTestTrait; | ||
|
||
public function testSomething() | ||
{ | ||
$config = [ | ||
'_cake_translations_' => [ | ||
'className' => 'FileEngine', | ||
'prefix' => 'myapp_cake_core_', | ||
'path' => 'persistent', | ||
'serialize' => true, | ||
'duration' => '+1 years', | ||
], | ||
]; | ||
} | ||
|
||
public function testConnectionHelper() | ||
{ | ||
$connectionHelper = new ConnectionHelper(); | ||
$connection = ConnectionManager::get('test'); | ||
\Cake\TestSuite\ConnectionHelper::runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
\Cake\TestSuite\ConnectionHelper::dropTables('test', ['table']); | ||
\Cake\TestSuite\ConnectionHelper::enableQueryLogging(['test']); | ||
\Cake\TestSuite\ConnectionHelper::truncateTables('test', ['table']); | ||
\Cake\TestSuite\ConnectionHelper::addTestAliases(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this prefix also be replaced?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to modify with userland prefixes.