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,60 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'ack:alarm',
description: '',
)]
class WithNamedArgModeDifferentPosition extends Command
{
protected function configure(): void
{
$this
->addOption(
name: 'startDate',
mode: InputOption::VALUE_REQUIRED,
description: 'non empty description needed to reproduce the bug',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
return Command::SUCCESS;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'ack:alarm',
description: '',
)]
class WithNamedArgModeDifferentPosition
{
public function __invoke(
#[\Symfony\Component\Console\Attribute\Option(name: 'startDate', mode: InputOption::VALUE_REQUIRED, description: 'non empty description needed to reproduce the bug')]
$startdate
): int
{
return Command::SUCCESS;
}
}

?>
50 changes: 22 additions & 28 deletions rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand All @@ -32,47 +33,43 @@ public function resolve(ClassMethod $configureClassMethod): array
$commandOptions = [];

foreach ($addOptionMethodCalls as $addOptionMethodCall) {
$addOptionArgs = $addOptionMethodCall->getArgs();
$nameArg = $addOptionMethodCall->getArg('name', 0);
if (! $nameArg instanceof Arg) {
continue;
}

$optionName = $this->valueResolver->getValue($addOptionArgs[0]->value);

$isImplicitBoolean = $this->isImplicitBoolean($addOptionArgs);
$optionName = $this->valueResolver->getValue($nameArg->value);
$isImplicitBoolean = $this->isImplicitBoolean($addOptionMethodCall);

$commandOptions[] = new CommandOption(
$optionName,
$addOptionArgs[0]->value,
$addOptionArgs[1]->value ?? null,
$addOptionArgs[2]->value ?? null,
$addOptionArgs[3]->value ?? null,
$addOptionArgs[4]->value ?? null,
$this->isArrayMode($addOptionArgs),
$nameArg->value,
$addOptionMethodCall->getArg('shortcut', 1)?->value,
$addOptionMethodCall->getArg('mode', 2)?->value,
$addOptionMethodCall->getArg('description', 3)?->value,
$addOptionMethodCall->getArg('default', 4)?->value,
$this->isArrayMode($addOptionMethodCall),
$isImplicitBoolean,
$this->resolveDefaultType($addOptionArgs)
$this->resolveDefaultType($addOptionMethodCall)
);
}

return $commandOptions;
}

/**
* @param Arg[] $args
*/
private function resolveDefaultType(array $args): ?Type
private function resolveDefaultType(MethodCall $methodCall): ?Type
{
$defaultArg = $args[4] ?? null;
if (! $defaultArg instanceof Arg) {
$defaultExpr = $methodCall->getArg('default', 4)?->value;
if (! $defaultExpr instanceof Expr) {
return null;
}

return $this->nodeTypeResolver->getType($defaultArg->value);
return $this->nodeTypeResolver->getType($defaultExpr);
}

/**
* @param Arg[] $args
*/
private function isArrayMode(array $args): bool
private function isArrayMode(MethodCall $methodCall): bool
{
$modeExpr = $args[2]->value ?? null;
$modeExpr = $methodCall->getArg('mode', 2)?->value;
if (! $modeExpr instanceof Expr) {
return false;
}
Expand All @@ -82,12 +79,9 @@ private function isArrayMode(array $args): bool
return (bool) ($modeValue & 8);
}

/**
* @param Arg[] $args
*/
private function isImplicitBoolean(array $args): bool
private function isImplicitBoolean(MethodCall $methodCall): bool
{
$modeExpr = $args[2]->value ?? null;
$modeExpr = $methodCall->getArg('mode', 2)?->value;
if (! $modeExpr instanceof Expr) {
return false;
}
Expand Down