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 | class Line |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $text; |
||
15 | |||
16 | /** |
||
17 | * @var int |
||
18 | */ |
||
19 | private $numberOfItems; |
||
20 | |||
21 | /** |
||
22 | * @param int $nbOfItems |
||
23 | */ |
||
24 | 3 | public function setNumberOfItems($nbOfItems) |
|
28 | |||
29 | /** |
||
30 | * @return int |
||
31 | */ |
||
32 | 2 | public function getNumberOfItems() |
|
36 | |||
37 | /** |
||
38 | * @param string $text |
||
39 | */ |
||
40 | 3 | public function setText($text) |
|
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | 2 | public function getText() |
|
52 | |||
53 | /** |
||
54 | * @param string $text |
||
|
|||
55 | * @param int $numberOfItems |
||
56 | */ |
||
57 | 3 | public function __construct($text = null, $numberOfItems = null) |
|
66 | |||
67 | /** |
||
68 | * Return the object as an array for usage in the XML |
||
69 | * |
||
70 | * @param \DomDocument $document |
||
71 | * @param string $prefix |
||
72 | * @return \DOMElement |
||
73 | */ |
||
74 | 2 | public function toXML(\DomDocument $document, $prefix = null) |
|
110 | |||
111 | /** |
||
112 | * @param \SimpleXMLElement $xml |
||
113 | * @return Line |
||
114 | */ |
||
115 | 1 | public static function createFromXML(\SimpleXMLElement $xml) |
|
127 | } |
||
128 |
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.