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 |
||
22 | class Memcached extends AbstractCacheItemPool |
||
23 | { |
||
24 | /** |
||
25 | * The Memcached driver |
||
26 | * |
||
27 | * @var \Memcached |
||
28 | * @since 1.0 |
||
29 | */ |
||
30 | private $driver; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param \Memcached $memcached The Memcached driver being used for this pool |
||
36 | * @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
||
37 | * |
||
38 | * @since 1.0 |
||
39 | * @throws \RuntimeException |
||
40 | */ |
||
41 | 12 | public function __construct(\Memcached $memcached, $options = []) |
|
48 | |||
49 | /** |
||
50 | * This will wipe out the entire cache's keys |
||
51 | * |
||
52 | * @return boolean The result of the clear operation. |
||
53 | * |
||
54 | * @since 1.0 |
||
55 | */ |
||
56 | 12 | public function clear() |
|
60 | |||
61 | /** |
||
62 | * Method to get a storage entry value from a key. |
||
63 | * |
||
64 | * @param string $key The storage entry identifier. |
||
65 | * |
||
66 | * @return CacheItemInterface |
||
67 | * |
||
68 | * @since 1.0 |
||
69 | */ |
||
70 | 5 | View Code Duplication | public function getItem($key) |
83 | |||
84 | /** |
||
85 | * Method to remove a storage entry for a key. |
||
86 | * |
||
87 | * @param string $key The storage entry identifier. |
||
88 | * |
||
89 | * @return boolean |
||
90 | * |
||
91 | * @since 1.0 |
||
92 | */ |
||
93 | 2 | public function deleteItem($key) |
|
109 | |||
110 | /** |
||
111 | * Persists a cache item immediately. |
||
112 | * |
||
113 | * @param CacheItemInterface $item The cache item to save. |
||
114 | * |
||
115 | * @return static The invoked object. |
||
116 | */ |
||
117 | 9 | public function save(CacheItemInterface $item) |
|
132 | |||
133 | /** |
||
134 | * Method to determine whether a storage entry has been set for a key. |
||
135 | * |
||
136 | * @param string $key The storage entry identifier. |
||
137 | * |
||
138 | * @return boolean |
||
139 | * |
||
140 | * @since 1.0 |
||
141 | */ |
||
142 | 5 | public function hasItem($key) |
|
148 | |||
149 | /** |
||
150 | * Test to see if the CacheItemPoolInterface is available |
||
151 | * |
||
152 | * @return boolean True on success, false otherwise |
||
153 | * |
||
154 | * @since __DEPLOY_VERSION__ |
||
155 | */ |
||
156 | 12 | public static function isSupported() |
|
164 | } |
||
165 |
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.