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 namespace Comodojo\Cache\Drivers; |
||
25 | class Memory extends AbstractDriver { |
||
26 | |||
27 | const DRIVER_NAME = "memory"; |
||
28 | |||
29 | private $data = []; |
||
30 | |||
31 | public function __construct(array $configuration = []) {} |
||
32 | |||
33 | 85 | public function test() { |
|
38 | |||
39 | 63 | public function get($key, $namespace) { |
|
45 | |||
46 | 64 | public function set($key, $namespace, $value, $ttl = null) { |
|
60 | |||
61 | 6 | public function delete($key, $namespace) { |
|
70 | |||
71 | 11 | public function clear($namespace = null) { |
|
72 | |||
73 | 11 | if ( $namespace == null ) { |
|
74 | |||
75 | 9 | $this->data = []; |
|
76 | |||
77 | 9 | return true; |
|
78 | |||
79 | } else { |
||
80 | |||
81 | 2 | if ( $this->checkNamespace($namespace) ) { |
|
82 | 2 | unset($this->data[$namespace]); |
|
83 | 2 | return true; |
|
84 | } |
||
85 | |||
86 | return false; |
||
87 | |||
88 | } |
||
89 | |||
90 | } |
||
91 | |||
92 | 2 | View Code Duplication | public function getMultiple(array $keys, $namespace) { |
103 | |||
104 | 1 | View Code Duplication | public function setMultiple(array $key_values, $namespace, $ttl = null) { |
115 | |||
116 | 3 | View Code Duplication | public function deleteMultiple(array $keys, $namespace) { |
127 | |||
128 | 41 | public function has($key, $namespace) { |
|
133 | |||
134 | 2 | public function stats() { |
|
139 | |||
140 | 64 | private function checkNamespace($namespace, $create = false) { |
|
152 | |||
153 | 71 | private function checkMemory($key, $namespace) { |
|
169 | |||
170 | } |
||
171 |
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.