-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Laravel110] Adds ModelCastsPropertyToCastsMethodRector rule (#173)
* Adds ModelCastsPropertyToCastsMethodRector rule * Set method to protected instead of public
- Loading branch information
Showing
12 changed files
with
254 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\Class_\ModelCastsPropertyToCastsMethodRector; | ||
|
||
// see https://laravel.com/docs/11.x/upgrade | ||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../config.php'); | ||
|
||
// https://github.com/laravel/framework/pull/47237 | ||
$rectorConfig->rule(ModelCastsPropertyToCastsMethodRector::class); | ||
}; |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Set\LaravelLevelSetList; | ||
use RectorLaravel\Set\LaravelSetList; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->sets([LaravelSetList::LARAVEL_110, LaravelLevelSetList::UP_TO_LARAVEL_100]); | ||
}; |
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
96 changes: 96 additions & 0 deletions
96
src/Rector/Class_/ModelCastsPropertyToCastsMethodRector.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,96 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\Class_; | ||
|
||
use PhpParser\BuilderFactory; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector\ModelCastsPropertyToCastsMethodRectorTest | ||
*/ | ||
class ModelCastsPropertyToCastsMethodRector extends AbstractRector | ||
{ | ||
public function __construct(protected BuilderFactory $builderFactory) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Refactor Model $casts property with casts() method', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Database\Eloquent\Model; | ||
class Person extends Model | ||
{ | ||
protected $casts = [ | ||
'age' => 'integer', | ||
]; | ||
} | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Database\Eloquent\Model; | ||
class Person extends Model | ||
{ | ||
protected function casts(): array | ||
{ | ||
return [ | ||
'age' => 'integer', | ||
]; | ||
} | ||
} | ||
CODE_SAMPLE, | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Class_ | ||
{ | ||
// Check if it is a Model | ||
if (! $this->isObjectType($node, new ObjectType('Illuminate\Database\Eloquent\Model'))) { | ||
|
||
return null; | ||
} | ||
|
||
// Check if there is already a casts() method | ||
foreach ($node->stmts as $stmt) { | ||
if ($stmt instanceof ClassMethod && $this->isName($stmt, 'casts')) { | ||
return null; | ||
} | ||
} | ||
|
||
// Check if there is a protected $casts property | ||
foreach ($node->stmts as $index => $stmt) { | ||
if ($stmt instanceof Property && ($this->isName($stmt, 'casts') && $stmt->isProtected())) { | ||
$method = $this->builderFactory->method('casts') | ||
->setReturnType('array') | ||
->makeProtected(); | ||
// convert the property to a return statement | ||
$method->addStmt(new Return_($stmt->props[0]->default)); | ||
unset($node->stmts[$index]); | ||
$node->stmts[] = $method->getNode(); | ||
|
||
return $node; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
tests/Rector/Class_/ModelCastsPropertyToCastsMethodRector/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,34 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector\Fixture; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CastsPropertyExists extends Model | ||
{ | ||
protected $casts = [ | ||
'birthday' => 'datetime', | ||
'age' => 'integer', | ||
]; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector\Fixture; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CastsPropertyExists extends Model | ||
{ | ||
protected function casts(): array | ||
{ | ||
return [ | ||
'birthday' => 'datetime', | ||
'age' => 'integer', | ||
]; | ||
} | ||
} | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...Class_/ModelCastsPropertyToCastsMethodRector/Fixture/skip_class_with_casts_method.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,22 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector\Fixture; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ClassWithCastsMethod extends Model | ||
{ | ||
protected $casts = [ | ||
'name' => 'string', | ||
]; | ||
|
||
private function casts(): array | ||
{ | ||
return [ | ||
'birthday' => 'datetime', | ||
'age' => 'integer', | ||
]; | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...ector/Class_/ModelCastsPropertyToCastsMethodRector/Fixture/skip_non_model_classes.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,13 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector\Fixture; | ||
|
||
class NonModelClass | ||
{ | ||
protected $casts = [ | ||
'birthday' => 'datetime', | ||
'age' => 'integer', | ||
]; | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...lass_/ModelCastsPropertyToCastsMethodRector/ModelCastsPropertyToCastsMethodRectorTest.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\Class_\ModelCastsPropertyToCastsMethodRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ModelCastsPropertyToCastsMethodRectorTest extends AbstractRectorTestCase | ||
{ | ||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/Class_/ModelCastsPropertyToCastsMethodRector/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\Class_\ModelCastsPropertyToCastsMethodRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(ModelCastsPropertyToCastsMethodRector::class); | ||
}; |