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 |
||
19 | View Code Duplication | class Gallery |
|
|
|||
20 | { |
||
21 | /** @var integer materialFieldId Table materialField identifier */ |
||
22 | protected $materialFieldId = null; |
||
23 | |||
24 | /** @var QueryInterface Database query interface */ |
||
25 | protected $query; |
||
26 | |||
27 | /** |
||
28 | * Constructor Gallery. |
||
29 | * This constructor finds identifier additional field gallery from 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 | public function __construct(QueryInterface $query, $materialId, $fieldId) |
||
54 | |||
55 | /** |
||
56 | * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
||
57 | * |
||
58 | * @return boolean |
||
59 | **/ |
||
60 | public function hasImages() |
||
77 | |||
78 | /** |
||
79 | * Getting quantity images in additional field gallery |
||
80 | * |
||
81 | * @return integer $count |
||
82 | */ |
||
83 | public function getCount() |
||
99 | |||
100 | /** |
||
101 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
102 | * all images from gallery table would be returned empty array. |
||
103 | * |
||
104 | * @param integer $currentPage current page with images. Min value = 1 |
||
105 | * @param integer $countView quantity view by page |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getImages($currentPage = null, $countView = 20) |
||
134 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.