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); |
||
| 9 | final class Service |
||
| 10 | { |
||
| 11 | private $typeId; |
||
| 12 | private $port; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param int $typeId |
||
| 16 | * @throws InvalidValueException |
||
| 17 | */ |
||
| 18 | private function setTypeId(int $typeId): void |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param int $port |
||
| 29 | * @throws InvalidValueException |
||
| 30 | */ |
||
| 31 | View Code Duplication | private function setPort(int $port): void |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @param int $typeId |
||
| 45 | * @param int $port |
||
| 46 | * @throws InvalidValueException |
||
| 47 | */ |
||
| 48 | public function __construct(int $typeId, int $port) |
||
| 53 | |||
| 54 | public function getTypeId(): int |
||
| 58 | |||
| 59 | public function getPort(): int |
||
| 63 | |||
| 64 | public function getName(): string |
||
| 72 | } |
||
| 73 |
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.