Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DocumentorService 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 DocumentorService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class DocumentorService extends Service |
||
| 16 | { |
||
| 17 | const DTO_INTERFACE = '\\PSFS\\base\\dto\\Dto'; |
||
| 18 | const MODEL_INTERFACE = '\\Propel\\Runtime\\ActiveRecord\\ActiveRecordInterface'; |
||
| 19 | /** |
||
| 20 | * @Inyectable |
||
| 21 | * @var \PSFS\base\Router route |
||
| 22 | */ |
||
| 23 | protected $route; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Method that extract all modules |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function getModules() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Method that extract all endpoints for each module |
||
| 54 | * |
||
| 55 | * @param array $module |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function extractApiEndpoints(array $module) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Method that extract all the endpoit information by reflection |
||
| 83 | * |
||
| 84 | * @param string $namespace |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function extractApiInfo($namespace, $module) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Extract route from doc comments |
||
| 109 | * |
||
| 110 | * @param string $comments |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | protected function extractRoute($comments = '') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Extract api from doc comments |
||
| 124 | * |
||
| 125 | * @param string $comments |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | protected function extractApi($comments = '') |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Extract api from doc comments |
||
| 139 | * |
||
| 140 | * @param string $comments |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | protected function checkDeprecated($comments = '') |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Extract visibility from doc comments |
||
| 151 | * |
||
| 152 | * @param string $comments |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | protected function extractVisibility($comments = '') |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Method that extract the description for the endpoint |
||
| 169 | * |
||
| 170 | * @param string $comments |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function extractDescription($comments = '') |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Method that extract the type of a variable |
||
| 192 | * |
||
| 193 | * @param string $comments |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public static function extractVarType($comments = '') |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Method that extract the payload for the endpoint |
||
| 211 | * |
||
| 212 | * @param string $model |
||
| 213 | * @param string $comments |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected function extractPayload($model, $comments = '') |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Extract all the properties from Dto class |
||
| 231 | * |
||
| 232 | * @param string $class |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | protected function extractDtoProperties($class) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Extract return class for api endpoint |
||
| 249 | * |
||
| 250 | * @param string $model |
||
| 251 | * @param string $comments |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | protected function extractReturn($model, $comments = '') |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Extract all fields from a ActiveResource model |
||
| 286 | * |
||
| 287 | * @param string $namespace |
||
| 288 | * |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | protected function extractModelFields($namespace) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Method that extract all the needed info for each method in each API |
||
| 319 | * |
||
| 320 | * @param string $namespace |
||
| 321 | * @param \ReflectionMethod $method |
||
| 322 | * @param \ReflectionClass $reflection |
||
| 323 | * @param string $module |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | protected function extractMethodInfo($namespace, \ReflectionMethod $method, \ReflectionClass $reflection, $module) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Translator from php types to swagger types |
||
| 359 | * @param string $format |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public static function translateSwaggerFormats($format) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Method that parse the definitions for the api's |
||
| 404 | * @param array $endpoint |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public static function extractSwaggerDefinition(array $endpoint) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Method that export |
||
| 450 | * @param array $modules |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public static function swaggerFormatter(array $modules = []) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Method that extract the Dto class for the api documentation |
||
| 480 | * @param string $dto |
||
| 481 | * @param boolean $isArray |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | protected function extractDtoName($dto, $isArray = false) |
||
| 495 | } |
||
| 496 |