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 declare(strict_types=1); |
||
10 | abstract class Firmware |
||
11 | { |
||
12 | private $build; |
||
13 | private $version; |
||
14 | |||
15 | private function setBuild(\DateTimeImmutable $build): void |
||
19 | |||
20 | /** |
||
21 | * @param int $version |
||
22 | * @throws InvalidValueException |
||
23 | */ |
||
24 | View Code Duplication | private function setVersion(int $version): void |
|
34 | |||
35 | /** |
||
36 | * @param \DateTimeInterface $build |
||
37 | * @param int $version |
||
38 | * @throws InvalidValueException |
||
39 | */ |
||
40 | protected function __construct(\DateTimeInterface $build, int $version) |
||
45 | |||
46 | public function getBuild(): \DateTimeImmutable |
||
50 | |||
51 | public function getVersion(): int |
||
55 | } |
||
56 |
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.