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 |
||
24 | final class ProviderCache implements Provider |
||
25 | { |
||
26 | /** |
||
27 | * @var Provider |
||
28 | */ |
||
29 | private $realProvider; |
||
30 | |||
31 | /** |
||
32 | * @var CacheInterface |
||
33 | */ |
||
34 | private $cache; |
||
35 | |||
36 | /** |
||
37 | * How log a result is going to be cached. |
||
38 | * |
||
39 | * @var int|null |
||
40 | */ |
||
41 | private $lifetime; |
||
42 | |||
43 | /** |
||
44 | * @param Provider $realProvider |
||
45 | * @param CacheInterface $cache |
||
46 | * @param int $lifetime |
||
47 | */ |
||
48 | 6 | public function __construct(Provider $realProvider, CacheInterface $cache, int $lifetime = null) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 2 | View Code Duplication | public function geocodeQuery(GeocodeQuery $query): Collection |
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 2 | View Code Duplication | public function reverseQuery(ReverseQuery $query): Collection |
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 1 | public function getName(): string |
|
94 | |||
95 | 1 | public function __call($method, $args) |
|
99 | |||
100 | /** |
||
101 | * @param GeocodeQuery|ReverseQuery $query |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 4 | private function getCacheKey($query): string |
|
110 | } |
||
111 |
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.