Total Complexity | 100 |
Total Lines | 528 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 2 | Features | 1 |
Complex classes like PhpDomainBuilder 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.
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 PhpDomainBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class PhpDomainBuilder extends RstBuilder |
||
45 | { |
||
46 | const SECTION_BEFORE_DESCRIPTION = self::class.'::SECTION_BEFORE_DESCRIPTION'; |
||
47 | const SECTION_AFTER_DESCRIPTION = self::class.'::SECTION_AFTER_DESCRIPTION'; |
||
48 | const SECTION_AFTER_TITLE = self::class.'::SECTION_AFTER_TITLE'; |
||
49 | const SECTION_AFTER_INTRODUCTION = self::class.'::SECTION_AFTER_INTRODUCTION'; |
||
50 | |||
51 | use ExtensionBuilder { |
||
52 | ExtensionBuilder::__construct as private __extensionConstructor; |
||
53 | } |
||
54 | |||
55 | public function __construct($extensions) |
||
56 | { |
||
57 | $this->__extensionConstructor($extensions); |
||
58 | $this->addMultiline('.. role:: php(code)'.PHP_EOL.':language: php', true); |
||
59 | $this->addLine(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Add namespace. |
||
64 | * |
||
65 | * @param Element $element |
||
66 | */ |
||
67 | protected function addPageHeader(Element $element) |
||
68 | { |
||
69 | $this->addH1(self::escape($element->getName()))->addLine(); |
||
70 | if (self::getNamespace($element) !== '') { |
||
71 | $this->beginPhpDomain('namespace', substr(self::getNamespace($element), 1), false); |
||
72 | } |
||
73 | if ($element instanceof Class_) { |
||
74 | $modifiers = $element->isAbstract() ? ' abstract' : ''; |
||
75 | $modifiers = $element->isFinal() ? ' final' : $modifiers; |
||
76 | if ($modifiers !== '') { |
||
77 | $this->addLine('.. rst-class:: '.$modifiers)->addLine(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | $this->callExtensions(self::SECTION_AFTER_TITLE, $element); |
||
82 | |||
83 | $this->beginPhpDomain($this->getTypeForClass($element), $element->getName(), false); |
||
84 | $this->addLine(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Strip element name from Fqsen to return the namespace only. |
||
89 | * |
||
90 | * @param Element $element |
||
91 | * |
||
92 | * @return mixed |
||
93 | */ |
||
94 | public static function getNamespace(Element $element) |
||
95 | { |
||
96 | return substr($element->getFqsen(), 0, strlen($element->getFqsen()) - strlen('\\'.$element->getName())); |
||
97 | //return str_replace('\\' . $element->getName(), '', $element->getFqsen()); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param $type string |
||
102 | * @param $name string |
||
103 | * @param $indent bool Should indent after the section started |
||
104 | */ |
||
105 | public function beginPhpDomain($type, $name, $indent = true) |
||
106 | { |
||
107 | // FIXME: Add checks if it is properly ended |
||
108 | $this->addLine('.. php:'.$type.':: '.$name)->addLine(); |
||
109 | if ($indent === true) { |
||
110 | $this->indent(); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | private function getTypeForClass($element) |
||
115 | { |
||
116 | switch (get_class($element)) { |
||
117 | case Class_::class: |
||
118 | return 'class'; |
||
119 | case Interface_::class: |
||
120 | return 'interface'; |
||
121 | case Trait_::class: |
||
122 | return 'trait'; |
||
123 | case Function_::class: |
||
124 | return 'function'; |
||
125 | case Method::class: |
||
126 | return 'method'; |
||
127 | default: |
||
128 | return ''; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | protected function addAfterIntroduction($element) |
||
135 | } |
||
136 | |||
137 | protected function addConstants($constants) |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param Element $element |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function shouldRenderElement(Element $element) |
||
155 | { |
||
156 | /** @var Extension $extension */ |
||
157 | foreach ($this->extensions as $extension) { |
||
158 | if ($extension->shouldRenderElement($element) === false) { |
||
159 | return false; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return true; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param Constant $constant |
||
168 | */ |
||
169 | private function addConstant(Constant $constant) |
||
170 | { |
||
171 | $this->beginPhpDomain('const', $constant->getName().' = '.self::escape($constant->getValue())); |
||
172 | $docBlock = $constant->getDocBlock(); |
||
173 | $this->addDocBlockDescription($constant); |
||
174 | if ($docBlock) { |
||
175 | foreach ($docBlock->getTags() as $tag) { |
||
176 | $this->addDocblockTag($tag->getName(), $docBlock); |
||
177 | } |
||
178 | } |
||
179 | $this->endPhpDomain(); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param Class_|Interface_|Trait_|Property|Method|Constant $element |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function addDocBlockDescription($element) |
||
188 | { |
||
189 | if ($element === null) { |
||
190 | return $this; |
||
191 | } |
||
192 | $docBlock = $element->getDocBlock(); |
||
193 | $this->callExtensions(self::SECTION_BEFORE_DESCRIPTION, $element); |
||
194 | if ($docBlock !== null && $docBlock->getSummary() !== '') { |
||
195 | $this->addLine('.. rst-class:: phpdoc-description')->addLine(); |
||
196 | $this->indent(); |
||
197 | $this->addMultilineWithoutRendering(RstBuilder::escape($docBlock->getSummary()))->addLine(); |
||
198 | if ((string) $docBlock->getDescription() !== '') { |
||
199 | $this->addMultilineWithoutRendering(RstBuilder::escape($docBlock->getDescription()))->addLine(); |
||
200 | } |
||
201 | $this->unindent(); |
||
202 | } |
||
203 | $this->callExtensions(self::SECTION_AFTER_DESCRIPTION, $element); |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string $tagName Name of the tag to parse |
||
210 | * @param DocBlock $docBlock |
||
211 | */ |
||
212 | protected function addDocblockTag($tagName, DocBlock $docBlock) |
||
213 | { |
||
214 | $inclusion_tag_name = [ |
||
215 | 'return', |
||
216 | 'var', |
||
217 | 'throws', |
||
218 | 'since', |
||
219 | 'deprecated', |
||
220 | 'see', |
||
221 | 'license', |
||
222 | ]; |
||
223 | |||
224 | $tags = $docBlock->getTagsByName($tagName); |
||
225 | |||
226 | if (!in_array($tagName, $inclusion_tag_name) || (in_array($tagName, $inclusion_tag_name) && count($tags) === 0)) { |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | switch ($tagName) { |
||
231 | case 'return': |
||
232 | /** @var Return_ $return */ |
||
233 | $return = $tags[0]; |
||
234 | $this->addMultiline( |
||
235 | ':Returns: '.self::typesToRst($return->getType()).' '.RstBuilder::escape($return->getDescription()), |
||
236 | true |
||
237 | ); |
||
238 | break; |
||
239 | case 'var': |
||
240 | /** @var DocBlock\Tags\Var_ $return */ |
||
241 | $return = $tags[0]; |
||
242 | $this->addMultiline( |
||
243 | ':Type: '.self::typesToRst($return->getType()).' '.RstBuilder::escape($return->getDescription()), |
||
244 | true |
||
245 | ); |
||
246 | break; |
||
247 | case 'throws': |
||
248 | /** @var Throws $tag */ |
||
249 | foreach ($tags as $tag) { |
||
250 | $this->addMultiline( |
||
251 | ':Throws: '.self::typesToRst($tag->getType()).' '.RstBuilder::escape($tag->getDescription()), |
||
252 | true |
||
253 | ); |
||
254 | } |
||
255 | break; |
||
256 | case 'since': |
||
257 | /** @var Since $return */ |
||
258 | $return = $tags[0]; |
||
259 | $this->addMultiline( |
||
260 | ':Since: '.$return->getVersion().' '.RstBuilder::escape($return->getDescription()), |
||
261 | true |
||
262 | ); |
||
263 | break; |
||
264 | case 'deprecated': |
||
265 | /** @var Deprecated $return */ |
||
266 | $return = $tags[0]; |
||
267 | $this->addMultiline( |
||
268 | ':Deprecated: '.$return->getVersion().' '.RstBuilder::escape($return->getDescription()), |
||
269 | true |
||
270 | ); |
||
271 | break; |
||
272 | case 'see': |
||
273 | /** @var See $return */ |
||
274 | $return = $tags[0]; |
||
275 | $this->addMultiline( |
||
276 | ':See: '.self::typesToRst($return->getReference()).' '.RstBuilder::escape($return->getDescription()), |
||
277 | true |
||
278 | ); |
||
279 | break; |
||
280 | case 'license': |
||
281 | /** @var DocBlock\Tags\BaseTag $return */ |
||
282 | $return = $tags[0]; |
||
283 | $this->addMultiline(':License: '.RstBuilder::escape($return->getDescription()), true); |
||
284 | break; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param string $typesString |
||
290 | * |
||
291 | * @return bool|string |
||
292 | */ |
||
293 | public static function typesToRst($typesString) |
||
294 | { |
||
295 | // http://docs.phpdoc.org/guides/types.html |
||
296 | $whitelist = [ |
||
297 | 'string', |
||
298 | 'int', |
||
299 | 'integer', |
||
300 | 'float', |
||
301 | 'bool', |
||
302 | 'boolean', |
||
303 | 'array', |
||
304 | 'resource', |
||
305 | 'null', |
||
306 | 'callable', |
||
307 | 'mixed', |
||
308 | 'void', |
||
309 | 'object', |
||
310 | 'false', |
||
311 | 'true', |
||
312 | 'self', |
||
313 | 'static', |
||
314 | '$this', |
||
315 | ]; |
||
316 | $types = explode('|', $typesString); |
||
317 | $result = ''; |
||
318 | /* @var string $type */ |
||
319 | foreach ($types as $typeFull) { |
||
320 | $type = str_replace('[]', '', $typeFull); |
||
321 | if (in_array($type, $whitelist, true)) { |
||
322 | $result .= $typeFull.' | '; |
||
323 | continue; |
||
324 | } |
||
325 | if (0 === strpos($type, '\\')) { |
||
326 | $type = substr($type, 1); |
||
327 | } |
||
328 | $result .= ':any:`'.RstBuilder::escape($typeFull).' <'.RstBuilder::escape($type).'>` | '; |
||
329 | } |
||
330 | |||
331 | return substr($result, 0, -3); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param string $type |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function endPhpDomain($type = '') |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param Property[] $properties |
||
347 | */ |
||
348 | protected function addProperties($properties) |
||
349 | { |
||
350 | //Choose display properties |
||
351 | $displayProperties = []; |
||
352 | foreach ($properties as $property) { |
||
353 | if ($this->shouldRenderElement($property)) { |
||
354 | $displayProperties[] = $property; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | //Render |
||
359 | if (count($displayProperties) > 0) { |
||
360 | $this->addH2('Properties'); |
||
361 | foreach ($displayProperties as $property) { |
||
362 | $this->addProperty($property); |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @param Property $property |
||
369 | */ |
||
370 | private function addProperty(Property $property) |
||
371 | { |
||
372 | $modifiers = $property->isStatic() ? '' : ' static'; |
||
373 | $this->beginPhpDomain('attr', $property->getVisibility().$modifiers.' '.$property->getName()); |
||
374 | $docBlock = $property->getDocBlock(); |
||
375 | $this->addDocBlockDescription($property); |
||
376 | if ($docBlock) { |
||
377 | foreach ($docBlock->getTags() as $tag) { |
||
378 | $this->addDocblockTag($tag->getName(), $docBlock); |
||
379 | } |
||
380 | } |
||
381 | $this->endPhpDomain(); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param Interface_|Class_ $element |
||
386 | */ |
||
387 | protected function addParent($element) |
||
388 | { |
||
389 | if ($element instanceof Class_) { |
||
390 | $parent = $element->getParent(); |
||
391 | if ($parent !== null) { |
||
392 | $this->addFieldList('Parent', $parent !== null ? $this->getLink('class', $parent) : ''); |
||
393 | } |
||
394 | } |
||
395 | if ($element instanceof Interface_) { |
||
396 | $parents = $element->getParents(); |
||
397 | foreach ($parents as $parent) { |
||
398 | $this->addFieldList('Parent', $parent !== null ? $this->getLink('interface', $parent) : ''); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @param $type string |
||
405 | * @param $fqsen string |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | public static function getLink($type, $fqsen, $description = '') |
||
410 | { |
||
411 | if ($description !== '') { |
||
412 | return ':php:'.$type.':`'.RstBuilder::escape($description).'<'.RstBuilder::escape(substr( |
||
413 | $fqsen, |
||
414 | 1 |
||
415 | )).'>`'; |
||
416 | } |
||
417 | |||
418 | return ':php:'.$type.':`'.RstBuilder::escape(substr($fqsen, 1)).'`'; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param Class_|Trait_ $element |
||
423 | */ |
||
424 | protected function addUsedTraits($element) |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @param $methods |
||
437 | */ |
||
438 | protected function addMethods($methods) |
||
439 | { |
||
440 | if (count($methods) > 0) { |
||
441 | $this->addH2('Methods'); |
||
442 | foreach ($methods as $method) { |
||
443 | $this->addMethod($method); |
||
444 | } |
||
445 | } |
||
446 | } |
||
447 | |||
448 | private function addMethod(Method $method) |
||
449 | { |
||
450 | if (!$this->shouldRenderElement($method)) { |
||
451 | return; |
||
452 | } |
||
453 | $docBlock = $method->getDocBlock(); |
||
454 | $params = []; |
||
455 | $deprecated = []; |
||
456 | if ($docBlock !== null) { |
||
457 | /** @var Param $param */ |
||
458 | foreach ($docBlock->getTagsByName('param') as $param) { |
||
459 | if ($param instanceof Param) { |
||
460 | $params[$param->getVariableName()] = $param; |
||
461 | } |
||
462 | } |
||
463 | $deprecated = $docBlock->getTagsByName('deprecated'); |
||
464 | } |
||
465 | |||
466 | $modifiers = $method->getVisibility(); |
||
467 | $modifiers .= $method->isAbstract() ? ' abstract' : ''; |
||
468 | $modifiers .= $method->isFinal() ? ' final' : ''; |
||
469 | $modifiers .= $method->isStatic() ? ' static' : ''; |
||
470 | $deprecated = count($deprecated) > 0 ? ' deprecated' : ''; |
||
471 | $this->addLine('.. rst-class:: '.$modifiers.$deprecated)->addLine(); |
||
472 | $this->indent(); |
||
473 | |||
474 | $args = $this->processMethodArgumentTypes($method); |
||
475 | $this->beginPhpDomain('method', $modifiers.' '.$method->getName().'('.$args.')'); |
||
476 | $this->addDocBlockDescription($method); |
||
477 | $this->addLine(); |
||
478 | if (!empty($params)) { |
||
479 | $parameterDetails = $this->processMethodArgumentDocs($method, $params); |
||
480 | $this->addFieldList('Parameters', $parameterDetails); |
||
481 | } |
||
482 | if ($docBlock !== null) { |
||
483 | foreach ($docBlock->getTags() as $tag) { |
||
484 | $this->addDocblockTag($tag->getName(), $docBlock); |
||
485 | } |
||
486 | } |
||
487 | $this->endPhpDomain('method'); |
||
488 | $this->unindent(); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @param Method $method |
||
493 | * @param array $params |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | private function processMethodArgumentDocs(Method $method, array $params): string |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * @param Method $method |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | private function processMethodArgumentTypes(Method $method): string |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @param Argument $argument |
||
547 | * @param string $args |
||
548 | * |
||
549 | * @return string |
||
550 | */ |
||
551 | private function processMethodArgumentType(Argument $argument, string $args): string |
||
572 | } |
||
573 | } |
||
574 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.