Complex classes like ObjectFacade 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 ObjectFacade, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ObjectFacade |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var object |
||
| 19 | */ |
||
| 20 | private $object; |
||
| 21 | /** |
||
| 22 | * @var ReflectionClass |
||
| 23 | */ |
||
| 24 | private $reflection; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * ObjectFacade constructor. |
||
| 28 | * |
||
| 29 | * @param object $object |
||
| 30 | */ |
||
| 31 | 10 | public function __construct($object) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return object |
||
| 40 | */ |
||
| 41 | 3 | final public function getObject() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @return ReflectionClass |
||
| 48 | */ |
||
| 49 | 10 | final public function getReflection(): ReflectionClass |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $name |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | 2 | private function getNameVariations(string $name): array |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $name |
||
| 74 | * @param $value |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 2 | final public function setValue(string $name, $value): bool |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $name |
||
| 91 | * @param $value |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | private function tryMagicSet(string $name, $value): bool |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $name |
||
| 108 | * |
||
| 109 | * @return mixed|null |
||
| 110 | */ |
||
| 111 | 2 | final public function getValue(string $name) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $name |
||
| 130 | * @param $value |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 2 | final public function setValueByProperty(string $name, $value): bool |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $name |
||
| 148 | * @param $value |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 1 | final public function setValueByMethod(string $name, $value): bool |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $name |
||
| 166 | * |
||
| 167 | * @return mixed|null |
||
| 168 | */ |
||
| 169 | 2 | final public function getValueByMethod(string $name) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $name |
||
| 181 | * |
||
| 182 | * @return mixed|null |
||
| 183 | */ |
||
| 184 | 2 | final public function getValueByProperty(string $name) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $postfix |
||
| 196 | * |
||
| 197 | * @return null|ReflectionMethod |
||
| 198 | */ |
||
| 199 | 2 | final public function getSetterMethod(string $postfix) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $postfix |
||
| 206 | * |
||
| 207 | * @return null|ReflectionMethod |
||
| 208 | */ |
||
| 209 | 3 | final public function getGetterMethod(string $postfix) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $postfix |
||
| 216 | * @param array $prefixe |
||
| 217 | * |
||
| 218 | * @return null|ReflectionMethod |
||
| 219 | */ |
||
| 220 | 4 | final public function getMethod(string $postfix, array $prefixe) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $name |
||
| 234 | * @param array ...$args |
||
| 235 | * |
||
| 236 | * @return mixed|null |
||
| 237 | */ |
||
|
|
|||
| 238 | final public function invokeMethod(string $name, ...$args) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $name |
||
| 250 | * |
||
| 251 | * @return null|ReflectionMethod |
||
| 252 | */ |
||
| 253 | 5 | final public function getMethodByName(string $name) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $name |
||
| 260 | * |
||
| 261 | * @return null|ReflectionProperty |
||
| 262 | */ |
||
| 263 | 4 | final public function getPropertyByName(string $name) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $name |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 5 | final public function hasProperty(string $name): bool |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $name |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 6 | final public function hasMethod(string $name): bool |
|
| 287 | } |
||
| 288 |