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 |
||
48 | class MountManager |
||
49 | { |
||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $plugins = array(); |
||
54 | |||
55 | /** |
||
56 | * Register a plugin. |
||
57 | * |
||
58 | * @param PluginInterface $plugin |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function addPlugin(PluginInterface $plugin) |
||
68 | |||
69 | /** |
||
70 | * Find a specific plugin. |
||
71 | * |
||
72 | * @param string $method |
||
73 | * |
||
74 | * @throws LogicException |
||
75 | * |
||
76 | * @return PluginInterface $plugin |
||
77 | */ |
||
78 | 18 | View Code Duplication | protected function findPlugin($method) |
90 | |||
91 | /** |
||
92 | * Invoke a plugin by method name. |
||
93 | * |
||
94 | * @param string $method |
||
95 | * @param array $arguments |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 18 | View Code Duplication | protected function invokePlugin($method, array $arguments, FilesystemInterface $filesystem) |
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * @var array |
||
112 | */ |
||
113 | protected $filesystems = array(); |
||
114 | |||
115 | /** |
||
116 | * Constructor. |
||
117 | * |
||
118 | * @param array $filesystems |
||
119 | */ |
||
120 | 45 | public function __construct(array $filesystems = array()) |
|
124 | |||
125 | /** |
||
126 | * Mount filesystems. |
||
127 | * |
||
128 | * @param array $filesystems [:prefix => Filesystem,] |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | 45 | public function mountFilesystems(array $filesystems) |
|
140 | |||
141 | /** |
||
142 | * Mount filesystems. |
||
143 | * |
||
144 | * @param string $prefix |
||
145 | * @param FilesystemInterface $filesystem |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | 33 | public function mountFilesystem($prefix, FilesystemInterface $filesystem) |
|
159 | |||
160 | /** |
||
161 | * Get the filesystem with the corresponding prefix. |
||
162 | * |
||
163 | * @param string $prefix |
||
164 | * |
||
165 | * @throws LogicException |
||
166 | * |
||
167 | * @return FilesystemInterface |
||
168 | */ |
||
169 | 33 | public function getFilesystem($prefix) |
|
177 | |||
178 | /** |
||
179 | * Retrieve the prefix from an arguments array. |
||
180 | * |
||
181 | * @param array $arguments |
||
182 | * |
||
183 | * @return array [:prefix, :arguments] |
||
184 | */ |
||
185 | 36 | public function filterPrefix(array $arguments) |
|
206 | |||
207 | /** |
||
208 | * @param string $directory |
||
209 | * @param bool $recursive |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 3 | public function listContents($directory = '', $recursive = false) |
|
226 | |||
227 | /** |
||
228 | * Call forwarder. |
||
229 | * |
||
230 | * @param string $method |
||
231 | * @param array $arguments |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | 15 | public function __call($method, $arguments) |
|
241 | |||
242 | /** |
||
243 | * @param $from |
||
244 | * @param $to |
||
245 | * @param array $config |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | 6 | public function copy($from, $to, array $config = array()) |
|
271 | |||
272 | /** |
||
273 | * @param array $keys |
||
274 | * @param string $directory |
||
275 | * @param bool $recursive |
||
276 | * @return mixed |
||
277 | */ |
||
278 | 3 | public function listWith(array $keys = array(), $directory = '', $recursive = false) |
|
286 | |||
287 | /** |
||
288 | * Move a file. |
||
289 | * |
||
290 | * @param $from |
||
291 | * @param $to |
||
292 | * @param array $config |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | 3 | public function move($from, $to, array $config = array()) |
|
306 | |||
307 | /** |
||
308 | * Invoke a plugin on a filesystem mounted on a given prefix. |
||
309 | * |
||
310 | * @param $method |
||
311 | * @param $arguments |
||
312 | * @param $prefix |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | 18 | public function invokePluginOnFilesystem($method, $arguments, $prefix) |
|
330 | } |
||
331 |
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.