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 |
||
12 | class YamlStorage implements Storage |
||
13 | { |
||
14 | /** |
||
15 | * The data collection. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $data; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | * |
||
24 | * @param string $yamlFile The dir path of YAML file |
||
|
|||
25 | */ |
||
26 | public function __construct($yamlFile = null) |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | View Code Duplication | public function findAll($limit, $offset) |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | View Code Duplication | public function query(array $criteria, $limit, $offset) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function properties() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function size() |
||
88 | |||
89 | /** |
||
90 | * Paginates the given collection of data. |
||
91 | * |
||
92 | * @param array $data The collection data |
||
93 | * @param int $limit Logs per page |
||
94 | * @param int $offset The number of the page |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | private function paginate($data, $limit, $offset) |
||
102 | |||
103 | /** |
||
104 | * Compares the given two items. |
||
105 | * |
||
106 | * @param mixed $item1 The first item |
||
107 | * @param mixed $item2 The second item |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | View Code Duplication | private function sort($item1, $item2) |
|
129 | } |
||
130 |
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.