Complex classes like Comparator 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 Comparator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Comparator |
||
| 6 | { |
||
| 7 | private $custom = []; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $temporaryCustom; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $exists; |
||
| 18 | |||
| 19 | public static function addCustom(array $custom) |
||
| 25 | |||
| 26 | private function initialize(array $temporaryCustom) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param $data |
||
| 35 | * @param $structure |
||
| 36 | * @param array $custom |
||
| 37 | * @return StructureDiffInfo |
||
| 38 | */ |
||
| 39 | public static function check($data, $structure, array $custom = []) |
||
| 47 | |||
| 48 | private function compare($data, $structure) |
||
| 60 | |||
| 61 | private function checkTypes($value, array $types) |
||
| 84 | |||
| 85 | private function diffType($data, $structure) |
||
| 91 | |||
| 92 | private function diffSet($data, $set) |
||
| 107 | |||
| 108 | private function diffStructure($data, array $structure) |
||
| 125 | |||
| 126 | private function diffArrayData(array $data, array $structure) |
||
| 155 | |||
| 156 | private function assoc(array $assoc, array $data) |
||
| 169 | |||
| 170 | private function getType($value) |
||
| 174 | |||
| 175 | private function getStructureType(array $structure) |
||
| 188 | |||
| 189 | private function createDiff($key, $message) |
||
| 193 | |||
| 194 | private function processDiff(StructureDiffInfo $diff, $key) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return Comparator |
||
| 203 | */ |
||
| 204 | private static function instance() |
||
| 210 | } |
It seems like you are relying on a variable being defined by an iteration: