Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Convert a array value into a object or array of objects |
||
| 173 | * |
||
| 174 | * @param mixed $value |
||
| 175 | * @param string $type |
||
| 176 | * @param \ReflectionClass $context |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | static private function valueToObject($value, $type, \ReflectionClass $context) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $name |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | static private function camelize($name) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param \ReflectionProperty $property |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | static private function getPropertyTypes(\ReflectionProperty $property) |
||
| 262 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: