Skip to content

Commit 75db75e

Browse files
authored
[deprecation] Deprecate ScalarValueToConstFetchRector (#8344)
1 parent 7c1071c commit 75db75e

8 files changed

Lines changed: 15 additions & 121 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ parameters:
469469
- '#Class "Rector\\TypeDeclarationDocblocks\\Rector\\ClassMethod\\AddParamArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#'
470470
- '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#'
471471
- '#Class "Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector" is missing @see annotation with test case class reference#'
472+
- '#Class "Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector" is missing @see annotation with test case class reference#'
472473

473474
# @todo fix in phpstan-rules
474475
-

‎rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/Fixture/skip_non_matching_values.php.inc‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/ScalarValueToConstFetchRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/Source/ClassWithConst.php‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Scalar/ScalarValueToConstFetchRector/config/configured_rule.php‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,19 @@
1212
use PhpParser\Node\Scalar\Float_;
1313
use PhpParser\Node\Scalar\Int_;
1414
use PhpParser\Node\Scalar\String_;
15+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
1516
use Rector\Contract\Rector\ConfigurableRectorInterface;
17+
use Rector\Exception\ShouldNotHappenException;
1618
use Rector\Rector\AbstractRector;
1719
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
1921
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
20-
use Webmozart\Assert\Assert;
2122

2223
/**
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.
2425
*/
25-
final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
26+
final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
2627
{
27-
/**
28-
* @var ScalarValueToConstFetch[]
29-
*/
30-
private array $scalarValueToConstFetches;
31-
3228
public function getRuleDefinition(): RuleDefinition
3329
{
3430
return new RuleDefinition(
@@ -58,19 +54,16 @@ public function getNodeTypes(): array
5854
*/
5955
public function refactor(Node $node): ConstFetch|ClassConstFetch|null
6056
{
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+
));
6861
}
6962

63+
/**
64+
* @param mixed[] $configuration
65+
*/
7066
public function configure(array $configuration): void
7167
{
72-
Assert::allIsAOf($configuration, ScalarValueToConstFetch::class);
73-
74-
$this->scalarValueToConstFetches = $configuration;
7568
}
7669
}

‎rules/Transform/ValueObject/ScalarValueToConstFetch.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use PhpParser\Node\Scalar\Int_;
1111
use PhpParser\Node\Scalar\String_;
1212

13+
/**
14+
* @api used in deprecated ScalarValueToConstFetchRector configs
15+
*/
1316
final readonly class ScalarValueToConstFetch
1417
{
1518
public function __construct(

0 commit comments

Comments
 (0)