-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathCommandInvokeParamsFactory.php
More file actions
199 lines (157 loc) · 6.45 KB
/
CommandInvokeParamsFactory.php
File metadata and controls
199 lines (157 loc) · 6.45 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony73\NodeFactory;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node;
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 PHPStan\Type\Type;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
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,
private StaticTypeMapper $staticTypeMapper
) {
}
/**
* @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) {
$variableName = $this->createCamelCase($commandArgument->getNameValue());
$argumentParam = new Param(new Variable($variableName));
$this->decorateParamType($argumentParam, $commandArgument);
if ($commandArgument->getDefault() instanceof Expr) {
$argumentParam->default = $commandArgument->getDefault();
}
if ($this->isOptionalArgument($commandArgument)) {
$argumentParam->type = new NullableType($argumentParam->type);
}
// @todo default string, multiple values array
$argumentArgs = [new Arg(value: $commandArgument->getName(), name: new Identifier('name'))];
if ($this->isNonEmptyExpr($commandArgument->getDescription())) {
$argumentArgs[] = new Arg(value: $commandArgument->getDescription(), name: new Identifier(
'description'
));
}
$argumentParam->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified(SymfonyAttribute::COMMAND_ARGUMENT), $argumentArgs),
]);
$argumentParams[] = $argumentParam;
}
return $argumentParams;
}
/**
* @param CommandOption[] $commandOptions
* @return Param[]
*/
private function createOptionParams(array $commandOptions): array
{
$optionParams = [];
foreach ($commandOptions as $commandOption) {
$variableName = $this->createCamelCase($commandOption->getNameValue());
$optionParam = new Param(new Variable($variableName));
if ($commandOption->getDefault() instanceof Expr) {
$optionParam->default = $commandOption->getDefault();
} elseif ($commandOption->isImplicitBoolean()) {
$optionParam->default = new ConstFetch(new Name('false'));
}
$this->decorateParamType($optionParam, $commandOption);
$optionArgs = [new Arg(value: $commandOption->getName(), name: new Identifier('name'))];
if ($this->isNonEmptyExpr($commandOption->getShortcut())) {
$optionArgs[] = new Arg(value: $commandOption->getShortcut(), name: new Identifier('shortcut'));
}
if ($this->isNonEmptyExpr($commandOption->getMode())) {
$optionArgs[] = new Arg(value: $commandOption->getMode(), name: new Identifier('mode'));
}
if ($this->isNonEmptyExpr($commandOption->getDescription())) {
$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;
}
private function createCamelCase(string $value): string
{
// Replace dashes/underscores with spaces
$value = str_replace(['-', '_'], ' ', strtolower($value));
// Capitalize each word, then remove spaces
$value = str_replace(' ', '', ucwords($value));
// Lowercase first character to make it camelCase
return lcfirst($value);
}
private function isOptionalArgument(CommandArgument $commandArgument): bool
{
if (! $commandArgument->getMode() instanceof Expr) {
return true;
}
return $this->valueResolver->isValue($commandArgument->getMode(), 2);
}
private function isNonEmptyExpr(?Expr $expr): bool
{
if (! $expr instanceof Expr) {
return false;
}
if ($this->valueResolver->isNull($expr)) {
return false;
}
return ! $this->valueResolver->isValue($expr, '');
}
private function decorateParamType(
Param $argumentParam,
CommandArgument|CommandOption $commandArgumentOrOption
): void {
if ($commandArgumentOrOption instanceof CommandOption && $commandArgumentOrOption->isImplicitBoolean()) {
$argumentParam->type = new Identifier('bool');
return;
}
if ($commandArgumentOrOption->isArray()) {
$argumentParam->type = new Identifier('array');
return;
}
$defaultType = $commandArgumentOrOption->getDefaultType();
if ($defaultType instanceof Type) {
$paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($defaultType, TypeKind::PARAM);
if ($paramType instanceof Node) {
$argumentParam->type = $paramType;
return;
}
}
// fallback
if ($commandArgumentOrOption instanceof CommandOption) {
return;
}
$argumentParam->type = new Identifier('string');
}
}