-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathCommandOptionsResolver.php
More file actions
99 lines (82 loc) · 2.75 KB
/
CommandOptionsResolver.php
File metadata and controls
99 lines (82 loc) · 2.75 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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony73\NodeAnalyzer;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Symfony\Symfony73\NodeFinder\MethodCallFinder;
use Rector\Symfony\Symfony73\ValueObject\CommandOption;
final readonly class CommandOptionsResolver
{
public function __construct(
private MethodCallFinder $methodCallFinder,
private ValueResolver $valueResolver,
private NodeTypeResolver $nodeTypeResolver
) {
}
/**
* @return CommandOption[]
*/
public function resolve(ClassMethod $configureClassMethod): array
{
$addOptionMethodCalls = $this->methodCallFinder->find($configureClassMethod, 'addOption');
$commandOptions = [];
foreach ($addOptionMethodCalls as $addOptionMethodCall) {
$addOptionArgs = $addOptionMethodCall->getArgs();
$optionName = $this->valueResolver->getValue($addOptionArgs[0]->value);
$isImplicitBoolean = $this->isImplicitBoolean($addOptionArgs);
$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),
$isImplicitBoolean,
$this->resolveDefaultType($addOptionArgs)
);
}
return $commandOptions;
}
/**
* @param Arg[] $args
*/
private function resolveDefaultType(array $args): ?Type
{
$defaultArg = $args[4] ?? null;
if (! $defaultArg instanceof Arg) {
return null;
}
return $this->nodeTypeResolver->getType($defaultArg->value);
}
/**
* @param Arg[] $args
*/
private function isArrayMode(array $args): bool
{
$modeExpr = $args[2]->value ?? null;
if (! $modeExpr instanceof Expr) {
return false;
}
$modeValue = $this->valueResolver->getValue($modeExpr);
// binary check for InputOption::VALUE_IS_ARRAY
return (bool) ($modeValue & 8);
}
/**
* @param Arg[] $args
*/
private function isImplicitBoolean(array $args): bool
{
$modeExpr = $args[2]->value ?? null;
if (! $modeExpr instanceof Expr) {
return false;
}
$modeValue = $this->valueResolver->getValue($modeExpr);
// binary check for InputOption::VALUE_NONE
return (bool) ($modeValue & 1);
}
}