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() |
||
| 30 | { |
||
| 31 | $modules = []; |
||
| 32 | $domains = $this->route->getDomains(); |
||
| 33 | if (count($domains)) { |
||
| 34 | foreach ($domains as $module => $info) { |
||
| 35 | try { |
||
| 36 | $module = str_replace('/', '', str_replace('@', '', $module)); |
||
| 37 | if (!preg_match('/^ROOT/i', $module)) { |
||
| 38 | $modules[] = [ |
||
| 39 | 'name' => $module, |
||
| 40 | 'path' => realpath($info['template'] . DIRECTORY_SEPARATOR . '..'), |
||
| 41 | ]; |
||
| 42 | } |
||
| 43 | } catch (\Exception $e) { |
||
| 44 | $modules[] = $e->getMessage(); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | return $modules; |
||
| 50 | } |
||
| 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) |
||
| 60 | { |
||
| 61 | $module_path = $module['path'] . DIRECTORY_SEPARATOR . 'Api'; |
||
| 62 | $module_name = $module['name']; |
||
| 63 | $endpoints = []; |
||
| 64 | if (file_exists($module_path)) { |
||
| 65 | $finder = new Finder(); |
||
| 66 | $finder->files()->in($module_path)->depth(0)->name('*.php'); |
||
| 67 | if (count($finder)) { |
||
| 68 | /** @var \SplFileInfo $file */ |
||
| 69 | foreach ($finder as $file) { |
||
| 70 | $namespace = "\\{$module_name}\\Api\\" . str_replace('.php', '', $file->getFilename()); |
||
| 71 | $endpoints[$namespace] = $this->extractApiInfo($namespace, $module_name); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | return $endpoints; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Method that extract all the endpoit information by reflection |
||
| 80 | * |
||
| 81 | * @param string $namespace |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function extractApiInfo($namespace, $module) |
||
| 86 | { |
||
| 87 | $info = []; |
||
| 88 | $reflection = new \ReflectionClass($namespace); |
||
| 89 | foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 90 | try { |
||
| 91 | $mInfo = $this->extractMethodInfo($namespace, $method, $reflection, $module); |
||
| 92 | if (NULL !== $mInfo) { |
||
| 93 | $info[] = $mInfo; |
||
| 94 | } |
||
| 95 | } catch (\Exception $e) { |
||
| 96 | Logger::getInstance()->errorLog($e->getMessage()); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | return $info; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Extract route from doc comments |
||
| 104 | * |
||
| 105 | * @param string $comments |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | protected function extractRoute($comments = '') |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Extract api from doc comments |
||
| 119 | * |
||
| 120 | * @param string $comments |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | protected function extractApi($comments = '') |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Extract api from doc comments |
||
| 134 | * |
||
| 135 | * @param string $comments |
||
| 136 | * |
||
| 137 | * @return boolean |
||
| 138 | */ |
||
| 139 | protected function checkDeprecated($comments = '') |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Extract visibility from doc comments |
||
| 146 | * |
||
| 147 | * @param string $comments |
||
| 148 | * |
||
| 149 | * @return boolean |
||
| 150 | */ |
||
| 151 | protected function extractVisibility($comments = '') |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Method that extract the description for the endpoint |
||
| 164 | * |
||
| 165 | * @param string $comments |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | protected function extractDescription($comments = '') |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Method that extract the type of a variable |
||
| 187 | * |
||
| 188 | * @param string $comments |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public static function extractVarType($comments = '') |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Method that extract the payload for the endpoint |
||
| 206 | * |
||
| 207 | * @param string $model |
||
| 208 | * @param string $comments |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | protected function extractPayload($model, $comments = '') |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Extract all the properties from Dto class |
||
| 226 | * |
||
| 227 | * @param string $class |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function extractDtoProperties($class) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Extract return class for api endpoint |
||
| 244 | * |
||
| 245 | * @param string $model |
||
| 246 | * @param string $comments |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | protected function extractReturn($model, $comments = '') |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Extract all fields from a ActiveResource model |
||
| 281 | * |
||
| 282 | * @param string $namespace |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | protected function extractModelFields($namespace) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Method that extract all the needed info for each method in each API |
||
| 314 | * |
||
| 315 | * @param string $namespace |
||
| 316 | * @param \ReflectionMethod $method |
||
| 317 | * @param \ReflectionClass $reflection |
||
| 318 | * @param string $module |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | protected function extractMethodInfo($namespace, \ReflectionMethod $method, \ReflectionClass $reflection, $module) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Translator from php types to swagger types |
||
| 354 | * @param string $format |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public static function translateSwaggerFormats($format) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Method that parse the definitions for the api's |
||
| 399 | * @param array $endpoint |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public static function extractSwaggerDefinition(array $endpoint) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Method that export |
||
| 445 | * @param array $modules |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public static function swaggerFormatter(array $modules = []) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Method that extract the Dto class for the api documentation |
||
| 475 | * @param string $dto |
||
| 476 | * @param boolean $isArray |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | protected function extractDtoName($dto, $isArray = false) |
||
| 490 | } |
||
| 491 |