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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\ConstFetchToClassConstFetch;
use Webmozart\Assert\Assert;

/**
* @see Rector\Tests\Transform\Rector\ConstFetch\ConstFetchToClassConstFetchRector\ConstFetchToClassConstFetchTest
* @deprecated This rule is deprecated, as too niche and never used. A global const name can be reused across unrelated contexts and does not map cleanly to a single class constant. Use a custom rule scoped to your exact case instead.
*/
final class ConstFetchToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
final class ConstFetchToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, ConstFetchToClassConstFetch[]>
*/
private array $constFetchToClassConstsByOldName = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change const fetch to class const fetch', [
Expand All @@ -43,28 +39,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?ClassConstFetch
{
$constName = $this->getName($node);
if ($constName === null) {
return null;
}

foreach ($this->constFetchToClassConstsByOldName[strtolower($constName)] ?? [] as $constFetchToClassConst) {
return $this->nodeFactory->createClassConstFetch(
$constFetchToClassConst->getNewClassName(),
$constFetchToClassConst->getNewConstName()
);
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and never used; use a custom rule scoped to your exact case instead',
self::class
));
}

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

$this->constFetchToClassConstsByOldName = [];
foreach ($configuration as $constFetchToClassConst) {
$this->constFetchToClassConstsByOldName[strtolower($constFetchToClassConst->getOldConstName())][] = $constFetchToClassConst;
}
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/ConstFetchToClassConstFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Rector\Validation\RectorAssert;

/**
* @api kept for deprecated ConstFetchToClassConstFetchRector code sample
*/
final readonly class ConstFetchToClassConstFetch
{
public function __construct(
Expand Down
Loading