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 |
||
7 | class CodeIgniterCache implements CacheInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $cache; |
||
13 | |||
14 | /** |
||
15 | * @param array $driver |
||
16 | */ |
||
17 | public function __construct($driver) |
||
21 | |||
22 | /** |
||
23 | * Determine if an item exists in the cache. |
||
24 | * |
||
25 | * @param string $key |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function has($key) |
||
32 | |||
33 | /** |
||
34 | * Retrieve an item from the cache by key. |
||
35 | * |
||
36 | * @param string $key |
||
37 | * @param mixed $default |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function get($key, $default = null) |
||
48 | |||
49 | /** |
||
50 | * Retrieve an item from the cache and delete it. |
||
51 | * |
||
52 | * @param string $key |
||
53 | * @param mixed $default |
||
54 | * @return mixed |
||
55 | */ |
||
56 | public function pull($key, $default = null) |
||
67 | |||
68 | /** |
||
69 | * Store an item in the cache. |
||
70 | * |
||
71 | * @param string $key |
||
72 | * @param mixed $value |
||
73 | * @param \DateTime|int $minutes |
||
74 | * @return void |
||
75 | */ |
||
76 | View Code Duplication | public function put($key, $value, $minutes) |
|
86 | } |
||
87 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.