Total Complexity | 58 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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->setValue($object); |
||
52 | return $builder; |
||
53 | } |
||
54 | |||
55 | private static function getValue(string $string, $value): string |
||
56 | { |
||
57 | return (is_array($value) || is_object($value)) |
||
|
|||
58 | ? sprintfn($string, $value) |
||
59 | : mb_sprintf($string, $value); |
||
60 | } |
||
61 | |||
62 | 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 | $value = $builder->getValue(); |
||
186 | |||
187 | if (is_array($value) || is_object($value)) { |
||
188 | $toParse = []; |
||
189 | if (is_array($value)) { |
||
190 | $toParse = $value; |
||
191 | } |
||
192 | elseif (method_exists($value, 'toArray')) { |
||
193 | $toParse = $value->toArray(); |
||
194 | } |
||
195 | else { |
||
196 | $toParse = $value; |
||
197 | } |
||
198 | |||
199 | // si aucune column demandé et que l'expose est à false |
||
200 | if (is_null($columns) && !$builder->getExpose()) { |
||
201 | return []; |
||
202 | } |
||
203 | |||
204 | // Prépare l'array de retour des fields de l'instance |
||
205 | $ret = []; |
||
206 | $builder->setContextKey($builder->getFullKey()); |
||
207 | foreach ($toParse as $fieldKey => $fieldValue) { |
||
208 | $builder->setParent($value); |
||
209 | $builder->setKey($fieldKey); |
||
210 | $builder->setValue($fieldValue); |
||
211 | self::checkExpose($builder); |
||
212 | if ($builder->getExpose()) { |
||
213 | $ret [$fieldKey] = self::expose($builder); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | else { |
||
218 | $ret = $builder->getExpose() ? $value : false; |
||
219 | } |
||
220 | |||
221 | return $ret; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Here to parse the columns parameter into some kind of flatten array with |
||
226 | * the key path separated by dot "my.path" and the value true, false or a callback function |
||
227 | * including the ExposeBuilder object |
||
228 | * |
||
229 | * @param array|null $columns |
||
230 | * @param string|null $context |
||
231 | * |
||
232 | * @return array|null |
||
233 | */ |
||
234 | public static function parseColumnsRecursive(?array $columns = null, ?string $context = null): ?array |
||
282 | } |
||
283 | } |
||
284 |