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 Device extends AbstractResource |
||
13 | { |
||
14 | /** |
||
15 | * @param string $identifier |
||
16 | * @return DeviceModel |
||
17 | */ |
||
18 | View Code Duplication | public function getDevice(string $identifier): DeviceModel |
|
32 | |||
33 | /** |
||
34 | * @return DeviceModel[] |
||
35 | */ |
||
36 | View Code Duplication | public function getDevices(): array |
|
50 | |||
51 | /** |
||
52 | * @param string $identifier The device identifier. |
||
53 | * @param BeaconUpdate $data The data to send. |
||
54 | * @return boolean |
||
55 | */ |
||
56 | public function updateDevice(string $identifier, BeaconUpdate $data): bool |
||
78 | } |
||
79 |
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.