Skip to content

Commit ba2209e

Browse files
committed
add type declaration based on dfeault value type
1 parent 0881d5c commit ba2209e

7 files changed

Lines changed: 46 additions & 12 deletions

File tree

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/DefaultValue/argument_with_default_value.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ArgumentWithDefaultValue
4747
#[\Symfony\Component\Console\Attribute\Argument(name: 'second')]
4848
string $second = 'required value',
4949
#[\Symfony\Component\Console\Attribute\Option(name: 'third')]
50-
$third = 'third value'
50+
string $third = 'third value'
5151
): int
5252
{
5353
}

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/DefaultValue/match_scalar_type.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class MatchScalarType
5151
#[\Symfony\Component\Console\Attribute\Argument(name: 'second')]
5252
float $second = 200.5,
5353
#[\Symfony\Component\Console\Attribute\Option(name: 'third')]
54-
?int $third = 200,
54+
int $third = 200,
5555
#[\Symfony\Component\Console\Attribute\Option(name: 'fourth')]
56-
?float $fourth = 400.5
56+
float $fourth = 400.5
5757
): int
5858
{
5959
}

rules/Symfony73/NodeAnalyzer/CommandArgumentsResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Rector\Symfony\Symfony73\NodeAnalyzer;
66

7-
use PHPStan\Type\Type;
87
use PhpParser\Node\Arg;
98
use PhpParser\Node\Expr;
109
use PhpParser\Node\Stmt\ClassMethod;
10+
use PHPStan\Type\Type;
1111
use Rector\NodeTypeResolver\NodeTypeResolver;
1212
use Rector\PhpParser\Node\Value\ValueResolver;
1313
use Rector\Symfony\Symfony73\NodeFinder\MethodCallFinder;

rules/Symfony73/NodeAnalyzer/CommandOptionsResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Rector\Symfony\Symfony73\NodeAnalyzer;
66

7-
use PHPStan\Type\Type;
87
use PhpParser\Node\Arg;
98
use PhpParser\Node\Stmt\ClassMethod;
9+
use PHPStan\Type\Type;
1010
use Rector\NodeTypeResolver\NodeTypeResolver;
1111
use Rector\PhpParser\Node\Value\ValueResolver;
1212
use Rector\Symfony\Symfony73\NodeFinder\MethodCallFinder;

rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
use PhpParser\Node\Name\FullyQualified;
1414
use PhpParser\Node\NullableType;
1515
use PhpParser\Node\Param;
16+
use PHPStan\Type\Type;
1617
use Rector\PhpParser\Node\Value\ValueResolver;
18+
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
19+
use Rector\StaticTypeMapper\StaticTypeMapper;
1720
use Rector\Symfony\Enum\SymfonyAttribute;
1821
use Rector\Symfony\Symfony73\ValueObject\CommandArgument;
1922
use Rector\Symfony\Symfony73\ValueObject\CommandOption;
@@ -22,6 +25,7 @@
2225
{
2326
public function __construct(
2427
private ValueResolver $valueResolver,
28+
private StaticTypeMapper $staticTypeMapper
2529
) {
2630
}
2731

@@ -50,11 +54,7 @@ private function createArgumentParams(array $commandArguments): array
5054
$variableName = $this->createCamelCase($commandArgument->getNameValue());
5155
$argumentParam = new Param(new Variable($variableName));
5256

53-
if ($commandArgument->isArray()) {
54-
$argumentParam->type = new Identifier('array');
55-
} else {
56-
$argumentParam->type = new Identifier('string');
57-
}
57+
$this->decorateArgumentParamType($argumentParam, $commandArgument);
5858

5959
if ($commandArgument->getDefault() instanceof Expr) {
6060
$argumentParam->default = $commandArgument->getDefault();
@@ -100,6 +100,8 @@ private function createOptionParams(array $commandOptions): array
100100
$optionParam->default = $commandOption->getDefault();
101101
}
102102

103+
$this->decorateParamTypeByDefault($optionParam, $commandOption);
104+
103105
$optionArgs = [new Arg(value: $commandOption->getName(), name: new Identifier('name'))];
104106

105107
if ($this->isNonEmptyExpr($commandOption->getShortcut())) {
@@ -157,4 +159,36 @@ private function isNonEmptyExpr(?Expr $expr): bool
157159

158160
return ! $this->valueResolver->isValue($expr, '');
159161
}
162+
163+
private function decorateArgumentParamType(Param $argumentParam, CommandArgument $commandArgument): void
164+
{
165+
if ($commandArgument->isArray()) {
166+
$argumentParam->type = new Identifier('array');
167+
return;
168+
}
169+
170+
$this->decorateParamTypeByDefault($argumentParam, $commandArgument);
171+
}
172+
173+
private function decorateParamTypeByDefault(
174+
Param $argumentParam,
175+
CommandArgument|CommandOption $commandArgumentOrOption
176+
): void {
177+
$defaultType = $commandArgumentOrOption->getDefaultType();
178+
if ($defaultType instanceof Type) {
179+
$paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($defaultType, TypeKind::PARAM);
180+
181+
if ($paramType instanceof \PhpParser\Node) {
182+
$argumentParam->type = $paramType;
183+
return;
184+
}
185+
}
186+
187+
// fallback
188+
if ($commandArgumentOrOption instanceof CommandOption) {
189+
return;
190+
}
191+
192+
$argumentParam->type = new Identifier('string');
193+
}
160194
}

rules/Symfony73/ValueObject/CommandArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Rector\Symfony\Symfony73\ValueObject;
66

7-
use PHPStan\Type\Type;
87
use PhpParser\Node\Expr;
8+
use PHPStan\Type\Type;
99

1010
final readonly class CommandArgument
1111
{

rules/Symfony73/ValueObject/CommandOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Rector\Symfony\Symfony73\ValueObject;
66

7-
use PHPStan\Type\Type;
87
use PhpParser\Node\Expr;
8+
use PHPStan\Type\Type;
99

1010
final readonly class CommandOption
1111
{

0 commit comments

Comments
 (0)