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 |
||
| 30 | abstract class Node |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $attributes = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array[] |
||
| 39 | */ |
||
| 40 | private $children = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $lineNr = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var mixed |
||
| 49 | */ |
||
| 50 | private $value = ''; |
||
| 51 | |||
| 52 | public function __construct(int $lineNr = 0, $value = '') |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Accept a visitor |
||
| 60 | */ |
||
| 61 | public function accept(VisitorInterface $visitor): void |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get line number this node definition started at |
||
| 74 | */ |
||
| 75 | public function getLineNr(): int |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get node name |
||
| 82 | */ |
||
| 83 | public function getName(): string |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get node type identifier |
||
| 90 | */ |
||
| 91 | abstract public function getType(): string; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get raw value wrapped by node |
||
| 95 | * |
||
| 96 | * @return mixed Loaded node value |
||
| 97 | */ |
||
| 98 | public function getValue() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check if this is a null object implementation |
||
| 105 | */ |
||
| 106 | public function isNull(): bool |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set a custom attribute on node |
||
| 113 | * |
||
| 114 | * @param string $name Name of attribute |
||
| 115 | * @param mixed $value Value of attribute |
||
| 116 | */ |
||
| 117 | public function setAttribute(string $name, $value): void |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get custom attribute |
||
| 124 | * |
||
| 125 | * @param string $name Name of attribute |
||
| 126 | * @return mixed Value of attribute |
||
| 127 | */ |
||
| 128 | public function getAttribute(string $name) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Check if attribute has been set |
||
| 135 | */ |
||
| 136 | public function hasAttribute(string $name): bool |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get all registered attributes |
||
| 143 | */ |
||
| 144 | public function getAttributes(): array |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set a child node |
||
| 151 | */ |
||
| 152 | public function addChild(string $name, Node $node): void |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get child node |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function getChild(string $name): Node |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Check if child exists |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function hasChild(string $name): bool |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get registered child nodes |
||
| 187 | * |
||
| 188 | * @return iterable & Node[] |
||
| 189 | */ |
||
| 190 | public function getChildren(string $name = ''): iterable |
||
| 198 | } |
||
| 199 |
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.