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:
| 1 | <?php |
||
| 13 | class FileGenerator extends AbstractDocumentedGenerator |
||
| 14 | { |
||
| 15 | /** @var boolean */ |
||
| 16 | private $addEmptyLine; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param ConstantGenerator $constant |
||
| 20 | * @return $this |
||
| 21 | */ |
||
| 22 | public function addConstant(ConstantGenerator $constant) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param PropertyGenerator $property |
||
| 31 | * @return $this |
||
| 32 | */ |
||
| 33 | public function addProperty(PropertyGenerator $property) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param ClassGenerator $class |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function addClass(ClassGenerator $class) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param InterfaceGenerator $interface |
||
| 53 | * @return $this |
||
| 54 | */ |
||
| 55 | public function addInterface(InterfaceGenerator $interface) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param MethodGenerator $method |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | public function addMethod(MethodGenerator $method) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param TraitGenerator $trait |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function addTrait(TraitGenerator $trait) |
||
| 83 | |||
| 84 | public function markAsExecutable() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param int|string|array $content |
||
| 93 | * @return $this |
||
| 94 | * @todo add support for Generator as type |
||
| 95 | */ |
||
| 96 | public function addFileContent($content) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @throws InvalidArgumentException|RuntimeException |
||
| 113 | * @return string |
||
| 114 | * @todo implement exception throwing if mandatory parameter is missing |
||
| 115 | */ |
||
| 116 | public function generate() |
||
| 138 | |||
| 139 | private function generateContent() |
||
| 148 | |||
| 149 | private function generateConstants() |
||
| 156 | |||
| 157 | private function generateProperties() |
||
| 164 | |||
| 165 | private function generateTraits() |
||
| 172 | |||
| 173 | private function generateMethods() |
||
| 180 | |||
| 181 | private function generateClasses() |
||
| 188 | |||
| 189 | private function generateInterfaces() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param null|array $collection |
||
| 199 | * @todo figure out difference between addContentFromCollection and |
||
| 200 | * addContentFromCollectionTwo and express difference with fitting names |
||
| 201 | */ |
||
| 202 | private function addContentFromCollection($collection) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param null|array $collection |
||
| 222 | */ |
||
| 223 | View Code Duplication | private function addContentFromCollectionTwo($collection) |
|
| 239 | } |
||
| 240 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.