Total Complexity | 49 |
Total Lines | 277 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ConfigMapper 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 ConfigMapper, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
16 | class ConfigMapper |
||
17 | { |
||
18 | private ArgumentResolverFactory $argumentResolverFactory; |
||
19 | |||
20 | public function __construct(ArgumentResolverFactory $argumentResolverFactory = null) |
||
21 | { |
||
22 | if (null === $argumentResolverFactory) { |
||
23 | $argumentResolverFactory = new ArgumentResolverFactory(); |
||
24 | } |
||
25 | $this->argumentResolverFactory = $argumentResolverFactory; |
||
26 | } |
||
27 | |||
28 | public static function make(): static |
||
29 | { |
||
30 | return new static(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Register new custom argument resolver. |
||
35 | * |
||
36 | * @param string $resolverAlias - resolver name |
||
37 | * @param string $resolverClassName - class name of argument resolver |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function registerCustomArgumentResolver(string $resolverAlias, string $resolverClassName): void |
||
42 | { |
||
43 | $this->argumentResolverFactory->addResolver($resolverAlias, $resolverClassName); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @template T |
||
48 | * |
||
49 | * @param class-string<T> $targetClass |
||
|
|||
50 | * |
||
51 | * @throws ReflectionException |
||
52 | * @throws ValidationException |
||
53 | * @throws Resolver\ArgumentResolverException |
||
54 | * @throws Resolver\Parser\SyntaxException |
||
55 | * @return T |
||
56 | * |
||
57 | */ |
||
58 | public function mapFromFile(string $targetClass, string $configFile): object |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @template T |
||
67 | * |
||
68 | * @param class-string<T> $targetClass |
||
69 | * |
||
70 | * @throws ValidationException |
||
71 | * @throws ReflectionException|Resolver\ArgumentResolverException|Resolver\Parser\SyntaxException |
||
72 | * |
||
73 | * @return T |
||
74 | */ |
||
75 | public function map(string $targetClass, array $config): object |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @template T |
||
94 | * |
||
95 | * @param class-string<T> $targetClass |
||
96 | * |
||
97 | * @throws ValidationException |
||
98 | * @throws ReflectionException|Resolver\ArgumentResolverException|Resolver\Parser\SyntaxException |
||
99 | * |
||
100 | * @return T |
||
101 | */ |
||
102 | public function mapFromString(string $targetClass, string $yaml): object |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @throws Resolver\ArgumentResolverException |
||
111 | * @throws Resolver\Parser\SyntaxException |
||
112 | */ |
||
113 | private function getMappingConfig(ClassInfo $classInfo, ?array $config, $parentKey = null): array |
||
114 | { |
||
115 | $mappingConfig = []; |
||
116 | |||
117 | if ($config === null) { |
||
118 | $config = []; |
||
119 | } |
||
120 | |||
121 | foreach ($classInfo->getFields() as $field) { |
||
122 | $fieldName = $field->getName(); |
||
123 | if (!array_key_exists($fieldName, $config)) { |
||
124 | //Skip values that doesn't exist in config file but has a default values |
||
125 | if (!$field->hasDefaultValueResolver()) { |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | //fallback to defaultValueResolver |
||
130 | switch ($field->getDefaultValueResolver()) { |
||
131 | case DefaultValueResolver::PARENT_KEY: |
||
132 | { |
||
133 | $rawValue = $parentKey; |
||
134 | break; |
||
135 | } |
||
136 | case DefaultValueResolver::NESTED_LIST: |
||
137 | { |
||
138 | $rawValue = $config; |
||
139 | $config = []; |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | } else { |
||
144 | $rawValue = $config[$fieldName]; |
||
145 | unset($config[$fieldName]); |
||
146 | } |
||
147 | |||
148 | $mappingConfig[$fieldName] = $this->toArgumentResolver($field, $rawValue); |
||
149 | } |
||
150 | |||
151 | if (!empty($config) && !$classInfo->isIgnoreUnknown()) { |
||
152 | throw new InvalidConfigPathException($config, $classInfo); |
||
153 | } |
||
154 | foreach ($config as $fieldName => $rawValue) { |
||
155 | $mappingConfig[$fieldName] = $this->createArgumentResolverForPrimitive($rawValue); |
||
156 | } |
||
157 | |||
158 | return $mappingConfig; |
||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @param ClassField $field |
||
164 | * @param mixed $rawValue |
||
165 | * |
||
166 | * @throws Resolver\ArgumentResolverException |
||
167 | * @throws Resolver\Parser\SyntaxException |
||
168 | * @return ArgumentResolver |
||
169 | */ |
||
170 | private function toArgumentResolver(ClassField $field, mixed $rawValue): ArgumentResolver |
||
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * @param mixed $rawValue |
||
195 | * |
||
196 | * @throws Resolver\Parser\SyntaxException |
||
197 | * @return ArgumentResolver |
||
198 | */ |
||
199 | private function createArgumentResolverForPrimitive(mixed $rawValue): ArgumentResolver |
||
200 | { |
||
201 | if (is_bool($rawValue) || is_int($rawValue) || is_array($rawValue)) { |
||
202 | $argResolver = new ScalarArgumentResolver($rawValue); |
||
203 | } else { |
||
204 | $argResolver = $this->processExpression($rawValue); |
||
205 | } |
||
206 | |||
207 | return $argResolver; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @throws Resolver\Parser\SyntaxException |
||
212 | */ |
||
213 | private function processExpression(string $expression): ArgumentResolver |
||
214 | { |
||
215 | $parser = new Parser($expression); |
||
216 | |||
217 | return $parser->parse()->toResolver($this->argumentResolverFactory); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @throws Resolver\Parser\SyntaxException |
||
222 | */ |
||
223 | private function doMapArray(array $array): array |
||
237 | } |
||
238 | |||
239 | private function validate(ClassInfo $classInfo, ?array $config, ?array $parent = [], ?ConfigValidationResult $validationResult = null): ConfigValidationResult |
||
293 | } |
||
294 | } |
||
295 |