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 |
||
12 | class ApcStore implements StoreInterface, IncrementorInterface |
||
13 | { |
||
14 | // for some optimization reasons in APC it does not invalidate |
||
15 | // data on same request. @see https://bugs.php.net/bug.php?id=58084 |
||
16 | // To fix this behavior we'll use cache to store items along with timestamps |
||
17 | public $ttlFix = false; |
||
18 | |||
19 | protected $ttls = array(); |
||
20 | |||
21 | public function __construct(array $config = array()) |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | View Code Duplication | public function add($key, $value, $ttl = 0) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | View Code Duplication | public function set($key, $value, $ttl = 0) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function get($key) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function has($key) |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function delete($key) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function flush() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function inc($key, $step = 1) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function dec($key, $step = 1) |
||
146 | |||
147 | /** |
||
148 | * Checks APC is running |
||
149 | * |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public function checkRunning() |
||
157 | } |
||
158 |
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.