-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Laravel 10 Database Expressions (#98)
* Add rule to convert DB expression casts to method call * Add rule to convert DB expression __toString() calls to method call * Generate docs * Add rule to migrate DB expressions to the Laravel 10 set
- Loading branch information
Showing
12 changed files
with
316 additions
and
0 deletions.
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
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
70 changes: 70 additions & 0 deletions
70
src/Rector/Cast/DatabaseExpressionCastsToMethodCallRector.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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\Cast; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Cast\String_; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCallRector\DatabaseExpressionCastsToMethodCallRectorTest | ||
*/ | ||
final class DatabaseExpressionCastsToMethodCallRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Convert DB Expression string casts to getValue() method calls.', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Facades\DB; | ||
$string = (string) DB::raw('select 1'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Facades\DB; | ||
$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar()); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [String_::class]; | ||
} | ||
|
||
/** | ||
* @param String_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node->expr instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->expr->class, 'Illuminate\Support\Facades\DB')) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->expr->name, 'raw')) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createMethodCall($node->expr, 'getValue', [ | ||
$this->nodeFactory->createMethodCall( | ||
$this->nodeFactory->createStaticCall('Illuminate\Support\Facades\DB', 'connection'), | ||
'getQueryGrammar' | ||
), | ||
]); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Rector/MethodCall/DatabaseExpressionToStringToMethodCallRector.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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\DatabaseExpressionToStringToMethodCallRector\DatabaseExpressionToStringToMethodCallRectorTest | ||
*/ | ||
final class DatabaseExpressionToStringToMethodCallRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Convert DB Expression __toString() calls to getValue() method calls.', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Facades\DB; | ||
$string = DB::raw('select 1')->__toString(); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Facades\DB; | ||
$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar()); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node->name, '__toString')) { | ||
return null; | ||
} | ||
|
||
if (! $node->var instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->var->class, 'Illuminate\Support\Facades\DB')) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->var->name, 'raw')) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createMethodCall($node->var, 'getValue', [ | ||
$this->nodeFactory->createMethodCall( | ||
$this->nodeFactory->createStaticCall('Illuminate\Support\Facades\DB', 'connection'), | ||
'getQueryGrammar' | ||
), | ||
]); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...tabaseExpressionCastsToMethodCallRector/DatabaseExpressionCastsToMethodCallRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class DatabaseExpressionCastsToMethodCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Rector/Cast/DatabaseExpressionCastsToMethodCallRector/Fixture/fixture.php.inc
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,19 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
$string = (string) DB::raw('select 1'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
$string = DB::raw('select 1')->getValue(\Illuminate\Support\Facades\DB::connection()->getQueryGrammar()); | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...s/Rector/Cast/DatabaseExpressionCastsToMethodCallRector/Fixture/skip_other_usages.php.inc
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,9 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
DB::table('users')->where('name', DB::raw('select 1'))->get(); | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Rector/Cast/DatabaseExpressionCastsToMethodCallRector/config/configured_rule.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\Cast\DatabaseExpressionCastsToMethodCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(DatabaseExpressionCastsToMethodCallRector::class); | ||
}; |
28 changes: 28 additions & 0 deletions
28
...ExpressionToStringToMethodCallRector/DatabaseExpressionToStringToMethodCallRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\DatabaseExpressionToStringToMethodCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class DatabaseExpressionToStringToMethodCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Rector/MethodCall/DatabaseExpressionToStringToMethodCallRector/Fixture/fixture.php.inc
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,19 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
$string = DB::raw('select 1')->__toString(); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
$string = DB::raw('select 1')->getValue(\Illuminate\Support\Facades\DB::connection()->getQueryGrammar()); | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...MethodCall/DatabaseExpressionToStringToMethodCallRector/Fixture/skip_other_usages.php.inc
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,9 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Cast\DatabaseExpressionCastsToMethodCall\Fixture; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
DB::table('users')->where('name', DB::raw('select 1'))->get(); | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...Rector/MethodCall/DatabaseExpressionToStringToMethodCallRector/config/configured_rule.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\MethodCall\DatabaseExpressionToStringToMethodCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(DatabaseExpressionToStringToMethodCallRector::class); | ||
}; |