|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Cake\Upgrade\Rector\Cake5; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\ArrayItem; |
| 8 | +use PhpParser\Node\Expr\Array_; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Identifier; |
| 11 | +use PhpParser\Node\Scalar\String_; |
| 12 | +use PHPStan\Type\ObjectType; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | + |
| 17 | +/** |
| 18 | + * Transforms BreadcrumbsHelper::add(array) to addMany() and prepend(array) to prependMany() |
| 19 | + * |
| 20 | + * In CakePHP 5.3, passing an array of crumbs to add() or prepend() is deprecated. |
| 21 | + * Use addMany() or prependMany() instead. |
| 22 | + * |
| 23 | + * @see https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html |
| 24 | + */ |
| 25 | +final class BreadcrumbsHelperAddManyRector extends AbstractRector |
| 26 | +{ |
| 27 | + public function getRuleDefinition(): RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition( |
| 30 | + 'Change BreadcrumbsHelper::add(array) to addMany() and prepend(array) to prependMany()', |
| 31 | + [ |
| 32 | + new CodeSample( |
| 33 | + <<<'CODE_SAMPLE' |
| 34 | +$this->Breadcrumbs->add([ |
| 35 | + ['title' => 'Home', 'url' => '/'], |
| 36 | + ['title' => 'Articles', 'url' => '/articles'], |
| 37 | +]); |
| 38 | +CODE_SAMPLE |
| 39 | + , |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +$this->Breadcrumbs->addMany([ |
| 42 | + ['title' => 'Home', 'url' => '/'], |
| 43 | + ['title' => 'Articles', 'url' => '/articles'], |
| 44 | +]); |
| 45 | +CODE_SAMPLE, |
| 46 | + ), |
| 47 | + ], |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function getNodeTypes(): array |
| 52 | + { |
| 53 | + return [MethodCall::class]; |
| 54 | + } |
| 55 | + |
| 56 | + public function refactor(Node $node): ?Node |
| 57 | + { |
| 58 | + if (!$node instanceof MethodCall) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + // Must be add or prepend method |
| 63 | + if (!$node->name instanceof Identifier) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + $methodName = $node->name->toString(); |
| 68 | + if ($methodName !== 'add' && $methodName !== 'prepend') { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + // Must have at least one argument |
| 73 | + if (count($node->args) < 1) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + // First argument must be an array |
| 78 | + $firstArg = $node->args[0]->value; |
| 79 | + if (!$firstArg instanceof Array_) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + // Check if the array looks like an array of crumbs (array of arrays) |
| 84 | + // rather than a single crumb with title/url keys |
| 85 | + if (!$this->isArrayOfCrumbs($firstArg)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + // Check if this is called on BreadcrumbsHelper |
| 90 | + $callerType = $this->getType($node->var); |
| 91 | + if (!$callerType instanceof ObjectType) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + if (!$callerType->isInstanceOf('Cake\View\Helper\BreadcrumbsHelper')->yes()) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + // Rename to addMany or prependMany |
| 100 | + $newMethodName = $methodName === 'add' ? 'addMany' : 'prependMany'; |
| 101 | + $node->name = new Identifier($newMethodName); |
| 102 | + |
| 103 | + return $node; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Determine if an array is an array of crumbs (array of arrays) |
| 108 | + * vs a single crumb (array with title/url keys) |
| 109 | + */ |
| 110 | + private function isArrayOfCrumbs(Array_ $array): bool |
| 111 | + { |
| 112 | + // An array of crumbs is typically a numerically indexed array of arrays |
| 113 | + // e.g. [['title' => 'Home'], ['title' => 'Articles']] |
| 114 | + // A single crumb would have string keys like 'title', 'url' |
| 115 | + // e.g. ['title' => 'Home', 'url' => '/'] |
| 116 | + |
| 117 | + if (count($array->items) === 0) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + foreach ($array->items as $item) { |
| 122 | + if (!$item instanceof ArrayItem) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + // If item has a string key like 'title' or 'url', it's a single crumb |
| 127 | + if ($item->key instanceof String_) { |
| 128 | + $keyValue = $item->key->value; |
| 129 | + if ($keyValue === 'title' || $keyValue === 'url' || $keyValue === 'options') { |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // If the item value is an array, it's likely an array of crumbs |
| 135 | + if ($item->value instanceof Array_) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return false; |
| 141 | + } |
| 142 | +} |
0 commit comments