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 | abstract class AbstractGenerator extends AbstractBasicGenerator implements BlockGeneratorDependentInterface, LineGeneratorDependentInterface |
||
| 14 | { |
||
| 15 | /** @var boolean */ |
||
| 16 | private $canBeGenerated; |
||
| 17 | |||
| 18 | /** @var BlockGenerator */ |
||
| 19 | private $blockGenerator; |
||
| 20 | |||
| 21 | /** @var LineGenerator */ |
||
| 22 | private $lineGenerator; |
||
| 23 | |||
| 24 | /** @var array */ |
||
| 25 | private $properties = []; |
||
| 26 | |||
| 27 | public function __construct() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | public function clear() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function __clone() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param BlockGenerator $generator |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | public function setBlockGenerator(BlockGenerator $generator) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param LineGenerator $generator |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | public function setLineGenerator(LineGenerator $generator) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param Indention $indention |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | final public function setIndention(Indention $indention) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return boolean |
||
| 86 | */ |
||
| 87 | public function hasContent() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | final protected function markAsCanBeGenerated() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | final protected function canBeGenerated() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $name |
||
| 112 | * @param mixed $value |
||
| 113 | * @param bool $isStackable |
||
| 114 | */ |
||
| 115 | final protected function addGeneratorProperty($name, $value, $isStackable = true) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string|AbstractGenerator[] $content |
||
| 137 | * @param bool $isIndented |
||
| 138 | * @throws InvalidArgumentException |
||
| 139 | * @todo extend addContent to support int|string|array|LineGenerator|BlockGenerator |
||
| 140 | */ |
||
| 141 | final protected function addContent($content, $isIndented = false) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param GeneratorInterface $generator |
||
| 163 | * @param bool $isIndented - needed? |
||
| 164 | */ |
||
| 165 | final protected function addGeneratorAsContent(GeneratorInterface $generator, $isIndented = false) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | final protected function generateStringFromContent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $name |
||
| 187 | * @param mixed $default |
||
| 188 | * @return null|string|array |
||
| 189 | */ |
||
| 190 | final protected function getGeneratorProperty($name, $default = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param null|string|LineGenerator|BlockGenerator $content |
||
| 197 | * @return BlockGenerator |
||
| 198 | */ |
||
| 199 | View Code Duplication | final protected function getBlockGenerator($content = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param null|string $content |
||
| 213 | * @return LineGenerator |
||
| 214 | */ |
||
| 215 | View Code Duplication | final protected function getLineGenerator($content = null) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | final protected function getNotPrintableTypeHints() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param array $array |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | final protected function getLastArrayKey(array $array) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | final protected function resetContent() |
||
| 265 | } |
||
| 266 |
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.