Total Complexity | 59 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Exposer 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 Exposer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Exposer |
||
41 | { |
||
42 | public static function createBuilder($object, ?array $columns = null, ?bool $expose = null, ?bool $protected = null): Builder |
||
43 | { |
||
44 | $expose ??= true; |
||
45 | $protected ??= false; |
||
46 | |||
47 | $builder = new Builder(); |
||
48 | $builder->setColumns(self::parseColumnsRecursive($columns)); |
||
49 | $builder->setExpose($expose); |
||
50 | $builder->setProtected($protected); |
||
51 | $builder->setParent($object); |
||
52 | $builder->setValue($object); |
||
53 | return $builder; |
||
54 | } |
||
55 | |||
56 | private static function getValue(string $string, $value): string |
||
57 | { |
||
58 | return (is_array($value) || is_object($value)) |
||
|
|||
59 | ? sprintfn($string, $value) |
||
60 | : mb_sprintf($string, $value); |
||
61 | } |
||
62 | |||
63 | private static function checkExpose(Builder $builder): void |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return array|false|Model |
||
181 | */ |
||
182 | public static function expose(Builder $builder) |
||
183 | { |
||
184 | $columns = $builder->getColumns(); |
||
185 | |||
186 | $value = $builder->getValue(); |
||
187 | |||
188 | if (is_array($value) || is_object($value)) { |
||
189 | $toParse = []; |
||
190 | if (is_array($value)) { |
||
191 | $toParse = $value; |
||
192 | } |
||
193 | elseif (method_exists($value, 'toArray')) { |
||
194 | $toParse = $value->toArray(); |
||
195 | } |
||
196 | |||
197 | // @TODO fix / refactor this |
||
198 | if (isset($value->dirtyRelated)) { |
||
199 | foreach ($value->dirtyRelated as $dirtyRelatedKey => $dirtyRelated) { |
||
200 | $toParse[$dirtyRelatedKey] = $dirtyRelated ?? false; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | // si aucune column demandé et que l'expose est à false |
||
205 | if (is_null($columns) && !$builder->getExpose()) { |
||
206 | return []; |
||
207 | } |
||
208 | |||
209 | // Prépare l'array de retour des fields de l'instance |
||
210 | $ret = []; |
||
211 | $builder->setContextKey($builder->getFullKey()); |
||
212 | foreach ($toParse as $fieldKey => $fieldValue) { |
||
213 | $builder->setParent($value); |
||
214 | $builder->setKey($fieldKey); |
||
215 | $builder->setValue($fieldValue); |
||
216 | self::checkExpose($builder); |
||
217 | if ($builder->getExpose()) { |
||
218 | $ret [$fieldKey] = self::expose($builder); |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | else { |
||
223 | $ret = $builder->getExpose() ? $value : false; |
||
224 | } |
||
225 | |||
226 | return $ret; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Here to parse the columns parameter into some kind of flatten array with |
||
231 | * the key path separated by dot "my.path" and the value true, false or a callback function |
||
232 | * including the ExposeBuilder object |
||
233 | * |
||
234 | * @param array|null $columns |
||
235 | * @param string|null $context |
||
236 | * |
||
237 | * @return array|null |
||
238 | */ |
||
239 | public static function parseColumnsRecursive(?array $columns = null, ?string $context = null): ?array |
||
287 | } |
||
288 | } |
||
289 |