Complex classes like Transformer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Transformer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Transformer |
||
12 | { |
||
13 | /** |
||
14 | * Associative array of data to be processed. |
||
15 | * |
||
16 | * @var Collection |
||
17 | */ |
||
18 | protected $data; |
||
19 | |||
20 | /** |
||
21 | * A pipe separated listing of dot-formatted data keys, used for regex matching of fields. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $dataKeysForRegex = ''; |
||
26 | |||
27 | /** |
||
28 | * Associative array of rules to be applied to the data. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $rules = []; |
||
33 | |||
34 | /** |
||
35 | * An array of rules matched to fields, used during transform only. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $matchedRules = []; |
||
40 | |||
41 | /** |
||
42 | * Flag indicating that rule processing should be halted. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $bail; |
||
47 | |||
48 | /** |
||
49 | * Flag indicating that rule processing should be halted and we should drop the current field. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $drop; |
||
54 | |||
55 | /** |
||
56 | * Index of loaded RulePacks. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $rulePacks = []; |
||
61 | |||
62 | /** |
||
63 | * Index of transformation rules, linking to their parent RulePack. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $ruleMethods = []; |
||
68 | |||
69 | /** |
||
70 | * Track the indices that the current field has during execution of the rules. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $loopIndices = []; |
||
75 | |||
76 | /** |
||
77 | * Transformer constructor. |
||
78 | * |
||
79 | * @param array|string $rulePacks |
||
80 | * @param array $rules |
||
81 | */ |
||
82 | public function __construct($rulePacks = [], $rules = []) |
||
86 | |||
87 | /** |
||
88 | * Set the rules that will be applied to the data. |
||
89 | * |
||
90 | * @param array $rules |
||
91 | * @return self |
||
92 | */ |
||
93 | public function setRules(array $rules = []) : self |
||
103 | |||
104 | /** |
||
105 | * Set the data that rules are to be applied to. |
||
106 | * |
||
107 | * @param array $data |
||
108 | * @return self |
||
109 | */ |
||
110 | public function setData(array $data = []) : self |
||
117 | |||
118 | /** |
||
119 | * Perform the transformation. |
||
120 | * |
||
121 | * @param array $data |
||
122 | * @param array $rules |
||
123 | * @return Collection |
||
124 | */ |
||
125 | public function transform(array $data, array $rules = null) : Collection |
||
137 | |||
138 | /** |
||
139 | * Apply the matched rules to the input. |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | protected function applyRules() : self |
||
153 | |||
154 | /** |
||
155 | * Execute the array of rules. |
||
156 | * |
||
157 | * @param $field |
||
158 | */ |
||
159 | protected function executeRules($field) |
||
192 | |||
193 | /** |
||
194 | * Indicate that the current loop should bail. |
||
195 | * |
||
196 | * @param bool $bail |
||
197 | */ |
||
198 | public function bail(bool $bail = true) |
||
202 | |||
203 | /** |
||
204 | * Indicate that the current field should be dropped. |
||
205 | * |
||
206 | * @param bool $drop |
||
207 | */ |
||
208 | public function drop(bool $drop = true) |
||
212 | |||
213 | /** |
||
214 | * Construct the method name to call, given the name of the rule. |
||
215 | * |
||
216 | * @param $rule |
||
217 | * @return string |
||
218 | * @throws InvalidRule |
||
219 | */ |
||
220 | protected function getRuleMethod($rule) : string |
||
224 | |||
225 | /** |
||
226 | * Check if the current loop should bail. |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | protected function shouldBail() : bool |
||
234 | |||
235 | /** |
||
236 | * Check if the current field should be dropped. |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | protected function shouldDrop() : bool |
||
244 | |||
245 | /** |
||
246 | * Match the loaded rule to fields in the data, based on the $field expression provided. |
||
247 | * |
||
248 | * @return self |
||
249 | */ |
||
250 | protected function matchRulesToFields() : self |
||
266 | |||
267 | /** |
||
268 | * Parse fieldExpression to match all the fields in the data we need to transform. It passes back an array of field |
||
269 | * names with a set of 'indices' associated to each field name (these are where we match wildcards). |
||
270 | * |
||
271 | * @param $fieldExpression |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function findMatchingFields($fieldExpression) : array |
||
290 | |||
291 | /** |
||
292 | * Return a key/value array of rules/parameters. |
||
293 | * |
||
294 | * @param $set |
||
295 | * @return array |
||
296 | */ |
||
297 | protected function parseRuleSet($set) : array |
||
308 | |||
309 | /** |
||
310 | * Split a rule expression into the rule name and any parameters present. |
||
311 | * |
||
312 | * @param $expression |
||
313 | * @return mixed |
||
314 | */ |
||
315 | protected function parseRuleExpression($expression) : array |
||
327 | |||
328 | /** |
||
329 | * @param string $expression |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | protected function parseTextRuleExpression(string $expression): array |
||
342 | |||
343 | /** |
||
344 | * @param $rule |
||
345 | * |
||
346 | * @return string |
||
347 | * @throws InvalidRule |
||
348 | */ |
||
349 | protected function validateRule($rule) : string |
||
357 | |||
358 | /** |
||
359 | * @param $name |
||
360 | * @param $parameters |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function __call($name, $parameters) |
||
372 | |||
373 | /** |
||
374 | * Register multiple rule packs. |
||
375 | * |
||
376 | * @param array $rulePacks |
||
377 | * @return Transformer |
||
378 | */ |
||
379 | public function addRulePacks(array $rulePacks) : self |
||
387 | |||
388 | /** |
||
389 | * Register a rule pack. |
||
390 | * |
||
391 | * @param RulePack|string $rulePack |
||
392 | * @return Transformer |
||
393 | */ |
||
394 | public function addRulePack($rulePack) : self |
||
412 | |||
413 | /** |
||
414 | * Check if the Transformer instance has a given rule pack. |
||
415 | * |
||
416 | * @param RulePack|string $rulePack |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function hasRulePack($rulePack) : bool |
||
425 | |||
426 | /** |
||
427 | * Return an array of all loaded rule packs. |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | public function rulePacks() : array |
||
435 | |||
436 | /** |
||
437 | * Return class name if input is object, otherwise return input. |
||
438 | * |
||
439 | * @param string|object $class |
||
440 | * @return string |
||
441 | */ |
||
442 | protected function getClassName($class) : string |
||
446 | |||
447 | /** |
||
448 | * Parse the parameters that have been passed in and try to find an associated value in the current data |
||
449 | * collection, by replacing wildcards with the indices that are kept for the current loop. |
||
450 | * |
||
451 | * @param $parameter |
||
452 | * @return mixed |
||
453 | */ |
||
454 | public function getValue($parameter) |
||
458 | |||
459 | /** |
||
460 | * Remove the current field from the data array, and merge in the new values. For dot-delimited arrays, remove all |
||
461 | * fields that are being worked on, e.g. when a date array of day, month, year is being combined into a string. |
||
462 | * |
||
463 | * @param string $field |
||
464 | * @param mixed $result |
||
465 | */ |
||
466 | protected function replaceDataValue($field, $result) |
||
474 | } |
||
475 |