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 | * @param $value |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | 2 | final public function setValue(string $name, $value): bool |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $name |
||
| 77 | * @param $value |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | private function tryMagicSet(string $name, $value): bool |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $name |
||
| 94 | * |
||
| 95 | * @return mixed|null |
||
| 96 | */ |
||
| 97 | 2 | final public function getValue(string $name) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $name |
||
| 116 | * @param $value |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | 2 | final public function setValueByProperty(string $name, $value): bool |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $name |
||
| 134 | * @param $value |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 1 | final public function setValueByMethod(string $name, $value): bool |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $name |
||
| 152 | * |
||
| 153 | * @return mixed|null |
||
| 154 | */ |
||
| 155 | 2 | final public function getValueByMethod(string $name) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $name |
||
| 167 | * |
||
| 168 | * @return mixed|null |
||
| 169 | */ |
||
| 170 | 2 | final public function getValueByProperty(string $name) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $postfix |
||
| 182 | * |
||
| 183 | * @return null|ReflectionMethod |
||
| 184 | */ |
||
| 185 | 2 | final public function getSetterMethod(string $postfix) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $postfix |
||
| 192 | * |
||
| 193 | * @return null|ReflectionMethod |
||
| 194 | */ |
||
| 195 | 3 | final public function getGetterMethod(string $postfix) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $postfix |
||
| 202 | * @param array $prefixe |
||
| 203 | * |
||
| 204 | * @return null|ReflectionMethod |
||
| 205 | */ |
||
| 206 | 4 | final public function getMethod(string $postfix, array $prefixe) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $name |
||
| 220 | * @param array ...$args |
||
| 221 | * |
||
| 222 | * @return mixed|null |
||
| 223 | */ |
||
|
|
|||
| 224 | final public function invokeMethod(string $name, ...$args) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $name |
||
| 236 | * |
||
| 237 | * @return null|ReflectionMethod |
||
| 238 | */ |
||
| 239 | 5 | final public function getMethodByName(string $name) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $name |
||
| 246 | * |
||
| 247 | * @return null|ReflectionProperty |
||
| 248 | */ |
||
| 249 | 4 | final public function getPropertyByName(string $name) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $name |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 5 | final public function hasProperty(string $name): bool |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $name |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 6 | final public function hasMethod(string $name): bool |
|
| 273 | } |
||
| 274 |