Total Complexity | 89 |
Total Lines | 367 |
Duplicated Lines | 22.89 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 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 |
||
49 | class PhpDomainBuilder extends RstBuilder { |
||
50 | |||
51 | const SECTION_BEFORE_DESCRIPTION = self::class . '::SECTION_BEFORE_DESCRIPTION'; |
||
52 | const SECTION_AFTER_DESCRIPTION = self::class . '::SECTION_AFTER_DESCRIPTION'; |
||
53 | const SECTION_AFTER_TITLE = self::class . '::SECTION_AFTER_TITLE'; |
||
54 | const SECTION_AFTER_INTRODUCTION = self::class . '::SECTION_AFTER_INTRODUCTION'; |
||
55 | |||
56 | use ExtensionBuilder { |
||
57 | ExtensionBuilder::__construct as private __extensionConstructor; |
||
58 | } |
||
59 | |||
60 | public function __construct($extensions) { |
||
61 | $this->__extensionConstructor($extensions); |
||
62 | $this->addMultiline('.. role:: php(code)' . PHP_EOL . ':language: php', true); |
||
63 | $this->addLine(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Strip element name from Fqsen to return the namespace only |
||
68 | * |
||
69 | * @param Element $element |
||
70 | * @return mixed |
||
71 | */ |
||
72 | public static function getNamespace(Element $element) { |
||
73 | return substr($element->getFqsen(), 0, strlen($element->getFqsen())-strlen('\\'. $element->getName())); |
||
74 | //return str_replace('\\' . $element->getName(), '', $element->getFqsen()); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Add namespace |
||
79 | * @param Element $element |
||
80 | */ |
||
81 | protected function addPageHeader(Element $element) { |
||
82 | $this->addH1(self::escape($element->getName()))->addLine(); |
||
83 | if (self::getNamespace($element) !== '') { |
||
84 | $this->beginPhpDomain('namespace', substr(self::getNamespace($element), 1), false); |
||
85 | } |
||
86 | if ($element instanceof Class_) { |
||
87 | $modifiers = $element->isAbstract() ? ' abstract' : ''; |
||
88 | $modifiers = $element->isFinal() ? ' final' : $modifiers; |
||
89 | if ($modifiers !== '') { |
||
90 | $this->addLine('.. rst-class:: ' . $modifiers)->addLine(); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $this->callExtensions(self::SECTION_AFTER_TITLE, $element); |
||
95 | |||
96 | |||
97 | $this->beginPhpDomain($this->getTypeForClass($element), $element->getName(), false); |
||
98 | $this->addLine(); |
||
99 | } |
||
100 | |||
101 | private function getTypeForClass($element) { |
||
102 | switch (get_class($element)) { |
||
103 | case Class_::class: |
||
104 | return 'class'; |
||
105 | break; |
||
|
|||
106 | case Interface_::class: |
||
107 | return 'interface'; |
||
108 | break; |
||
109 | case Trait_::class: |
||
110 | return 'trait'; |
||
111 | break; |
||
112 | case Function_::class: |
||
113 | return 'function'; |
||
114 | break; |
||
115 | case Method::class: |
||
116 | return 'method'; |
||
117 | default: |
||
118 | return ''; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | protected function addAfterIntroduction($element) { |
||
123 | $this->callExtensions(self::SECTION_AFTER_INTRODUCTION, $element); |
||
124 | } |
||
125 | |||
126 | |||
127 | protected function addConstants($constants) { |
||
128 | if (count($constants) > 0) { |
||
129 | $this->addH2('Constants'); |
||
130 | foreach ($constants as $constant) { |
||
131 | if ($this->shouldRenderElement($constant)) { |
||
132 | $this->addConstant($constant); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param Constant $constant |
||
140 | */ |
||
141 | View Code Duplication | private function addConstant(Constant $constant) { |
|
142 | $this->beginPhpDomain('const', $constant->getName() . ' = ' . $constant->getValue()); |
||
143 | $docBlock = $constant->getDocBlock(); |
||
144 | $this->addDocBlockDescription($constant); |
||
145 | if ($docBlock) { |
||
146 | foreach ($docBlock->getTags() as $tag) { |
||
147 | $this->addDocblockTag($tag->getName(), $docBlock); |
||
148 | } |
||
149 | } |
||
150 | $this->endPhpDomain(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param Property[] $properties |
||
155 | */ |
||
156 | protected function addProperties($properties) { |
||
157 | if (count($properties) > 0) { |
||
158 | $this->addH2('Properties'); |
||
159 | foreach ($properties as $property) { |
||
160 | if ($this->shouldRenderElement($property)) { |
||
161 | $this->addProperty($property); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param Property $property |
||
169 | */ |
||
170 | View Code Duplication | private function addProperty(Property $property) { |
|
171 | $this->beginPhpDomain('attr', $property->getName()); |
||
172 | $docBlock = $property->getDocBlock(); |
||
173 | $this->addDocBlockDescription($property); |
||
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 Interface_|Class_|Trait_ $element |
||
184 | */ |
||
185 | protected function addParent($element) { |
||
186 | View Code Duplication | if ($element instanceof Class_) { |
|
187 | $parent = $element->getParent(); |
||
188 | if ($parent !== null) { |
||
189 | $this->addFieldList('Parent', $parent !== null ? $this->getLink('class', $parent) : ''); |
||
190 | } |
||
191 | } |
||
192 | View Code Duplication | if ($element instanceof Trait_) { |
|
193 | $parent = $element->getParent(); |
||
1 ignored issue
–
show
|
|||
194 | if ($parent !== null) { |
||
195 | $this->addFieldList('Parent', $parent !== null ? $this->getLink('trait', $parent) : ''); |
||
196 | } |
||
197 | } |
||
198 | if ($element instanceof Interface_) { |
||
199 | $parents = $element->getParents(); |
||
200 | foreach ($parents as $parent) { |
||
201 | $this->addFieldList('Parent', $parent !== null ? $this->getLink('interface', $parent) : ''); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param Class_|Trait_ $element |
||
208 | */ |
||
209 | protected function addUsedTraits($element) { |
||
210 | $usedTraits = ''; |
||
211 | foreach ($element->getUsedTraits() as $trait) { |
||
212 | $usedTraits .= $this->getLink('trait', $trait) . ' '; |
||
213 | } |
||
214 | if ($usedTraits !== '') { |
||
215 | $this->addFieldList('Used traits', $usedTraits); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | protected function addMethods($methods) { |
||
220 | if (count($methods) > 0) { |
||
221 | $this->addH2('Methods'); |
||
222 | foreach ($methods as $method) { |
||
223 | $this->addMethod($method); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | private function addMethod(Method $method) { |
||
229 | if (!$this->shouldRenderElement($method)) { |
||
230 | return; |
||
231 | } |
||
232 | $docBlock = $method->getDocBlock(); |
||
233 | $params = []; |
||
234 | if ($docBlock !== null) { |
||
235 | /** @var Param $param */ |
||
236 | foreach ($docBlock->getTagsByName('param') as $param) { |
||
237 | $params[$param->getVariableName()] = $param; |
||
1 ignored issue
–
show
|
|||
238 | } |
||
239 | } |
||
240 | $args = ''; |
||
241 | /** @var Argument $argument */ |
||
242 | foreach ($method->getArguments() as $argument) { |
||
243 | // TODO: defaults, types |
||
244 | $args .= ' $' . $argument->getName() . ', '; |
||
245 | } |
||
246 | $args = substr($args, 0, -2); |
||
247 | |||
248 | $modifiers = $method->getVisibility(); |
||
249 | $modifiers .= $method->isAbstract() ? ' abstract' : ''; |
||
250 | $modifiers .= $method->isFinal() ? ' final' : ''; |
||
251 | $modifiers .= $method->isStatic() ? ' static' : ''; |
||
252 | $this->addLine('.. rst-class:: ' . $modifiers)->addLine(); |
||
253 | $this->indent(); |
||
254 | $this->beginPhpDomain('method', $method->getName() . '(' . $args . ')'); |
||
255 | $this->addDocBlockDescription($method); |
||
256 | $this->addLine(); |
||
257 | View Code Duplication | if (!empty($params)) { |
|
258 | foreach ($method->getArguments() as $argument) { |
||
259 | /** @var Param $param */ |
||
260 | $param = $params[$argument->getName()]; |
||
261 | if ($param !== null) $this->addMultiline(':param ' . self::escape($param->getType()) . ' $' . $argument->getName() . ': ' . $param->getDescription(), true); |
||
262 | } |
||
263 | } |
||
264 | $this->endPhpDomain('method'); |
||
265 | $this->unindent(); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param $type string |
||
270 | * @param $fqsen string |
||
271 | * @return string |
||
272 | */ |
||
273 | public static function getLink($type, $fqsen, $description='') { |
||
274 | if($description !== '') { |
||
275 | return ':php:' . $type . ':`' . RstBuilder::escape($description) . '<' . RstBuilder::escape(substr($fqsen, 1)) . '>`'; |
||
276 | } |
||
277 | return ':php:' . $type . ':`' . RstBuilder::escape(substr($fqsen, 1)) . '`'; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param $type string |
||
282 | * @param $name string |
||
283 | * @param $indent bool Should indent after the section started |
||
284 | */ |
||
285 | public function beginPhpDomain($type, $name, $indent = true) { |
||
286 | // FIXME: Add checks if it is properly ended |
||
287 | $this->addLine('.. php:' . $type . ':: ' . $name)->addLine(); |
||
288 | if ($indent === true) { |
||
289 | $this->indent(); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param string $type |
||
295 | */ |
||
296 | public function endPhpDomain($type = '') { |
||
297 | $this->unindent(); |
||
298 | $this->addLine(); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param Class_|Interface_|Trait_|Property|Method|Constant $element |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function addDocBlockDescription($element) { |
||
306 | if ($element === null) { |
||
307 | return; |
||
308 | } |
||
309 | $docBlock = $element->getDocBlock(); |
||
310 | $this->callExtensions(self::SECTION_BEFORE_DESCRIPTION, $element); |
||
311 | if ($docBlock !== null && $docBlock->getSummary() !== '') { |
||
312 | $this->addLine('.. rst-class:: phpdoc-description')->addLine(); |
||
313 | $this->addLine('::')->addLine(); |
||
314 | $this->indent(); |
||
315 | $this->addMultiline($docBlock->getSummary())->addLine(); |
||
316 | $this->addMultiline($docBlock->getDescription())->addLine(); |
||
317 | $this->unindent(); |
||
318 | } |
||
319 | $this->callExtensions(self::SECTION_AFTER_DESCRIPTION, $element); |
||
320 | return $this; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @param string $tagName Name of the tag to parse |
||
325 | * @param DocBlock $docBlock |
||
326 | */ |
||
327 | protected function addDocblockTag($tagName, DocBlock $docBlock) { |
||
328 | $tags = $docBlock->getTagsByName($tagName); |
||
329 | switch ($tagName) { |
||
330 | View Code Duplication | case 'return': |
|
331 | if (count($tags) === 0) continue; |
||
332 | /** @var Return_ $return */ |
||
333 | $return = $tags[0]; |
||
334 | $this->addMultiline(':Returns: ' . $return->getType() . ' ' . RstBuilder::escape($return->getDescription()), true); |
||
335 | break; |
||
336 | View Code Duplication | case 'var': |
|
337 | if (count($tags) === 0) continue; |
||
338 | /** @var DocBlock\Tags\Var_ $return */ |
||
339 | $return = $tags[0]; |
||
340 | $this->addMultiline(':Type: ' . self::typesToRst($return->getType()) . ' ' . RstBuilder::escape($return->getDescription()), true); |
||
341 | break; |
||
342 | View Code Duplication | case 'throws': |
|
343 | if (count($tags) === 0) continue; |
||
344 | /** @var Throws $tag */ |
||
345 | foreach ($tags as $tag) { |
||
346 | $this->addMultiline(':Throws: ' . $tag->getType() . ' ' . RstBuilder::escape($tag->getDescription()), true); |
||
347 | } |
||
348 | break; |
||
349 | View Code Duplication | case 'since': |
|
350 | if (count($tags) === 0) continue; |
||
351 | /** @var Since $return */ |
||
352 | $return = $tags[0]; |
||
353 | $this->addMultiline(':Since: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true); |
||
354 | break; |
||
355 | View Code Duplication | case 'deprecated': |
|
356 | if (count($tags) === 0) continue; |
||
357 | /** @var Deprecated $return */ |
||
358 | $return = $tags[0]; |
||
359 | $this->addMultiline(':Deprecated: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true); |
||
360 | break; |
||
361 | View Code Duplication | case 'see': |
|
362 | if (count($tags) === 0) continue; |
||
363 | /** @var See $return */ |
||
364 | $return = $tags[0]; |
||
365 | $this->addMultiline(':See: ' . $return->getReference() . ' ' . RstBuilder::escape($return->getDescription()), true); |
||
366 | break; |
||
367 | View Code Duplication | case 'license': |
|
368 | if (count($tags) === 0) continue; |
||
369 | /** @var DocBlock\Tags\BaseTag $return */ |
||
370 | $return = $tags[0]; |
||
371 | $this->addMultiline(':License: ' . RstBuilder::escape($return->getDescription()), true); |
||
372 | break; |
||
373 | case 'param': |
||
374 | // param handling is done by subclasses since it is more that docbook parsing |
||
375 | break; |
||
376 | default: |
||
377 | //echo 'Tag handling not defined for: ' . $tag . PHP_EOL; |
||
378 | break; |
||
379 | } |
||
380 | |||
381 | } |
||
382 | |||
383 | public static function typesToRst($types) { |
||
406 | } |
||
407 | |||
408 | public function shouldRenderElement(Element $element) { |
||
409 | /** @var Extension $extension */ |
||
410 | foreach ($this->extensions as $extension) { |
||
416 | } |
||
417 | |||
418 | |||
419 | } |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.