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 |
||
7 | View Code Duplication | class CacheTime implements ThrottlerItemInterface |
|
|
|||
8 | { |
||
9 | /** @var float $limit */ |
||
10 | private $time; |
||
11 | |||
12 | /** @var int $ttl */ |
||
13 | private $ttl; |
||
14 | |||
15 | /** |
||
16 | * @param float $time |
||
17 | * @param int $ttl |
||
18 | */ |
||
19 | 16 | public function __construct(float $time, int $ttl = null) |
|
24 | |||
25 | /** |
||
26 | * @param array $array |
||
27 | * |
||
28 | * @return ThrottlerItemInterface |
||
29 | */ |
||
30 | 16 | public static function createFromArray(array $array): ThrottlerItemInterface |
|
37 | |||
38 | /** |
||
39 | * @return array |
||
40 | */ |
||
41 | 16 | public function jsonSerialize() |
|
48 | |||
49 | /** |
||
50 | * @return float |
||
51 | */ |
||
52 | 16 | public function getTime(): float |
|
56 | |||
57 | /** |
||
58 | * @return float|null |
||
59 | */ |
||
60 | 16 | public function getTtl() |
|
64 | } |
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.