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 |
||
9 | abstract class CappedStorage |
||
10 | { |
||
11 | use \PHPDaemon\Traits\ClassWatchdog; |
||
12 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
13 | |||
14 | /** |
||
15 | * @var callable Sorter function |
||
16 | */ |
||
17 | public $sorter; |
||
18 | |||
19 | /** |
||
20 | * @var integer Maximum number of cached elements |
||
21 | */ |
||
22 | public $maxCacheSize = 64; |
||
23 | |||
24 | /** |
||
25 | * @var integer Additional window to decrease number of sorter calls |
||
26 | */ |
||
27 | public $capWindow = 16; |
||
28 | |||
29 | /** |
||
30 | * @var array Storage of cached items |
||
31 | */ |
||
32 | public $cache = []; |
||
33 | |||
34 | /** |
||
35 | * Sets cache size |
||
36 | * @param integer $size Maximum number of elements |
||
37 | * @return void |
||
38 | */ |
||
39 | public function setMaxCacheSize($size) |
||
43 | |||
44 | /** |
||
45 | * Sets cap window |
||
46 | * @param integer $w Additional window to decrease number of sorter calls |
||
47 | * @return void |
||
48 | */ |
||
49 | public function setCapWindow($w) |
||
53 | |||
54 | /** |
||
55 | * Puts element in cache |
||
56 | * @param string $key Key |
||
57 | * @param mixed $value Value |
||
58 | * @param integer $ttl Time to live |
||
|
|||
59 | * @return mixed |
||
60 | */ |
||
61 | public function put($key, $value, $ttl = null) |
||
86 | |||
87 | /** |
||
88 | * Hash function |
||
89 | * @param string $key Key |
||
90 | * @return integer |
||
91 | */ |
||
92 | public function hash($key) |
||
96 | |||
97 | /** |
||
98 | * Invalidates cache element |
||
99 | * @param string $key Key |
||
100 | * @return void |
||
101 | */ |
||
102 | public function invalidate($key) |
||
107 | |||
108 | /** |
||
109 | * Gets element by key |
||
110 | * @param string $key Key |
||
111 | * @return object Item |
||
112 | */ |
||
113 | View Code Duplication | public function get($key) |
|
128 | |||
129 | /** |
||
130 | * Gets value of element by key |
||
131 | * @param string $key Key |
||
132 | * @return mixed |
||
133 | */ |
||
134 | View Code Duplication | public function getValue($key) |
|
149 | } |
||
150 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.