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 |
||
8 | View Code Duplication | class MysqlDistanceRepository implements DistanceRepositoryInterface |
|
|
|||
9 | { |
||
10 | |||
11 | /** @var ConnectionLocator */ |
||
12 | protected $connections; |
||
13 | |||
14 | /** |
||
15 | * @param ConnectonLocator $connections |
||
16 | */ |
||
17 | public function __construct(ConnectionLocator $connections) |
||
21 | |||
22 | /** |
||
23 | * @param integer $id |
||
24 | * |
||
25 | * @return array|false |
||
26 | */ |
||
27 | public function getDistanceById($id) |
||
43 | |||
44 | /** |
||
45 | * @param DateTimeInterface $date |
||
46 | * @param string $type |
||
47 | * @param double $mileage |
||
48 | * |
||
49 | * @return array|false |
||
50 | */ |
||
51 | public function getDistanceByFields(DateTimeInterface $date, $type, $mileage) |
||
69 | |||
70 | /** |
||
71 | * @return array|false |
||
72 | */ |
||
73 | public function getUnmappedDistances() |
||
86 | } |
||
87 |
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.