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 | 55 | 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 | 55 | protected function assignMemberVariables(array $data) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get the list of the properties that are themselves Entities |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 31 | 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 | 51 | 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 | 34 | public function __call($method, $args) |
|
| 210 | { |
||
| 211 | //Convert method to snake_case (which is the name of the property) |
||
| 212 | 34 | $property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); |
|
| 213 | |||
| 214 | 34 | $action = substr($method, 0, 3); |
|
| 215 | 34 | if ($action === 'get') { |
|
| 216 | 34 | $property = $this->getProperty($property_name); |
|
| 217 | |||
| 218 | 34 | if ($property !== null) { |
|
| 219 | //Get all sub-Entities of the current Entity |
||
| 220 | 34 | $sub_entities = $this->subEntities(); |
|
| 221 | |||
| 222 | 34 | if (isset($sub_entities[$property_name])) { |
|
| 223 | 15 | return new $sub_entities[$property_name]($property); |
|
| 224 | } |
||
| 225 | |||
| 226 | 33 | return $property; |
|
| 227 | } |
||
| 228 | 3 | } elseif ($action === 'set') { |
|
| 229 | // Limit setters to specific classes. |
||
| 230 | 3 | if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) { |
|
| 231 | 3 | $this->$property_name = $args[0]; |
|
| 232 | |||
| 233 | 3 | return $this; |
|
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | 21 | return null; |
|
| 238 | } |
||
| 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 |