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)  | 
            ||
| 186 | |||
| 187 | /**  | 
            ||
| 188 | * Indicate that the current loop should bail.  | 
            ||
| 189 | *  | 
            ||
| 190 | * @param bool $bail  | 
            ||
| 191 | */  | 
            ||
| 192 | public function bail(bool $bail = true)  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Indicate that the current field should be dropped.  | 
            ||
| 199 | *  | 
            ||
| 200 | * @param bool $drop  | 
            ||
| 201 | */  | 
            ||
| 202 | public function drop(bool $drop = true)  | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Construct the method name to call, given the name of the rule.  | 
            ||
| 209 | *  | 
            ||
| 210 | * @param $rule  | 
            ||
| 211 | * @return string  | 
            ||
| 212 | * @throws InvalidRule  | 
            ||
| 213 | */  | 
            ||
| 214 | protected function getRuleMethod($rule) : string  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * Check if the current loop should bail.  | 
            ||
| 221 | *  | 
            ||
| 222 | * @return bool  | 
            ||
| 223 | */  | 
            ||
| 224 | protected function shouldBail() : bool  | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * Check if the current field should be dropped.  | 
            ||
| 231 | *  | 
            ||
| 232 | * @return bool  | 
            ||
| 233 | */  | 
            ||
| 234 | protected function shouldDrop() : bool  | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * Match the loaded rule to fields in the data, based on the $field expression provided.  | 
            ||
| 241 | *  | 
            ||
| 242 | * @return self  | 
            ||
| 243 | */  | 
            ||
| 244 | protected function matchRulesToFields() : self  | 
            ||
| 260 | |||
| 261 | /**  | 
            ||
| 262 | * Parse fieldExpression to match all the fields in the data we need to transform. It passes back an array of field  | 
            ||
| 263 | * names with a set of 'indices' associated to each field name (these are where we match wildcards).  | 
            ||
| 264 | *  | 
            ||
| 265 | * @param $fieldExpression  | 
            ||
| 266 | * @return array  | 
            ||
| 267 | */  | 
            ||
| 268 | protected function findMatchingFields($fieldExpression) : array  | 
            ||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Return a key/value array of rules/parameters.  | 
            ||
| 287 | *  | 
            ||
| 288 | * @param $set  | 
            ||
| 289 | * @return array  | 
            ||
| 290 | */  | 
            ||
| 291 | protected function parseRuleSet($set) : array  | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * Split a rule expression into the rule name and any parameters present.  | 
            ||
| 304 | *  | 
            ||
| 305 | * @param $expression  | 
            ||
| 306 | * @return mixed  | 
            ||
| 307 | */  | 
            ||
| 308 | protected function parseRuleExpression($expression) : array  | 
            ||
| 317 | |||
| 318 | /**  | 
            ||
| 319 | * @param $rule  | 
            ||
| 320 | *  | 
            ||
| 321 | * @return string  | 
            ||
| 322 | * @throws InvalidRule  | 
            ||
| 323 | */  | 
            ||
| 324 | protected function validateRule($rule) : string  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * @param $name  | 
            ||
| 335 | * @param $parameters  | 
            ||
| 336 | * @return mixed  | 
            ||
| 337 | */  | 
            ||
| 338 | public function __call($name, $parameters)  | 
            ||
| 347 | |||
| 348 | /**  | 
            ||
| 349 | * Register multiple rule packs.  | 
            ||
| 350 | *  | 
            ||
| 351 | * @param array $rulePacks  | 
            ||
| 352 | * @return Transformer  | 
            ||
| 353 | */  | 
            ||
| 354 | public function addRulePacks(array $rulePacks) : self  | 
            ||
| 362 | |||
| 363 | /**  | 
            ||
| 364 | * Register a rule pack.  | 
            ||
| 365 | *  | 
            ||
| 366 | * @param RulePack|String $rulePack  | 
            ||
| 367 | * @return Transformer  | 
            ||
| 368 | */  | 
            ||
| 369 | public function addRulePack($rulePack) : self  | 
            ||
| 387 | |||
| 388 | /**  | 
            ||
| 389 | * Check if the Transformer instance has a given rule pack.  | 
            ||
| 390 | *  | 
            ||
| 391 | * @param RulePack|String $rulePack  | 
            ||
| 392 | * @return bool  | 
            ||
| 393 | */  | 
            ||
| 394 | public function hasRulePack($rulePack) : bool  | 
            ||
| 400 | |||
| 401 | /**  | 
            ||
| 402 | * Return an array of all loaded rule packs.  | 
            ||
| 403 | *  | 
            ||
| 404 | * @return array  | 
            ||
| 405 | */  | 
            ||
| 406 | public function rulePacks() : array  | 
            ||
| 410 | |||
| 411 | /**  | 
            ||
| 412 | * Return class name if input is object, otherwise return input.  | 
            ||
| 413 | *  | 
            ||
| 414 | * @param string|object $class  | 
            ||
| 415 | * @return string  | 
            ||
| 416 | */  | 
            ||
| 417 | protected function getClassName($class) : string  | 
            ||
| 421 | |||
| 422 | /**  | 
            ||
| 423 | * Parse the parameters that have been passed in and try to find an associated value in the current data  | 
            ||
| 424 | * collection, by replacing wildcards with the indices that are kept for the current loop.  | 
            ||
| 425 | *  | 
            ||
| 426 | * @param $parameter  | 
            ||
| 427 | * @return mixed  | 
            ||
| 428 | */  | 
            ||
| 429 | public function getValue($parameter)  | 
            ||
| 433 | |||
| 434 | /**  | 
            ||
| 435 | * Remove the current field from the data array, and merge in the new values. For dot-delimited arrays, remove all  | 
            ||
| 436 | * fields that are being worked on, e.g. when a date array of day, month, year is being combined into a string.  | 
            ||
| 437 | *  | 
            ||
| 438 | * @param string $field  | 
            ||
| 439 | * @param mixed $result  | 
            ||
| 440 | */  | 
            ||
| 441 | protected function replaceDataValue($field, $result)  | 
            ||
| 449 | }  | 
            ||
| 450 |