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 |
||
15 | View Code Duplication | class Local implements Adapter, |
|
16 | Adapter\StreamFactory, |
||
17 | Adapter\ChecksumCalculator, |
||
18 | Adapter\SizeCalculator, |
||
19 | Adapter\MimeTypeProvider |
||
20 | { |
||
21 | protected $directory; |
||
22 | private $create; |
||
23 | private $mode; |
||
24 | |||
25 | /** |
||
26 | * @param string $directory Directory where the filesystem is located |
||
27 | * @param bool $create Whether to create the directory if it does not |
||
28 | * exist (default FALSE) |
||
29 | * @param int $mode Mode for mkdir |
||
30 | * |
||
31 | * @throws \RuntimeException if the specified directory does not exist and |
||
32 | * could not be created |
||
33 | */ |
||
34 | public function __construct($directory, $create = false, $mode = 0777) |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function read($key) |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function write($key, $content) |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function rename($sourceKey, $targetKey) |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function exists($key) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function keys() |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function mtime($key) |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function delete($key) |
||
133 | |||
134 | /** |
||
135 | * @param string $key |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function isDirectory($key) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function createStream($key) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function checksum($key) |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function size($key) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function mimeType($key) |
||
177 | |||
178 | /** |
||
179 | * Computes the key from the specified path. |
||
180 | * |
||
181 | * @param string $path |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function computeKey($path) |
||
191 | |||
192 | /** |
||
193 | * Computes the path from the specified key. |
||
194 | * |
||
195 | * @param string $key The key which for to compute the path |
||
196 | * |
||
197 | * @return string A path |
||
198 | * |
||
199 | * @throws \OutOfBoundsException If the computed path is out of the |
||
200 | * directory |
||
201 | * @throws \RuntimeException If directory does not exists and cannot be created |
||
202 | */ |
||
203 | protected function computePath($key) |
||
209 | |||
210 | /** |
||
211 | * Normalizes the given path. |
||
212 | * |
||
213 | * @param string $path |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | protected function normalizePath($path) |
||
227 | |||
228 | /** |
||
229 | * Ensures the specified directory exists, creates it if it does not. |
||
230 | * |
||
231 | * @param string $directory Path of the directory to test |
||
232 | * @param bool $create Whether to create the directory if it does |
||
233 | * not exist |
||
234 | * |
||
235 | * @throws \RuntimeException if the directory does not exists and could not |
||
236 | * be created |
||
237 | */ |
||
238 | protected function ensureDirectoryExists($directory, $create = false) |
||
248 | |||
249 | /** |
||
250 | * Creates the specified directory and its parents. |
||
251 | * |
||
252 | * @param string $directory Path of the directory to create |
||
253 | * |
||
254 | * @throws \InvalidArgumentException if the directory already exists |
||
255 | * @throws \RuntimeException if the directory could not be created |
||
256 | */ |
||
257 | protected function createDirectory($directory) |
||
267 | } |
||
268 |
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.