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 |
||
18 | class Gallery |
||
19 | { |
||
20 | /** @var integer materialFieldId Table materialField identifier */ |
||
21 | protected $materialFieldId = null; |
||
22 | |||
23 | /** @var QueryInterface Database query interface */ |
||
24 | protected $query; |
||
25 | |||
26 | /** |
||
27 | * Constructor Gallery. |
||
28 | * This constructor finds identifier additional field gallery from |
||
29 | * database record its material and field identifiers. |
||
30 | * |
||
31 | * @param QueryInterface $query Database query interface |
||
32 | * @param integer $materialId material identifier |
||
33 | * @param integer $fieldId field identifier |
||
34 | */ |
||
35 | 3 | public function __construct(QueryInterface $query, $materialId, $fieldId) |
|
56 | |||
57 | /** |
||
58 | * Getting quantity images in additional field gallery |
||
59 | * |
||
60 | * @return integer $count |
||
61 | */ |
||
62 | 1 | public function getCount() |
|
78 | |||
79 | /** |
||
80 | * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
||
81 | * |
||
82 | * @return boolean |
||
83 | **/ |
||
84 | 3 | public function hasImages() |
|
102 | |||
103 | /** |
||
104 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
105 | * all images from gallery table would be returned empty array. |
||
106 | * |
||
107 | * @param integer $currentPage current page with images. Min value = 1 |
||
108 | * @param integer $countView quantity view by page |
||
109 | * |
||
110 | *@return array |
||
111 | * @deprecated Use find() |
||
112 | */ |
||
113 | 2 | public function getImages($currentPage = null, $countView = 20) |
|
117 | |||
118 | /** |
||
119 | * Perform SamsonCMS query and get entity gallery images. |
||
120 | * |
||
121 | * @param int $page Page number |
||
122 | * @param int $size Page size |
||
123 | * |
||
124 | * @return GalleryField[] Collection of entity gallery images |
||
125 | */ |
||
126 | 2 | public function find($page = null, $size = null) |
|
153 | } |
||
154 |
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.