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 |
||
| 8 | class Cache |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var CacheInterface |
||
| 12 | */ |
||
| 13 | private $cacheAdapter; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Cache constructor. |
||
| 17 | * |
||
| 18 | * @param CacheInterface $cacheAdapter |
||
| 19 | */ |
||
| 20 | public function __construct(CacheInterface $cacheAdapter) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param $key |
||
| 27 | * |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | View Code Duplication | public function has($key) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param $key |
||
| 41 | * @param null $default |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function get($key, $default = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param $key |
||
| 60 | * @param $value |
||
| 61 | * @param null $ttl |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function set($key, $value, $ttl = null) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param $values |
||
| 76 | * @param null $ttl |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function setMultiple($values, $ttl = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param $key |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function delete($key) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function clear() |
||
| 118 | } |
||
| 119 |