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.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,27 @@
namespace Rector\Transform\Rector\Attribute;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest
* @deprecated This rule is deprecated, as only used for 2 cases now covered by dedicated rules: ColumnTypeToTypesConstantRector in rector-doctrine and EmailModeToConstantRector in rector-symfony. Write a custom rule for the exact project case instead.
*/
final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var AttributeKeyToClassConstFetch[]
*/
private array $attributeKeysToClassConstFetches = [];

public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace key value on specific attribute to class constant', [
Expand Down Expand Up @@ -96,89 +82,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->attrGroups === []) {
return null;
}

$hasChanged = false;
foreach ($this->attributeKeysToClassConstFetches as $attributeKeyToClassConstFetch) {
foreach ($node->attrGroups as $attrGroup) {
if ($this->processToClassConstFetch($attrGroup, $attributeKeyToClassConstFetch)) {
$hasChanged = true;
}
}
}

if ($hasChanged) {
return $node;
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as only used for 2 cases now covered by dedicated rules; write a custom rule for the exact project case instead',
self::class
));
}

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

$this->attributeKeysToClassConstFetches = $configuration;
}

private function processToClassConstFetch(
AttributeGroup $attributeGroup,
AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch
): bool {
$hasChanged = false;

foreach ($attributeGroup->attrs as $attribute) {
if (! $this->isName($attribute->name, $attributeKeyToClassConstFetch->getAttributeClass())) {
continue;
}

foreach ($attribute->args as $arg) {
$argName = $arg->name;
if (! $argName instanceof Identifier) {
continue;
}

if (! $this->isName($argName, $attributeKeyToClassConstFetch->getAttributeKey())) {
continue;
}

if ($this->processArg($arg, $attributeKeyToClassConstFetch)) {
$hasChanged = true;
}
}
}

return $hasChanged;
}

private function processArg(Arg $arg, AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch): bool
{
$value = $this->valueResolver->getValue($arg->value);

$constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null;
if ($constName === null) {
return false;
}

$classConstFetch = $this->nodeFactory->createClassConstFetch(
$attributeKeyToClassConstFetch->getConstantClass(),
$constName
);

if (
$arg->value instanceof ClassConstFetch
&& $this->getName($arg->value) === $this->getName($classConstFetch)
) {
return false;
}

$arg->value = $classConstFetch;

return true;
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/AttributeKeyToClassConstFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\Transform\ValueObject;

/**
* @api used in deprecated AttributeKeyToClassConstFetchRector configs
*/
final readonly class AttributeKeyToClassConstFetch
{
/**
Expand Down
Loading