Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ parameters:
- '#Class "Rector\\TypeDeclarationDocblocks\\Rector\\ClassMethod\\AddParamArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnArrayDocblockBasedOnArrayMapRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector" is missing @see annotation with test case class reference#'

# @todo fix in phpstan-rules
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 11 additions & 18 deletions rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\Scalar\ScalarValueToConstFetchRector\ScalarValueToConstFetchRectorTest
* @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.
*/
final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
final class ScalarValueToConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var ScalarValueToConstFetch[]
*/
private array $scalarValueToConstFetches;

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -58,19 +54,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ConstFetch|ClassConstFetch|null
{
foreach ($this->scalarValueToConstFetches as $scalarValueToConstFetch) {
if ($node->value === $scalarValueToConstFetch->getScalar()->value) {
return $scalarValueToConstFetch->getConstFetch();
}
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%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',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, ScalarValueToConstFetch::class);

$this->scalarValueToConstFetches = $configuration;
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/ScalarValueToConstFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;

/**
* @api used in deprecated ScalarValueToConstFetchRector configs
*/
final readonly class ScalarValueToConstFetch
{
public function __construct(
Expand Down
Loading