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 |
||
12 | class DocumentorService extends Service |
||
13 | { |
||
14 | const DTO_INTERFACE = '\\PSFS\\base\\dto\\Dto'; |
||
15 | const MODEL_INTERFACE = '\\Propel\\Runtime\\ActiveRecord\\ActiveRecordInterface'; |
||
16 | /** |
||
17 | * @Inyectable |
||
18 | * @var \PSFS\base\Router route |
||
19 | */ |
||
20 | protected $route; |
||
21 | |||
22 | /** |
||
23 | * Method that extract all modules |
||
24 | * @return array |
||
25 | */ |
||
26 | public function getModules() |
||
44 | |||
45 | /** |
||
46 | * Method that extract all endpoints for each module |
||
47 | * |
||
48 | * @param string $module |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | public function extractApiEndpoints($module) |
||
70 | |||
71 | /** |
||
72 | * Method that extract all the endpoit information by reflection |
||
73 | * |
||
74 | * @param string $namespace |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function extractApiInfo($namespace) |
||
99 | |||
100 | /** |
||
101 | * Extract route from doc comments |
||
102 | * |
||
103 | * @param string $comments |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function extractRoute($comments = '') |
||
114 | |||
115 | /** |
||
116 | * Extract method from doc comments |
||
117 | * |
||
118 | * @param string $comments |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function extractMethod($comments = '') |
||
129 | |||
130 | /** |
||
131 | * Extract visibility from doc comments |
||
132 | * |
||
133 | * @param string $comments |
||
134 | * |
||
135 | * @return boolean |
||
136 | */ |
||
137 | protected function extractVisibility($comments = '') |
||
147 | |||
148 | /** |
||
149 | * Method that extract the description for the endpoint |
||
150 | * |
||
151 | * @param string $comments |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | protected function extractDescription($comments = '') |
||
170 | |||
171 | /** |
||
172 | * Method that extract the type of a variable |
||
173 | * |
||
174 | * @param string $comments |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function extractVarType($comments = '') |
||
189 | |||
190 | /** |
||
191 | * Method that extract the payload for the endpoint |
||
192 | * |
||
193 | * @param string $model |
||
194 | * @param string $comments |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function extractPayload($model, $comments = '') |
||
209 | |||
210 | /** |
||
211 | * Extract all the properties from Dto class |
||
212 | * |
||
213 | * @param string $class |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function extractDtoProperties($class) |
||
234 | |||
235 | /** |
||
236 | * Extract return class for api endpoint |
||
237 | * |
||
238 | * @param string $model |
||
239 | * @param string $comments |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function extractReturn($model, $comments = '') |
||
271 | |||
272 | /** |
||
273 | * Extract all fields from a ActiveResource model |
||
274 | * |
||
275 | * @param string $namespace |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | protected function extractModelFields($namespace) |
||
304 | |||
305 | /** |
||
306 | * Method that extract all the needed info for each method in each API |
||
307 | * |
||
308 | * @param string $namespace |
||
309 | * @param \ReflectionMethod $method |
||
310 | * @param \ReflectionClass $reflection |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function extractMethodInfo($namespace, \ReflectionMethod $method, \ReflectionClass $reflection) |
||
343 | |||
344 | /** |
||
345 | * Translator from php types to swagger types |
||
346 | * @param string $format |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public static function translateSwaggerFormats($format) |
||
388 | |||
389 | /** |
||
390 | * Method that parse the definitions for the api's |
||
391 | * @param array $endpoint |
||
392 | * |
||
393 | * @return array |
||
394 | */ |
||
395 | public static function extractSwaggerDefinition(array $endpoint) |
||
434 | |||
435 | /** |
||
436 | * Method that export |
||
437 | * @param array $modules |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | public static function swaggerFormatter(array $modules = []) |
||
464 | |||
465 | /** |
||
466 | * Method that extract the Dto class for the api documentation |
||
467 | * @param string $dto |
||
468 | * @param boolean $isArray |
||
469 | * |
||
470 | * @return string |
||
471 | */ |
||
472 | protected function extractDtoName($dto, $isArray = false) |
||
482 | } |
||
483 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.