|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Rector\Rule\Internal; |
| 10 | + |
| 11 | +use PhpParser\Node; |
| 12 | +use PhpParser\Node\Expr\Array_; |
| 13 | +use PhpParser\Node\Expr\ArrayItem; |
| 14 | +use PhpParser\Node\Expr\New_; |
| 15 | +use PhpParser\Node\Stmt\ClassMethod; |
| 16 | +use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal This rule is internal, for Ibexa 1st party packages |
| 23 | + */ |
| 24 | +final class RemoveDeprecatedTwigDefinitionsRector extends AbstractRector implements ConfigurableRectorInterface |
| 25 | +{ |
| 26 | + /** @var string|int|true */ |
| 27 | + private $version; |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws \Exception |
| 31 | + */ |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Remove legacy eZ Systems & Ibexa class_alias calls', [new ConfiguredCodeSample( |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | + public function getFunctions(): array |
| 37 | + { |
| 38 | + return [ |
| 39 | + new TwigFunction( |
| 40 | + 'old_function_name', |
| 41 | + null, |
| 42 | + [ |
| 43 | + 'is_safe' => ['html'], |
| 44 | + 'needs_environment' => true, |
| 45 | + 'deprecated' => '4.0', |
| 46 | + 'alternative' => 'new_function_name', |
| 47 | + ] |
| 48 | + ), |
| 49 | + new TwigFunction( |
| 50 | + 'new_function_name', |
| 51 | + null, |
| 52 | + [ |
| 53 | + 'is_safe' => ['html'], |
| 54 | + 'needs_environment' => true, |
| 55 | + ] |
| 56 | + ), |
| 57 | + ]; |
| 58 | + } |
| 59 | +CODE_SAMPLE |
| 60 | + , |
| 61 | + <<<'CODE_SAMPLE' |
| 62 | + public function getFunctions(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + new TwigFunction( |
| 66 | + 'new_function_name', |
| 67 | + null, |
| 68 | + [ |
| 69 | + 'is_safe' => ['html'], |
| 70 | + 'needs_environment' => true, |
| 71 | + ] |
| 72 | + ), |
| 73 | + ]; |
| 74 | + } |
| 75 | +CODE_SAMPLE |
| 76 | + , |
| 77 | + ['var_dump'] |
| 78 | + )]); |
| 79 | + } |
| 80 | + |
| 81 | + public function getNodeTypes(): array |
| 82 | + { |
| 83 | + return [ClassMethod::class]; |
| 84 | + } |
| 85 | + |
| 86 | + public function refactor(Node $node): ?Node |
| 87 | + { |
| 88 | + // Ensure the method is named "getFunctions" |
| 89 | + if (!$node instanceof ClassMethod |
| 90 | + || ( |
| 91 | + !$this->isName($node, 'getFunctions') |
| 92 | + && !$this->isName($node, 'getFilters') |
| 93 | + ) |
| 94 | + ) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + if ($node->stmts === null) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + // Look for the return statement within the method |
| 103 | + foreach ($node->stmts as $stmt) { |
| 104 | + if ($stmt instanceof Node\Stmt\Return_ && $stmt->expr instanceof Array_) { |
| 105 | + $arrayNode = $stmt->expr; |
| 106 | + |
| 107 | + // Filter out TwigFunction declarations with the 'deprecated' option |
| 108 | + $arrayNode->items = array_filter($arrayNode->items, function (?ArrayItem $item) { |
| 109 | + if ($item === null || !$item->value instanceof New_) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /** @var \PhpParser\Node\Expr\New_ $newExpr */ |
| 114 | + $newExpr = $item->value; |
| 115 | + |
| 116 | + // Ensure it's a 'Twig\TwigFunction' instantiation |
| 117 | + if (!$newExpr->class instanceof Node\Name |
| 118 | + || (!$this->isName($newExpr->class, 'Twig\TwigFunction') |
| 119 | + && !$this->isName($newExpr->class, 'Twig\TwigFilter')) |
| 120 | + ) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + // Check if the third argument (options array) contains the 'deprecated' key |
| 125 | + if (isset($newExpr->args[2]) |
| 126 | + && $newExpr->args[2] instanceof Node\Arg |
| 127 | + && $newExpr->args[2]->value instanceof Array_ |
| 128 | + ) { |
| 129 | + $optionsArray = $newExpr->args[2]->value; |
| 130 | + |
| 131 | + foreach ($optionsArray->items as $optionItem) { |
| 132 | + if ($optionItem instanceof ArrayItem && |
| 133 | + $optionItem->key instanceof Node\Scalar\String_ && |
| 134 | + $optionItem->key->value === 'deprecated' && |
| 135 | + $optionItem->value instanceof Node\Scalar\String_ && |
| 136 | + $optionItem->value->value === $this->version |
| 137 | + ) { |
| 138 | + // Skip this item if 'deprecated' is found |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return true; |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return $node; |
| 150 | + } |
| 151 | + |
| 152 | + public function configure(array $configuration): void |
| 153 | + { |
| 154 | + $this->version = $configuration['version'] ?? true; |
| 155 | + } |
| 156 | +} |
0 commit comments