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 |
||
18 | final class InMemoryStorage implements StorageInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $store = []; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 10 | public function get(string $key, $default = false) |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 10 | public function set(string $key, $value, int $ttl) |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 7 | public function increment(string $key, int $by) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 8 | View Code Duplication | public function ttl(string $key) : int |
|
|||
63 | { |
||
64 | 8 | if (!isset($this->store[$key]['expires'])) { |
|
65 | 1 | return -1; |
|
66 | } |
||
67 | |||
68 | 7 | return max($this->store[$key]['expires'] - time(), 0); |
|
69 | } |
||
70 | |||
71 | 10 | private function has(string $key) : bool |
|
75 | |||
76 | 9 | View Code Duplication | private function hasExpired(string $key) : bool |
84 | } |
||
85 |
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.