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 | View Code Duplication | class FlySystemStorage implements StorageInterface |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var Filesystem |
||
16 | */ |
||
17 | private $filesystem; |
||
18 | |||
19 | /** |
||
20 | * @param Filesystem $filesystem |
||
21 | */ |
||
22 | public function __construct(Filesystem $filesystem) |
||
26 | |||
27 | /** |
||
28 | * Returns the value for a key. |
||
29 | * |
||
30 | * @param string $key A unique key |
||
31 | * |
||
32 | * @return string|false The file contents or false on failure. |
||
33 | */ |
||
34 | public function get($key) |
||
42 | |||
43 | /** |
||
44 | * Checks if the cache has a value for a key. |
||
45 | * |
||
46 | * @param string $key A unique key |
||
47 | * |
||
48 | * @return Boolean Whether the cache has a value for this key |
||
49 | */ |
||
50 | public function has($key) |
||
54 | |||
55 | /** |
||
56 | * Sets a value in the cache. |
||
57 | * |
||
58 | * @param string $key A unique key |
||
59 | * @param string $contents The value to cache |
||
60 | * |
||
61 | * @return bool True on success, false on failure. |
||
62 | */ |
||
63 | public function set($key, $contents) |
||
67 | |||
68 | /** |
||
69 | * Removes a value from the cache. |
||
70 | * |
||
71 | * @param string $key A unique key |
||
72 | * |
||
73 | * @return bool True on success, false on failure. |
||
74 | */ |
||
75 | public function remove($key) |
||
83 | } |
||
84 |
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.