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 |
||
21 | class DocumentorService extends Service |
||
22 | { |
||
23 | public static $nativeMethods = [ |
||
24 | 'modelList', // Api list |
||
25 | 'get', // Api get |
||
26 | 'post', // Api post |
||
27 | 'put', // Api put |
||
28 | 'delete', // Api delete |
||
29 | ]; |
||
30 | |||
31 | const DTO_INTERFACE = '\\PSFS\\base\\dto\\Dto'; |
||
32 | const MODEL_INTERFACE = '\\Propel\\Runtime\\ActiveRecord\\ActiveRecordInterface'; |
||
33 | |||
34 | private $classes = []; |
||
|
|||
35 | |||
36 | /** |
||
37 | * @Injectable |
||
38 | * @var \PSFS\base\Router route |
||
39 | */ |
||
40 | protected $route; |
||
41 | |||
42 | /** |
||
43 | * Method that extract all modules |
||
44 | * @param string $requestModule |
||
45 | * @return array |
||
46 | */ |
||
47 | public function getModules($requestModule) |
||
48 | { |
||
49 | $modules = []; |
||
50 | $domains = $this->route->getDomains(); |
||
51 | if (count($domains)) { |
||
52 | foreach ($domains as $module => $info) { |
||
53 | try { |
||
54 | $module = preg_replace('/(@|\/)/', '', $module); |
||
55 | if (!preg_match('/^ROOT/i', $module) && $module == $requestModule) { |
||
56 | $modules = [ |
||
57 | 'name' => $module, |
||
58 | 'path' => realpath($info['template'] . DIRECTORY_SEPARATOR . '..'), |
||
59 | ]; |
||
60 | } |
||
61 | } catch (\Exception $e) { |
||
62 | $modules[] = $e->getMessage(); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $modules; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Method that extract all endpoints for each module |
||
72 | * |
||
73 | * @param array $module |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function extractApiEndpoints(array $module) |
||
78 | { |
||
79 | $module_path = $module['path'] . DIRECTORY_SEPARATOR . 'Api'; |
||
80 | $module_name = $module['name']; |
||
81 | $endpoints = []; |
||
82 | if (file_exists($module_path)) { |
||
83 | $finder = new Finder(); |
||
84 | $finder->files()->in($module_path)->depth(0)->name('*.php'); |
||
85 | if (count($finder)) { |
||
86 | /** @var \SplFileInfo $file */ |
||
87 | foreach ($finder as $file) { |
||
88 | $namespace = "\\{$module_name}\\Api\\" . str_replace('.php', '', $file->getFilename()); |
||
89 | $info = $this->extractApiInfo($namespace, $module_name); |
||
90 | if (!empty($info)) { |
||
91 | $endpoints[$namespace] = $info; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | return $endpoints; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Method that extract all the endpoit information by reflection |
||
101 | * |
||
102 | * @param string $namespace |
||
103 | * @param string $module |
||
104 | * @return array |
||
105 | */ |
||
106 | public function extractApiInfo($namespace, $module) |
||
107 | { |
||
108 | $info = []; |
||
109 | if (Router::exists($namespace) && !I18nHelper::checkI18Class($namespace)) { |
||
110 | $reflection = new \ReflectionClass($namespace); |
||
111 | foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
||
112 | try { |
||
113 | $mInfo = $this->extractMethodInfo($namespace, $method, $reflection, $module); |
||
114 | if (NULL !== $mInfo) { |
||
115 | $info[] = $mInfo; |
||
116 | } |
||
117 | } catch (\Exception $e) { |
||
118 | Logger::getInstance()->errorLog($e->getMessage()); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | return $info; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Extract route from doc comments |
||
127 | * |
||
128 | * @param string $comments |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function extractRoute($comments = '') |
||
133 | { |
||
134 | $route = ''; |
||
135 | preg_match('/@route\ (.*)\n/i', $comments, $route); |
||
136 | |||
137 | return $route[1]; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Extract api from doc comments |
||
142 | * |
||
143 | * @param string $comments |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function extractApi($comments = '') |
||
148 | { |
||
149 | $api = ''; |
||
150 | preg_match('/@api\ (.*)\n/i', $comments, $api); |
||
151 | |||
152 | return $api[1]; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Extract api from doc comments |
||
157 | * |
||
158 | * @param string $comments |
||
159 | * |
||
160 | * @return boolean |
||
161 | */ |
||
162 | protected function checkDeprecated($comments = '') |
||
163 | { |
||
164 | return false != preg_match('/@deprecated\n/i', $comments); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Extract visibility from doc comments |
||
169 | * |
||
170 | * @param string $comments |
||
171 | * |
||
172 | * @return boolean |
||
173 | */ |
||
174 | protected function extractVisibility($comments = '') |
||
175 | { |
||
176 | $visible = TRUE; |
||
177 | preg_match('/@visible\ (true|false)\n/i', $comments, $visibility); |
||
178 | if (count($visibility)) { |
||
179 | $visible = !('false' == $visibility[1]); |
||
180 | } |
||
181 | |||
182 | return $visible; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Method that extract the description for the endpoint |
||
187 | * |
||
188 | * @param string $comments |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | protected function extractDescription($comments = '') |
||
193 | { |
||
194 | $description = ''; |
||
195 | $docs = explode("\n", $comments); |
||
196 | if (count($docs)) { |
||
197 | foreach ($docs as &$doc) { |
||
198 | View Code Duplication | if (!preg_match('/(\*\*|\@)/i', $doc) && preg_match('/\*\ /i', $doc)) { |
|
199 | $doc = explode('* ', $doc); |
||
200 | $description = $doc[1]; |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $description; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Method that extract the type of a variable |
||
210 | * |
||
211 | * @param string $comments |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public static function extractVarType($comments = '') |
||
216 | { |
||
217 | $type = 'string'; |
||
218 | preg_match('/@var\ (.*) (.*)\n/i', $comments, $varType); |
||
219 | if (count($varType)) { |
||
220 | $aux = trim($varType[1]); |
||
221 | $type = str_replace(' ', '', strlen($aux) > 0 ? $varType[1] : $varType[2]); |
||
222 | } |
||
223 | |||
224 | return $type; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Method that extract the payload for the endpoint |
||
229 | * |
||
230 | * @param string $model |
||
231 | * @param string $comments |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | protected function extractPayload($model, $comments = '') |
||
236 | { |
||
237 | $payload = []; |
||
238 | preg_match('/@payload\ (.*)\n/i', $comments, $doc); |
||
239 | if (count($doc)) { |
||
240 | $namespace = str_replace('{__API__}', $model, $doc[1]); |
||
241 | $payload = $this->extractModelFields($namespace); |
||
242 | $reflector = new \ReflectionClass($namespace); |
||
243 | $namespace = $reflector->getShortName(); |
||
244 | } else { |
||
245 | $namespace = $model; |
||
246 | } |
||
247 | |||
248 | return [$namespace, $payload]; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Extract all the properties from Dto class |
||
253 | * |
||
254 | * @param string $class |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function extractDtoProperties($class) |
||
259 | { |
||
260 | $properties = []; |
||
261 | $reflector = new \ReflectionClass($class); |
||
262 | if ($reflector->isSubclassOf(self::DTO_INTERFACE)) { |
||
263 | $properties = array_merge($properties, InjectorHelper::extractVariables($reflector)); |
||
264 | } |
||
265 | |||
266 | return $properties; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Extract return class for api endpoint |
||
271 | * |
||
272 | * @param string $model |
||
273 | * @param string $comments |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | protected function extractReturn($model, $comments = '') |
||
278 | { |
||
279 | $modelDto = []; |
||
280 | preg_match('/\@return\ (.*)\((.*)\)\n/i', $comments, $returnTypes); |
||
281 | if (count($returnTypes)) { |
||
282 | // Extract principal DTO information |
||
283 | if (array_key_exists(1, $returnTypes)) { |
||
284 | $modelDto = $this->extractDtoProperties($returnTypes[1]); |
||
285 | } |
||
286 | if (array_key_exists(2, $returnTypes)) { |
||
287 | $subDtos = preg_split('/,?\ /', str_replace('{__API__}', $model, $returnTypes[2])); |
||
288 | if (count($subDtos)) { |
||
289 | foreach ($subDtos as $subDto) { |
||
290 | $isArray = false; |
||
291 | list($field, $dtoName) = explode('=', $subDto); |
||
292 | if (false !== strpos($dtoName, '[') && false !== strpos($dtoName, ']')) { |
||
293 | $dtoName = str_replace(']', '', str_replace('[', '', $dtoName)); |
||
294 | $isArray = true; |
||
295 | } |
||
296 | $dto = $this->extractModelFields($dtoName); |
||
297 | $modelDto[$field] = ($isArray) ? [$dto] : $dto; |
||
298 | $modelDto['objects'][$dtoName] = $dto; |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return $modelDto; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Extract all fields from a ActiveResource model |
||
309 | * |
||
310 | * @param string $namespace |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | protected function extractModelFields($namespace) |
||
315 | { |
||
316 | $payload = []; |
||
317 | try { |
||
318 | $reflector = new \ReflectionClass($namespace); |
||
319 | // Checks if reflector is a subclass of propel ActiveRecords |
||
320 | if (NULL !== $reflector && $reflector->isSubclassOf(self::MODEL_INTERFACE)) { |
||
321 | $tableMap = $namespace::TABLE_MAP; |
||
322 | $tableMap = $tableMap::getTableMap(); |
||
323 | /** @var ColumnMap $field */ |
||
324 | foreach ($tableMap->getColumns() as $field) { |
||
325 | list($type, $format) = DocumentorService::translateSwaggerFormats($field->getType()); |
||
326 | $info = [ |
||
327 | "type" => $type, |
||
328 | "required" => $field->isNotNull(), |
||
329 | 'format' => $format, |
||
330 | ]; |
||
331 | $payload[$field->getPhpName()] = $info; |
||
332 | } |
||
333 | } elseif (null !== $reflector && $reflector->isSubclassOf(self::DTO_INTERFACE)) { |
||
334 | $payload = $this->extractDtoProperties($namespace); |
||
335 | } |
||
336 | } catch (\Exception $e) { |
||
337 | Logger::getInstance()->errorLog($e->getMessage()); |
||
338 | } |
||
339 | |||
340 | return $payload; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Method that extract all the needed info for each method in each API |
||
345 | * |
||
346 | * @param string $namespace |
||
347 | * @param \ReflectionMethod $method |
||
348 | * @param \ReflectionClass $reflection |
||
349 | * @param string $module |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function extractMethodInfo($namespace, \ReflectionMethod $method, \ReflectionClass $reflection, $module) |
||
354 | { |
||
355 | $methodInfo = NULL; |
||
356 | $docComments = $method->getDocComment(); |
||
357 | if (FALSE !== $docComments && preg_match('/\@route\ /i', $docComments)) { |
||
358 | $api = self::extractApi($reflection->getDocComment()); |
||
359 | list($route, $info) = RouterHelper::extractRouteInfo($method, $api, $module); |
||
360 | $route = explode('#|#', $route); |
||
361 | $modelNamespace = str_replace('Api', 'Models', $namespace); |
||
362 | if ($info['visible'] && !self::checkDeprecated($docComments)) { |
||
363 | try { |
||
364 | $return = $this->extractReturn($modelNamespace, $docComments); |
||
365 | $url = array_pop($route); |
||
366 | $methodInfo = [ |
||
367 | 'url' => str_replace("/" . $module . "/api", '', $url), |
||
368 | 'method' => $info['http'], |
||
369 | 'description' => $info['label'], |
||
370 | 'return' => $return, |
||
371 | 'objects' => $return['objects'], |
||
372 | 'class' => $reflection->getShortName(), |
||
373 | ]; |
||
374 | unset($methodInfo['return']['objects']); |
||
375 | $this->setRequestParams($method, $methodInfo, $modelNamespace, $docComments); |
||
376 | $this->setQueryParams($method, $methodInfo); |
||
377 | $this->setRequestHeaders($reflection, $methodInfo); |
||
378 | } catch (\Exception $e) { |
||
379 | Logger::getInstance()->errorLog($e->getMessage()); |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | |||
384 | return $methodInfo; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Translator from php types to swagger types |
||
389 | * @param string $format |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | 1 | public static function translateSwaggerFormats($format) |
|
434 | |||
435 | /** |
||
436 | * Method that parse the definitions for the api's |
||
437 | * @param string $name |
||
438 | * @param array $fields |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | public static function extractSwaggerDefinition($name, array $fields) |
||
466 | |||
467 | /** |
||
468 | * @return array |
||
469 | */ |
||
470 | private static function swaggerResponses() |
||
515 | |||
516 | /** |
||
517 | * Method that export |
||
518 | * @param array $module |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | public static function swaggerFormatter(array $module) |
||
621 | |||
622 | /** |
||
623 | * Method that extract the Dto class for the api documentation |
||
624 | * @param string $dto |
||
625 | * @param boolean $isArray |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | protected function extractDtoName($dto, $isArray = false) |
||
639 | |||
640 | /** |
||
641 | * @param \ReflectionMethod $method |
||
642 | * @param $methodInfo |
||
643 | */ |
||
644 | protected function setQueryParams(\ReflectionMethod $method, &$methodInfo) |
||
674 | /** |
||
675 | * @param \ReflectionClass $reflection |
||
676 | * @param $methodInfo |
||
677 | */ |
||
678 | protected function setRequestHeaders(\ReflectionClass $reflection, &$methodInfo) |
||
710 | |||
711 | /** |
||
712 | * @param \ReflectionMethod $method |
||
713 | * @param array $methodInfo |
||
714 | * @param string $modelNamespace |
||
715 | * @param string $docComments |
||
716 | */ |
||
717 | protected function setRequestParams(\ReflectionMethod $method, &$methodInfo, $modelNamespace, $docComments) |
||
740 | } |
||
741 |
This check marks private properties in classes that are never used. Those properties can be removed.