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 | class Shm implements CacheInterface |
||
8 | { |
||
9 | const SETTING_NAME = 'SETTING_NAME'; |
||
10 | const SETTING_SIZE = 'SETTING_SIZE'; |
||
11 | |||
12 | private $shmInstance = null; |
||
13 | private $isAvailable = null; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $settings = array(); |
||
19 | |||
20 | /** |
||
21 | * constructor of File |
||
22 | * @param array $settings |
||
23 | */ |
||
24 | 14 | public function __construct($settings = array()) |
|
28 | |||
29 | /** |
||
30 | * Can define settings of the component |
||
31 | * @param array $settings |
||
32 | * @return $this |
||
33 | */ |
||
34 | 14 | public function setSettings($settings = array()) |
|
39 | |||
40 | /** |
||
41 | * @param string $setting |
||
42 | * @param mixed $value |
||
43 | * @return $this |
||
44 | */ |
||
45 | 2 | public function setSetting($setting, $value) |
|
50 | |||
51 | /** |
||
52 | * Get a specific value of setting |
||
53 | * @param string $setting |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 8 | public function getSetting($setting) |
|
60 | |||
61 | /** |
||
62 | * Internal method to secure a SHM name |
||
63 | * @param string $name |
||
64 | * @return int |
||
65 | */ |
||
66 | 7 | private function secureName($name = null) |
|
70 | |||
71 | /** |
||
72 | * Convert string to a unique id |
||
73 | * @param string $string |
||
74 | * @return int |
||
75 | */ |
||
76 | 7 | private function stringToUniqueId($string) |
|
77 | { |
||
78 | 7 | if (is_numeric($string)) { |
|
79 | 6 | return (int)$string; |
|
80 | } |
||
81 | 6 | $length = strlen($string); |
|
82 | 6 | $return = 0; |
|
83 | 6 | View Code Duplication | for ($i = 0; $i < $length; $i++) { |
|
|||
84 | 6 | $return += ord($string{$i}); |
|
85 | } |
||
86 | 6 | return (int)$length . '1' . $return; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get SHM resource |
||
91 | * @return resource |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | 8 | private function getShm() |
|
95 | { |
||
96 | 8 | if (!$this->isAvailable()) { |
|
97 | 1 | throw new Exception('SHM is not available'); |
|
98 | } |
||
99 | 7 | if (!$this->shmInstance) { |
|
100 | 7 | $memorySize = $this->getSetting(self::SETTING_SIZE); |
|
101 | 7 | $shmName = $this->secureName($this->getSetting(self::SETTING_NAME)); |
|
102 | 7 | $this->shmInstance = is_numeric($memorySize) |
|
103 | 1 | ? $this->shmAttach($shmName, (int)$memorySize) |
|
104 | 6 | : $this->shmAttach($shmName); |
|
105 | } |
||
106 | 7 | return $this->shmInstance; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return resource |
||
111 | * @codeCoverageIgnore |
||
112 | */ |
||
113 | protected function shmAttach() |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | * @codeCoverageIgnore |
||
121 | */ |
||
122 | protected function shmGetVar() |
||
126 | |||
127 | /** |
||
128 | * Retrieve stored value |
||
129 | * @param string $key |
||
130 | * @return mixed|null |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | 2 | View Code Duplication | public function get($key) |
141 | |||
142 | /** |
||
143 | * @return bool |
||
144 | * @codeCoverageIgnore |
||
145 | */ |
||
146 | protected function shmHasVar() |
||
150 | |||
151 | /** |
||
152 | * Check whether key exists in SHM |
||
153 | * @param string $key |
||
154 | * @return bool |
||
155 | * @throws Exception |
||
156 | */ |
||
157 | 5 | View Code Duplication | public function has($key) |
165 | |||
166 | /** |
||
167 | * Remove a stored key if exists |
||
168 | * @param string $key |
||
169 | * @return $this |
||
170 | * @throws Exception |
||
171 | */ |
||
172 | 3 | View Code Duplication | public function remove($key) |
183 | |||
184 | /** |
||
185 | * @param resource $shmResource |
||
186 | * @param string $key |
||
187 | * @codeCoverageIgnore |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function shmRemoveVar($shmResource, $key) |
||
194 | |||
195 | /** |
||
196 | * Define a key in SHM |
||
197 | * @param string $key |
||
198 | * @param mixed $value |
||
199 | * @throws Exception |
||
200 | * @return $this |
||
201 | */ |
||
202 | 4 | View Code Duplication | public function set($key, $value) |
213 | |||
214 | /** |
||
215 | * @param resource $shmResource |
||
216 | * @param string $key |
||
217 | * @param mixed $value |
||
218 | * @return bool |
||
219 | * @codeCoverageIgnore |
||
220 | */ |
||
221 | protected function shmPutVar($shmResource, $key, $value) |
||
225 | |||
226 | /** |
||
227 | * Check whether apc is available |
||
228 | * @return bool |
||
229 | */ |
||
230 | 1 | public function isAvailable() |
|
237 | } |
||
238 |
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.