|
12 | 12 | use PhpParser\Node\Scalar\Float_; |
13 | 13 | use PhpParser\Node\Scalar\Int_; |
14 | 14 | use PhpParser\Node\Scalar\String_; |
| 15 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
15 | 16 | use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 17 | +use Rector\Exception\ShouldNotHappenException; |
16 | 18 | use Rector\Rector\AbstractRector; |
17 | 19 | use Rector\Transform\ValueObject\ScalarValueToConstFetch; |
18 | 20 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; |
19 | 21 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
20 | | -use Webmozart\Assert\Assert; |
21 | 22 |
|
22 | 23 | /** |
23 | | - * @see \Rector\Tests\Transform\Rector\Scalar\ScalarValueToConstFetchRector\ScalarValueToConstFetchRectorTest |
| 24 | + * @deprecated This rule is deprecated, as the scalar value target is too wide. The very same value can be used in an unrelated context, e.g. an array key, a version number or a message, and get replaced by a constant that does not belong there. Use a custom rule scoped to the exact context instead. |
24 | 25 | */ |
25 | | -final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface |
| 26 | +final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface |
26 | 27 | { |
27 | | - /** |
28 | | - * @var ScalarValueToConstFetch[] |
29 | | - */ |
30 | | - private array $scalarValueToConstFetches; |
31 | | - |
32 | 28 | public function getRuleDefinition(): RuleDefinition |
33 | 29 | { |
34 | 30 | return new RuleDefinition( |
@@ -58,19 +54,16 @@ public function getNodeTypes(): array |
58 | 54 | */ |
59 | 55 | public function refactor(Node $node): ConstFetch|ClassConstFetch|null |
60 | 56 | { |
61 | | - foreach ($this->scalarValueToConstFetches as $scalarValueToConstFetch) { |
62 | | - if ($node->value === $scalarValueToConstFetch->getScalar()->value) { |
63 | | - return $scalarValueToConstFetch->getConstFetch(); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - return null; |
| 57 | + throw new ShouldNotHappenException(sprintf( |
| 58 | + '"%s" rule is deprecated, as the scalar value target is too wide and replaces values in unrelated contexts; use a custom rule scoped to the exact context instead', |
| 59 | + self::class |
| 60 | + )); |
68 | 61 | } |
69 | 62 |
|
| 63 | + /** |
| 64 | + * @param mixed[] $configuration |
| 65 | + */ |
70 | 66 | public function configure(array $configuration): void |
71 | 67 | { |
72 | | - Assert::allIsAOf($configuration, ScalarValueToConstFetch::class); |
73 | | - |
74 | | - $this->scalarValueToConstFetches = $configuration; |
75 | 68 | } |
76 | 69 | } |
0 commit comments