-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathCommandInvokeParamsFactory.php
More file actions
118 lines (93 loc) · 3.9 KB
/
CommandInvokeParamsFactory.php
File metadata and controls
118 lines (93 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony73\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Symfony\Enum\SymfonyAttribute;
use Rector\Symfony\Symfony73\ValueObject\CommandArgument;
use Rector\Symfony\Symfony73\ValueObject\CommandOption;
final readonly class CommandInvokeParamsFactory
{
public function __construct(
private ValueResolver $valueResolver
) {
}
/**
* @param CommandArgument[] $commandArguments
* @param CommandOption[] $commandOptions
* @return Param[]
*/
public function createParams(array $commandArguments, array $commandOptions): array
{
$argumentParams = $this->createArgumentParams($commandArguments);
$optionParams = $this->createOptionParams($commandOptions);
return array_merge($argumentParams, $optionParams);
}
/**
* @param CommandArgument[] $commandArguments
* @return Param[]
*/
private function createArgumentParams(array $commandArguments): array
{
$argumentParams = [];
foreach ($commandArguments as $commandArgument) {
$argumentName = (string) $this->valueResolver->getValue($commandArgument->getName());
$variableName = str_replace('-', '_', $argumentName);
$argumentParam = new Param(new Variable($variableName));
$argumentParam->type = new Identifier('string');
$modeValue = $this->valueResolver->getValue($commandArgument->getMode());
if ($modeValue === null || $modeValue === 2) {
$argumentParam->type = new NullableType($argumentParam->type);
}
// @todo fill type or default value
// @todo default string, multiple values array
$argumentParam->attrGroups[] = new AttributeGroup([
new Attribute(
new FullyQualified(SymfonyAttribute::COMMAND_ARGUMENT),
[
new Arg(value: $commandArgument->getName(), name: new Identifier('name')),
new Arg(value: $commandArgument->getDescription(), name: new Identifier('description')),
]
),
]);
$argumentParams[] = $argumentParam;
}
return $argumentParams;
}
/**
* @param CommandOption[] $commandOptions
* @return Param[]
*/
private function createOptionParams(array $commandOptions): array
{
$optionParams = [];
foreach ($commandOptions as $commandOption) {
$optionName = $commandOption->getStringName();
$variableName = str_replace('-', '_', $optionName);
$optionParam = new Param(new Variable($variableName));
$optionArgs = [new Arg(value: $commandOption->getName(), name: new Identifier('name'))];
if ($commandOption->getShortcut() instanceof Expr) {
$optionArgs[] = new Arg(value: $commandOption->getShortcut(), name: new Identifier('shortcut'));
}
if ($commandOption->getMode() instanceof Expr) {
$optionArgs[] = new Arg(value: $commandOption->getMode(), name: new Identifier('mode'));
}
if ($commandOption->getDescription() instanceof Expr) {
$optionArgs[] = new Arg(value: $commandOption->getDescription(), name: new Identifier('description'));
}
$optionParam->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified(SymfonyAttribute::COMMAND_OPTION), $optionArgs),
]);
$optionParams[] = $optionParam;
}
return $optionParams;
}
}