-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathCommandInvokeParamsFactory.php
More file actions
160 lines (125 loc) · 5.13 KB
/
CommandInvokeParamsFactory.php
File metadata and controls
160 lines (125 loc) · 5.13 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
<?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) {
$variableName = $this->createCamelCase($commandArgument->getNameValue());
$argumentParam = new Param(new Variable($variableName));
if ($commandArgument->isArray()) {
$argumentParam->type = new Identifier('array');
} else {
$argumentParam->type = new Identifier('string');
}
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();
}
$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, '');
}
}