Total Complexity | 53 |
Total Lines | 211 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | final class Parser |
||
24 | { |
||
25 | public function __construct( |
||
26 | private readonly ReaderInterface $reader = new AttributeReader() |
||
27 | ) { |
||
28 | } |
||
29 | |||
30 | public function hasCommandAttribute(\ReflectionClass $reflection): bool |
||
34 | } |
||
35 | |||
36 | public function parse(\ReflectionClass $reflection): CommandDefinition |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | public function fillProperties(Command $command, InputInterface $input): void |
||
54 | { |
||
55 | $reflection = new \ReflectionClass($command); |
||
56 | |||
57 | foreach ($reflection->getProperties() as $property) { |
||
58 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
||
59 | if ($attribute === null) { |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | if ($input->hasArgument($attribute->name ?? $property->getName())) { |
||
64 | $property->setValue( |
||
65 | $command, |
||
66 | $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property) |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | foreach ($reflection->getProperties() as $property) { |
||
72 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
||
73 | if ($attribute === null) { |
||
74 | continue; |
||
75 | } |
||
76 | |||
77 | if ($input->hasOption($attribute->name ?? $property->getName())) { |
||
78 | $value = $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property); |
||
79 | |||
80 | if ($value !== null || $this->getPropertyType($property)->allowsNull()) { |
||
81 | $property->setValue($command, $value); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | private function parseArguments(\ReflectionClass $reflection): array |
||
88 | { |
||
89 | $result = []; |
||
90 | $arrayArgument = null; |
||
91 | foreach ($reflection->getProperties() as $property) { |
||
92 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
||
93 | if ($attribute === null) { |
||
94 | continue; |
||
95 | } |
||
96 | |||
97 | $type = $this->getPropertyType($property); |
||
98 | |||
99 | $isOptional = $property->hasDefaultValue() || $type->allowsNull(); |
||
100 | $isArray = $type->getName() === 'array'; |
||
101 | $mode = match (true) { |
||
102 | $isArray && !$isOptional => InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
||
103 | $isArray && $isOptional => InputArgument::IS_ARRAY | InputArgument::OPTIONAL, |
||
104 | $isOptional => InputArgument::OPTIONAL, |
||
105 | default => InputArgument::REQUIRED |
||
106 | }; |
||
107 | |||
108 | $argument = new InputArgument( |
||
109 | name: $attribute->name ?? $property->getName(), |
||
110 | mode: $mode, |
||
111 | description: (string) $attribute->description, |
||
112 | default: $property->hasDefaultValue() ? $property->getDefaultValue() : null, |
||
113 | suggestedValues: $attribute->suggestedValues |
||
114 | ); |
||
115 | |||
116 | if ($arrayArgument !== null && $isArray) { |
||
117 | throw new ConfiguratorException('There must be only one array argument!'); |
||
118 | } |
||
119 | |||
120 | // It must be used at the end of the argument list. |
||
121 | if ($isArray) { |
||
122 | $arrayArgument = $argument; |
||
123 | continue; |
||
124 | } |
||
125 | $result[] = $argument; |
||
126 | } |
||
127 | |||
128 | if ($arrayArgument !== null) { |
||
129 | $result[] = $arrayArgument; |
||
130 | } |
||
131 | |||
132 | return $result; |
||
133 | } |
||
134 | |||
135 | private function parseOptions(\ReflectionClass $reflection): array |
||
172 | } |
||
173 | |||
174 | private function typecast(mixed $value, \ReflectionProperty $property): mixed |
||
175 | { |
||
176 | $type = $property->hasType() ? $property->getType() : null; |
||
177 | |||
178 | if (!$type instanceof \ReflectionNamedType || $value === null) { |
||
179 | return $value; |
||
180 | } |
||
181 | |||
182 | return match ($type->getName()) { |
||
183 | 'int' => (int) $value, |
||
184 | 'string' => (string) $value, |
||
185 | 'bool' => (bool) $value, |
||
186 | 'float' => (float) $value, |
||
187 | 'array' => (array) $value, |
||
188 | default => $value |
||
189 | }; |
||
190 | } |
||
191 | |||
192 | private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return positive-int |
||
223 | */ |
||
224 | private function guessOptionMode(\ReflectionNamedType $type, \ReflectionProperty $property): int |
||
237 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths