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 |
||
20 | class Gallery |
||
21 | { |
||
22 | /** @var integer materialFieldId Table materialField identifier */ |
||
23 | protected $materialFieldId = null; |
||
24 | |||
25 | /** @var QueryInterface Database query interface */ |
||
26 | protected $query; |
||
27 | |||
28 | /** |
||
29 | * Constructor Gallery. |
||
30 | * This constructor finds identifier additional field gallery from database record its material and field identifiers. |
||
31 | * |
||
32 | * @param QueryInterface $query Database query interface |
||
33 | * @param integer $materialId material identifier |
||
34 | * @param integer $fieldId field identifier |
||
35 | */ |
||
36 | public function __construct(QueryInterface $query, $materialId, $fieldId) |
||
57 | |||
58 | /** |
||
59 | * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
||
60 | * |
||
61 | * @return boolean |
||
62 | **/ |
||
63 | public function hasImages() |
||
81 | |||
82 | /** |
||
83 | * Getting quantity images in additional field gallery |
||
84 | * |
||
85 | * @return integer $count |
||
86 | */ |
||
87 | public function getCount() |
||
103 | |||
104 | /** |
||
105 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
106 | * all images from gallery table would be returned empty array. |
||
107 | * |
||
108 | * @param integer $currentPage current page with images. Min value = 1 |
||
109 | * @param integer $countView quantity view by page |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getImages($currentPage = null, $countView = 20) |
||
138 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.