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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\CommandConfigureToAttributeRector\Fixture;

use Symfony\Component\Console\Command\Command;

class SkipNonConstantDescriptionCommand extends Command
{
public function __construct(private readonly string $description)
{
}

public function configure(): void
{
$this->setDescription($this->description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -185,10 +190,13 @@ function (Arg $arg): string {
$asCommandAttribute->args = $attributeArgs;
}

$hasChanged = $attributeArgs !== [];

// remove left overs
foreach ((array) $configureClassMethod->stmts as $key => $stmt) {
if ($this->isExpressionVariableThis($stmt)) {
unset($configureClassMethod->stmts[$key]);
$hasChanged = true;
}
}

Expand All @@ -197,11 +205,17 @@ function (Arg $arg): string {
foreach ($node->stmts as $key => $classStmt) {
if ($classStmt === $configureClassMethod) {
unset($node->stmts[$key]);
$hasChanged = true;
break;
}
}
}

// nothing could be extracted (e.g. only non-constant values), leave the class untouched
if (! $hasChanged) {
return null;
}

return $node;
}

Expand Down Expand Up @@ -262,14 +276,49 @@ private function findAndRemoveMethodExpr(ClassMethod $classMethod, string $metho
return null;
}

$expr = $node->getArgs()[0]
$argValue = $node->getArgs()[0]
->value;

// attribute arguments must be constant expressions;
// a runtime value (e.g. $this->description) cannot be inlined, so leave the call in place
if (! $this->isPermittedAttributeValue($argValue)) {
return null;
}

$expr = $argValue;
return $node->var;
});

return $expr;
}

private function isPermittedAttributeValue(Expr $expr): bool
{
if ($expr instanceof Scalar) {
return ! $expr instanceof InterpolatedString;
}

if ($expr instanceof ConstFetch || $expr instanceof ClassConstFetch) {
return true;
}

if ($expr instanceof Array_) {
foreach ($expr->items as $item) {
if ($item->key instanceof Expr && ! $this->isPermittedAttributeValue($item->key)) {
return false;
}

if (! $this->isPermittedAttributeValue($item->value)) {
return false;
}
}

return true;
}

return false;
}

private function isExpressionVariableThis(Stmt $stmt): bool
{
if (! $stmt instanceof Expression) {
Expand Down
Loading