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 | View Code Duplication | class CarMutation implements MutationInterface, AliasedInterface |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var VehicleRepositoryInterface |
||
16 | */ |
||
17 | private $vehicleRepository; |
||
18 | |||
19 | /** |
||
20 | * @param VehicleRepositoryInterface $vehicleRepository |
||
21 | */ |
||
22 | public function __construct(VehicleRepositoryInterface $vehicleRepository) |
||
26 | |||
27 | /** |
||
28 | * @param string $id |
||
29 | * @param string $manufacturer |
||
30 | * @param string $model |
||
31 | * @param int $seatsNumber |
||
32 | * @return Car |
||
33 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
34 | */ |
||
35 | public function createCar(string $id, string $manufacturer, string $model, int $seatsNumber): Car |
||
47 | |||
48 | /** |
||
49 | * @param string $id |
||
50 | * @param string $manufacturer |
||
51 | * @param string $model |
||
52 | * @param int $seatsNumber |
||
53 | * @return Car |
||
54 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
55 | */ |
||
56 | public function updateCar(string $id, string $manufacturer, string $model, int $seatsNumber): Car |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public static function getAliases(): array |
||
80 | } |
||
81 |
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.