-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathUnifyModelDatesWithCastsRector.php
147 lines (120 loc) · 3.99 KB
/
UnifyModelDatesWithCastsRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\Class_;
use PhpParser\Builder\Property as PropertyBuilder;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeManipulator\ClassInsertManipulator;
use Rector\PhpParser\Node\Value\ValueResolver;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @changelog https://github.com/laravel/framework/pull/32856
*
* @see \RectorLaravel\Tests\Rector\Class_\UnifyModelDatesWithCastsRector\UnifyModelDatesWithCastsRectorTest
*/
final class UnifyModelDatesWithCastsRector extends AbstractRector
{
public function __construct(
private readonly ClassInsertManipulator $classInsertManipulator,
private readonly ValueResolver $valueResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Unify Model $dates property with $casts', [
new CodeSample(
<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $casts = [
'age' => 'integer',
];
protected $dates = ['birthday'];
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $casts = [
'age' => 'integer', 'birthday' => 'datetime',
];
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node)
{
if (! $this->isObjectType($node, new ObjectType('Illuminate\Database\Eloquent\Model'))) {
return null;
}
$datesProperty = $node->getProperty('dates');
if (! $datesProperty instanceof Property) {
return null;
}
$datesPropertyProperty = $datesProperty->props[0];
if (! $datesPropertyProperty->default instanceof Array_) {
return null;
}
$dates = $this->valueResolver->getValue($datesPropertyProperty->default);
if (! is_array($dates)) {
return null;
}
if ($dates === []) {
return null;
}
$castsProperty = $node->getProperty('casts');
// add property $casts if not exists
if (! $castsProperty instanceof Property) {
$castsProperty = $this->createCastsProperty();
$this->classInsertManipulator->addAsFirstMethod($node, $castsProperty);
}
$castsPropertyProperty = $castsProperty->props[0];
if (! $castsPropertyProperty->default instanceof Array_) {
return null;
}
$casts = $this->valueResolver->getValue($castsPropertyProperty->default);
// exclude attributes added in $casts
$missingDates = array_diff($dates, array_keys($casts));
Assert::allString($missingDates);
foreach ($missingDates as $missingDate) {
$castsPropertyProperty->default->items[] = new ArrayItem(
new String_('datetime'),
new String_($missingDate)
);
}
unset($node->stmts[array_search($datesProperty, $node->stmts, true)]);
return null;
}
private function createCastsProperty(): Property
{
$propertyBuilder = new PropertyBuilder('casts');
$propertyBuilder->makeProtected();
$propertyBuilder->setDefault([]);
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
return $property;
}
}