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 |
||
| 25 | View Code Duplication | class MemcachedAdapter extends CacheAdapterAbstract implements CacheAdapterInterface |
|
|
|
|||
| 26 | { |
||
| 27 | /** @var null|array */ |
||
| 28 | protected static $calls; |
||
| 29 | /** @var CacheAdapterInterface */ |
||
| 30 | protected static $instance; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create Memcached class instance and connect to memcached pool |
||
| 34 | */ |
||
| 35 | 1 | protected function __construct() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Get data |
||
| 42 | * |
||
| 43 | * @param string $key Key |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | 1 | public function get($key) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Save data |
||
| 53 | * |
||
| 54 | * @param string $key Key |
||
| 55 | * @param mixed $value Data |
||
| 56 | * @param int $ttl Time to live |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 1 | public function set($key, $value, $ttl) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Check if data stored in cache |
||
| 66 | * |
||
| 67 | * @param string $key Key |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 1 | public function has($key) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Delete data |
||
| 77 | * |
||
| 78 | * @param string $key Key |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | 1 | public function del($key) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Method for deletion keys by template |
||
| 88 | * |
||
| 89 | * ATTENTION: if key contains spaces, for example 'THIS IS KEY::ID:50d98ld', |
||
| 90 | * then in cache it will be saved as 'THIS_IS_KEY::ID:50d98ld'. So, template |
||
| 91 | * for that key deletion must be look like - 'THIS_IS_KEY'. |
||
| 92 | * Deletion can be made by substring, containing in keys. For example |
||
| 93 | * '_KEY::ID'. |
||
| 94 | * |
||
| 95 | * @param string $tpl Substring containing in needed keys |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | 1 | public function delByTemplate($tpl) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Cache cleanup |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 1 | public function clear() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Increment key value |
||
| 121 | * |
||
| 122 | * @param string $key Key |
||
| 123 | * @param int $offset Offset |
||
| 124 | * |
||
| 125 | * @return bool|int |
||
| 126 | */ |
||
| 127 | 1 | public function increment($key, $offset) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Decrement key value |
||
| 134 | * |
||
| 135 | * @param string $key Key |
||
| 136 | * @param int $offset Offset |
||
| 137 | * |
||
| 138 | * @return bool|int |
||
| 139 | */ |
||
| 140 | 1 | public function decrement($key, $offset) |
|
| 144 | } |
||
| 145 |
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.