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 |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $name |
||
| 97 | * |
||
| 98 | * @return mixed|null |
||
| 99 | */ |
||
| 100 | 2 | final public function getValue(string $name) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $name |
||
| 119 | * @param $value |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 2 | final public function setValueByProperty(string $name, $value): bool |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $name |
||
| 137 | * @param $value |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | 1 | final public function setValueByMethod(string $name, $value): bool |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $name |
||
| 155 | * |
||
| 156 | * @return mixed|null |
||
| 157 | */ |
||
| 158 | 2 | final public function getValueByMethod(string $name) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $name |
||
| 170 | * |
||
| 171 | * @return mixed|null |
||
| 172 | */ |
||
| 173 | 2 | final public function getValueByProperty(string $name) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $postfix |
||
| 185 | * |
||
| 186 | * @return null|ReflectionMethod |
||
| 187 | */ |
||
| 188 | 2 | final public function getSetterMethod(string $postfix) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $postfix |
||
| 195 | * |
||
| 196 | * @return null|ReflectionMethod |
||
| 197 | */ |
||
| 198 | 3 | final public function getGetterMethod(string $postfix) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $postfix |
||
| 205 | * @param array $prefixe |
||
| 206 | * |
||
| 207 | * @return null|ReflectionMethod |
||
| 208 | */ |
||
| 209 | 4 | final public function getMethod(string $postfix, array $prefixe) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $name |
||
| 223 | * @param array ...$args |
||
| 224 | * |
||
| 225 | * @return mixed|null |
||
| 226 | */ |
||
|
|
|||
| 227 | final public function invokeMethod(string $name, ...$args) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $name |
||
| 239 | * |
||
| 240 | * @return null|ReflectionMethod |
||
| 241 | */ |
||
| 242 | 5 | final public function getMethodByName(string $name) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $name |
||
| 249 | * |
||
| 250 | * @return null|ReflectionProperty |
||
| 251 | */ |
||
| 252 | 4 | final public function getPropertyByName(string $name) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $name |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | 5 | final public function hasProperty(string $name): bool |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $name |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 6 | final public function hasMethod(string $name): bool |
|
| 276 | } |
||
| 277 |