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 |
||
15 | class ProfilingProvider implements Provider |
||
16 | { |
||
17 | /** |
||
18 | * @var Provider |
||
19 | */ |
||
20 | private $realProvider; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $queries = []; |
||
26 | |||
27 | /** |
||
28 | * @param Provider $realProvider |
||
29 | */ |
||
30 | public function __construct(Provider $realProvider) |
||
34 | |||
35 | View Code Duplication | public function geocodeQuery(GeocodeQuery $query): Collection |
|
48 | |||
49 | View Code Duplication | public function reverseQuery(ReverseQuery $query): Collection |
|
62 | |||
63 | /** |
||
64 | * @param GeocodeQuery|ReverseQuery $query |
||
65 | * @param float $duration geocoding duration |
||
66 | * @param Collection $result |
||
67 | */ |
||
68 | private function logQuery($query, float $duration, Collection $result = null) |
||
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getQueries(): array |
||
94 | |||
95 | public function __call($method, $args) |
||
99 | |||
100 | public function getName(): string |
||
104 | } |
||
105 |
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.