Complex classes like Array2Object 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 Array2Object, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Array2Object |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * createObject |
||
23 | * |
||
24 | * @param string $class class to create object or instance |
||
25 | * @param array $data array of data |
||
26 | * |
||
27 | * @return mixed |
||
28 | * |
||
29 | * @throws \InvalidArgumentException |
||
30 | */ |
||
31 | static public function createObject($class, array $data) |
||
43 | |||
44 | /** |
||
45 | * @param object $object object instance to populate |
||
46 | * @param array $data array of data to apply |
||
47 | * |
||
48 | * @throws \InvalidArgumentException |
||
49 | */ |
||
50 | static public function populate($object, array $data) |
||
70 | |||
71 | /** |
||
72 | * Get array of class properties including parents private properties |
||
73 | * |
||
74 | * @param \ReflectionClass $refClass |
||
75 | * |
||
76 | * @return array|\ReflectionProperty[] |
||
77 | */ |
||
78 | static private function getClassProperties(\ReflectionClass $refClass) |
||
96 | |||
97 | /** |
||
98 | * @param $propertyName |
||
99 | * @param $givenName |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | static private function isSameProperty($propertyName, $givenName) |
||
116 | |||
117 | /** |
||
118 | * Parse a value using given types |
||
119 | * |
||
120 | * @param mixed $value |
||
121 | * @param array $types |
||
122 | * @param \ReflectionClass $context |
||
123 | * |
||
124 | * @return array|bool|float|int|string |
||
125 | */ |
||
126 | static private function parseValue($value, $types, \ReflectionClass $context) |
||
175 | |||
176 | /** |
||
177 | * Convert a array value into a object or array of objects |
||
178 | * |
||
179 | * @param mixed $value |
||
180 | * @param string $type |
||
181 | * @param \ReflectionClass $context |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | static private function valueToObject($value, $type, \ReflectionClass $context) |
||
232 | |||
233 | /** |
||
234 | * @param $name |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | static private function camelize($name) |
||
242 | |||
243 | /** |
||
244 | * @param \ReflectionProperty $property |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | static private function getPropertyTypes(\ReflectionProperty $property) |
||
259 | } |