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 |
||
| 12 | abstract class Output |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected static $header = [ |
||
| 18 | 'Source of truth Location', |
||
| 19 | 'Source of truth File Hash', |
||
| 20 | 'Target Location', |
||
| 21 | 'Target File Hash' |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $mask = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $headerEnabled; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var OutputInterface |
||
| 36 | */ |
||
| 37 | protected $output; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ArrayCollection |
||
| 41 | */ |
||
| 42 | protected $differences; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Output constructor. |
||
| 46 | * @param ArrayCollection $differences |
||
| 47 | * @param OutputInterface $output |
||
| 48 | */ |
||
| 49 | public function __construct(ArrayCollection $differences, OutputInterface $output) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return boolean |
||
| 59 | */ |
||
| 60 | public function isHeaderEnabled() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param boolean $headerEnabled |
||
| 67 | * @return $this |
||
| 68 | */ |
||
| 69 | public function setHeaderEnabled($headerEnabled) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | protected function getMask() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param DiffItem $diffItem |
||
| 85 | */ |
||
| 86 | protected function writeDiffItem(DiffItem $diffItem) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | abstract public function write(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Perform any logic at construction time. |
||
| 139 | */ |
||
| 140 | abstract protected function init(); |
||
| 141 | } |
||
| 142 |
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.