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 |
||
| 17 | class DocumentorService extends Service |
||
| 18 | { |
||
| 19 | const DTO_INTERFACE = '\\PSFS\\base\\dto\\Dto'; |
||
| 20 | const MODEL_INTERFACE = '\\Propel\\Runtime\\ActiveRecord\\ActiveRecordInterface'; |
||
| 21 | /** |
||
| 22 | * @Inyectable |
||
| 23 | * @var \PSFS\base\Router route |
||
| 24 | */ |
||
| 25 | protected $route; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Method that extract all modules |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | public function getModules() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Method that extract all endpoints for each module |
||
| 56 | * |
||
| 57 | * @param array $module |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function extractApiEndpoints(array $module) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Method that extract all the endpoit information by reflection |
||
| 85 | * |
||
| 86 | * @param string $namespace |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function extractApiInfo($namespace, $module) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Extract route from doc comments |
||
| 111 | * |
||
| 112 | * @param string $comments |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | protected function extractRoute($comments = '') |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Extract api from doc comments |
||
| 126 | * |
||
| 127 | * @param string $comments |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | protected function extractApi($comments = '') |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Extract api from doc comments |
||
| 141 | * |
||
| 142 | * @param string $comments |
||
| 143 | * |
||
| 144 | * @return boolean |
||
| 145 | */ |
||
| 146 | protected function checkDeprecated($comments = '') |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Extract visibility from doc comments |
||
| 153 | * |
||
| 154 | * @param string $comments |
||
| 155 | * |
||
| 156 | * @return boolean |
||
| 157 | */ |
||
| 158 | protected function extractVisibility($comments = '') |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Method that extract the description for the endpoint |
||
| 171 | * |
||
| 172 | * @param string $comments |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | protected function extractDescription($comments = '') |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Method that extract the type of a variable |
||
| 194 | * |
||
| 195 | * @param string $comments |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public static function extractVarType($comments = '') |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Method that extract the payload for the endpoint |
||
| 213 | * |
||
| 214 | * @param string $model |
||
| 215 | * @param string $comments |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function extractPayload($model, $comments = '') |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Extract all the properties from Dto class |
||
| 233 | * |
||
| 234 | * @param string $class |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function extractDtoProperties($class) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Extract return class for api endpoint |
||
| 251 | * |
||
| 252 | * @param string $model |
||
| 253 | * @param string $comments |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function extractReturn($model, $comments = '') |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Extract all fields from a ActiveResource model |
||
| 288 | * |
||
| 289 | * @param string $namespace |
||
| 290 | * |
||
| 291 | * @return mixed |
||
| 292 | */ |
||
| 293 | protected function extractModelFields($namespace) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Method that extract all the needed info for each method in each API |
||
| 321 | * |
||
| 322 | * @param string $namespace |
||
| 323 | * @param \ReflectionMethod $method |
||
| 324 | * @param \ReflectionClass $reflection |
||
| 325 | * @param string $module |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | protected function extractMethodInfo($namespace, \ReflectionMethod $method, \ReflectionClass $reflection, $module) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Translator from php types to swagger types |
||
| 361 | * @param string $format |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public static function translateSwaggerFormats($format) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Method that parse the definitions for the api's |
||
| 406 | * @param array $endpoint |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public static function extractSwaggerDefinition(array $endpoint) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Method that export |
||
| 452 | * @param array $modules |
||
| 453 | * |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public static function swaggerFormatter(array $modules = []) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Method that extract the Dto class for the api documentation |
||
| 484 | * @param string $dto |
||
| 485 | * @param boolean $isArray |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | protected function extractDtoName($dto, $isArray = false) |
||
| 499 | } |
||
| 500 |