Complex classes like Entity 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 Entity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Entity |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $bot_name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get bot name |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | public function getBotName() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Entity constructor. |
||
| 41 | * |
||
| 42 | * @todo Get rid of the $bot_name, it shouldn't be here! |
||
| 43 | * |
||
| 44 | * @param $data |
||
| 45 | * @param string $bot_name |
||
| 46 | * |
||
| 47 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 48 | */ |
||
| 49 | 32 | public function __construct($data, $bot_name = '') |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Perform to json |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | 1 | public function toJson() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Reflect |
||
| 68 | * |
||
| 69 | * @param null $object |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | 1 | public function reflect($object = null) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Perform to string |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function __toString() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Helper to set member variables |
||
| 155 | * |
||
| 156 | * @param array $data |
||
| 157 | */ |
||
| 158 | 32 | protected function assignMemberVariables(array $data) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get the list of the properties that are themselves Entities |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 14 | protected function subEntities() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Perform any special entity validation |
||
| 177 | * |
||
| 178 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 179 | */ |
||
| 180 | 28 | protected function validate() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get a property from the current Entity |
||
| 186 | * |
||
| 187 | * @param mixed $property |
||
| 188 | * @param mixed $default |
||
| 189 | * |
||
| 190 | * @return mixed |
||
| 191 | */ |
||
| 192 | 27 | public function getProperty($property, $default = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Return the variable for the called getter or magically set properties dynamically. |
||
| 203 | * |
||
| 204 | * @param $method |
||
| 205 | * @param $args |
||
| 206 | * |
||
| 207 | * @return mixed|null |
||
| 208 | */ |
||
| 209 | 17 | public function __call($method, $args) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Return an array of nice objects from an array of object arrays |
||
| 242 | * |
||
| 243 | * This method is used to generate pretty object arrays |
||
| 244 | * mainly for PhotoSize and Entities object arrays. |
||
| 245 | * |
||
| 246 | * @param string $class |
||
| 247 | * @param string $property |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 6 | protected function makePrettyObjectArray($class, $property) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * stripMarkDown |
||
| 272 | * Gived a string escape special charactes used in Markdown |
||
| 273 | * |
||
| 274 | * @param string $string |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 4 | public function stripMarkDown($string) |
|
| 286 | } |
||
| 287 |