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 |
||
23 | class Filesystem implements StorageInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $rootPath; |
||
29 | |||
30 | /** |
||
31 | * normally 0 - 3 |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $hashLevel; |
||
36 | |||
37 | /** |
||
38 | * @param string $rootPath the base/root storage directory |
||
39 | * @param int $hashLevel directory hash depth |
||
40 | * @throws \RuntimeException if mkdir failed |
||
41 | */ |
||
42 | public function __construct( |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | public function get(string $key): array |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | public function set(string $key, string $value, int $ttl): bool |
||
86 | |||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | public function delete(string $key): bool |
||
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | public function clear(): bool |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | */ |
||
117 | public function garbageCollect() |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getRoot(): string |
||
138 | |||
139 | /** |
||
140 | * @param string $key |
||
141 | * @return string |
||
142 | */ |
||
143 | protected function getPath(string $key): string |
||
160 | |||
161 | /** |
||
162 | * get lock of the path |
||
163 | * |
||
164 | * @param string $path |
||
165 | * @return resource|false |
||
166 | */ |
||
167 | protected function getLock(string $path) |
||
191 | |||
192 | /** |
||
193 | * @param string $path |
||
194 | * @param resource $fp |
||
195 | * @return bool |
||
196 | */ |
||
197 | protected function releaseLock(string $path, $fp): bool |
||
202 | |||
203 | /** |
||
204 | * Remove a whole directory or stale files only |
||
205 | * |
||
206 | * @param string $dir |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function rmDir(string $dir, bool $staleOnly = false) |
||
223 | } |
||
224 |
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.