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 | class VehicleFactory |
||
13 | { |
||
14 | /** |
||
15 | * @param Argument $argument |
||
16 | * @return CarInput |
||
17 | */ |
||
18 | View Code Duplication | public static function createCarInput(Argument $argument): CarInput |
|
29 | |||
30 | /** |
||
31 | * @param CarInput $input |
||
32 | * @return Car |
||
33 | */ |
||
34 | public static function createCar(CarInput $input): Car |
||
43 | |||
44 | /** |
||
45 | * @param Argument $argument |
||
46 | * @return TruckInput |
||
47 | */ |
||
48 | View Code Duplication | public static function createTruckInput(Argument $argument): TruckInput |
|
59 | |||
60 | /** |
||
61 | * @param TruckInput $input |
||
62 | * @return Truck |
||
63 | */ |
||
64 | public static function createTruck(TruckInput $input): Truck |
||
73 | } |
||
74 |
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.