|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RectorLaravel\Rector\Class_; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\Attribute; |
| 8 | +use PhpParser\Node\AttributeGroup; |
| 9 | +use PhpParser\Node\Expr\Array_; |
| 10 | +use PhpParser\Node\Expr\ArrayItem; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\Name\FullyQualified; |
| 13 | +use PhpParser\Node\Scalar; |
| 14 | +use PhpParser\Node\Scalar\String_; |
| 15 | +use PhpParser\Node\Stmt\Class_; |
| 16 | +use PhpParser\Node\Stmt\Property; |
| 17 | +use PHPStan\Type\ObjectType; |
| 18 | +use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see RectorLaravel\Tests\Rector\Class_\LivewireComponentQueryStringToUrlAttributeRector\LivewireComponentQueryStringToUrlAttributeRectorTest |
| 25 | + */ |
| 26 | +final class LivewireComponentQueryStringToUrlAttributeRector extends AbstractRector |
| 27 | +{ |
| 28 | + private const URL_ATTRIBUTE = 'Livewire\Attributes\Url'; |
| 29 | + |
| 30 | + private const COMPONENT_CLASS = 'Livewire\Component'; |
| 31 | + |
| 32 | + private const QUERY_STRING_PROPERTY_NAME = 'queryString'; |
| 33 | + |
| 34 | + public function __construct(private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer) |
| 35 | + { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public function getRuleDefinition(): RuleDefinition |
| 40 | + { |
| 41 | + return new RuleDefinition( |
| 42 | + 'Converts the $queryString property of a Livewire component to use the Url Attribute', |
| 43 | + [ |
| 44 | + new CodeSample(<<<'CODE_SAMPLE' |
| 45 | +use Livewire\Component; |
| 46 | +
|
| 47 | +class MyComponent extends Component |
| 48 | +{ |
| 49 | + public string $something = ''; |
| 50 | +
|
| 51 | + public string $another = ''; |
| 52 | +
|
| 53 | + protected $queryString = [ |
| 54 | + 'something', |
| 55 | + 'another', |
| 56 | + ]; |
| 57 | +} |
| 58 | +CODE_SAMPLE, |
| 59 | + <<<'CODE_SAMPLE' |
| 60 | +use Livewire\Component; |
| 61 | +
|
| 62 | +class MyComponent extends Component |
| 63 | +{ |
| 64 | + #[\Livewire\Attributes\Url] |
| 65 | + public string $something = ''; |
| 66 | +
|
| 67 | + #[\Livewire\Attributes\Url] |
| 68 | + public string $another = ''; |
| 69 | +} |
| 70 | +CODE_SAMPLE |
| 71 | + ), |
| 72 | + ] |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public function getNodeTypes(): array |
| 77 | + { |
| 78 | + return [Class_::class]; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param Class_ $node |
| 83 | + */ |
| 84 | + public function refactor(Node $node): ?Class_ |
| 85 | + { |
| 86 | + if (! $this->isObjectType($node, new ObjectType(self::COMPONENT_CLASS))) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + $queryStringProperty = null; |
| 91 | + |
| 92 | + foreach ($node->stmts as $stmt) { |
| 93 | + if ($stmt instanceof Property && $this->isName($stmt, self::QUERY_STRING_PROPERTY_NAME)) { |
| 94 | + $queryStringProperty = $stmt; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (! $queryStringProperty instanceof Property) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + // find the properties and add the attribute |
| 103 | + $urlPropertyNames = $this->findQueryStringProperties($queryStringProperty); |
| 104 | + |
| 105 | + if ($urlPropertyNames === []) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + $propertyNodes = []; |
| 110 | + |
| 111 | + foreach ($node->stmts as $stmt) { |
| 112 | + if ($stmt instanceof Property && $this->isNames($stmt, array_keys((array) $urlPropertyNames))) { |
| 113 | + $propertyNodes[] = $stmt; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + foreach ($propertyNodes as $propertyNode) { |
| 118 | + $args = $urlPropertyNames[$this->getName($propertyNode)] ?? []; |
| 119 | + $this->addUrlAttributeToProperty($propertyNode, $args); |
| 120 | + } |
| 121 | + |
| 122 | + // remove the query string property if now empty |
| 123 | + $this->attemptQueryStringRemoval($node, $queryStringProperty); |
| 124 | + |
| 125 | + return $node; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return array<string, Node\Arg[]>|null |
| 130 | + */ |
| 131 | + private function findQueryStringProperties(Property $property): ?array |
| 132 | + { |
| 133 | + if ($property->props === []) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + $array = $property->props[0]->default; |
| 138 | + |
| 139 | + if (! $array instanceof Array_ || $array->items === []) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + $properties = []; |
| 144 | + $toFilter = []; |
| 145 | + |
| 146 | + foreach ($array->items as $item) { |
| 147 | + if ($item === null) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if ($item->key instanceof String_ && $item->value instanceof Array_) { |
| 152 | + $args = $this->processArrayOptionsIntoArgs($item->value); |
| 153 | + |
| 154 | + if ($args === null) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + $properties[$item->key->value] = $args; |
| 159 | + $toFilter[] = $item; |
| 160 | + |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + if ($item->value instanceof String_) { |
| 165 | + $properties[$item->value->value] = []; |
| 166 | + $toFilter[] = $item; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if ($properties === []) { |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + // we remove the array properties which will be converted |
| 175 | + $array->items = array_filter( |
| 176 | + $array->items, |
| 177 | + fn (?ArrayItem $arrayItem): bool => ! in_array($arrayItem, $toFilter, true), |
| 178 | + ); |
| 179 | + |
| 180 | + return $properties; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @param Node\Arg[] $args |
| 185 | + */ |
| 186 | + private function addUrlAttributeToProperty(Property $property, array $args): void |
| 187 | + { |
| 188 | + if ($this->phpAttributeAnalyzer->hasPhpAttribute($property, self::URL_ATTRIBUTE)) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + $property->attrGroups[] = new AttributeGroup([ |
| 193 | + new Attribute( |
| 194 | + new FullyQualified(self::URL_ATTRIBUTE), args: $args |
| 195 | + ), |
| 196 | + ]); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @return Node\Arg[]|null |
| 201 | + */ |
| 202 | + private function processArrayOptionsIntoArgs(Array_ $array): ?array |
| 203 | + { |
| 204 | + $args = []; |
| 205 | + |
| 206 | + foreach ($array->items as $item) { |
| 207 | + if ($item === null) { |
| 208 | + continue; |
| 209 | + } |
| 210 | + if ($item->key instanceof String_ && $item->value instanceof Scalar && in_array($item->key->value, ['except', 'as'], true)) { |
| 211 | + $args[] = new Arg($item->value, name: new Identifier($item->key->value)); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if (count($args) !== count($array->items)) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + |
| 219 | + return $args; |
| 220 | + } |
| 221 | + |
| 222 | + private function attemptQueryStringRemoval(Class_ $class, Property $property): void |
| 223 | + { |
| 224 | + $array = $property->props[0]->default; |
| 225 | + |
| 226 | + if ($array instanceof Array_ && $array->items === []) { |
| 227 | + $class->stmts = array_filter($class->stmts, fn (Node $node) => $node !== $property); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments