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 database record its material and field identifiers. |
||
| 29 | * |
||
| 30 | * @param QueryInterface $query Database query interface |
||
| 31 | * @param integer $materialId material identifier |
||
| 32 | * @param integer $fieldId field identifier |
||
| 33 | */ |
||
| 34 | 3 | public function __construct(QueryInterface $query, $materialId, $fieldId) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Getting quantity images in additional field gallery |
||
| 58 | * |
||
| 59 | * @return integer $count |
||
| 60 | */ |
||
| 61 | 1 | public function getCount() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
||
| 80 | * |
||
| 81 | * @return boolean |
||
| 82 | **/ |
||
| 83 | 3 | public function hasImages() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
| 104 | * all images from gallery table would be returned empty array. |
||
| 105 | * |
||
| 106 | * @param integer $currentPage current page with images. Min value = 1 |
||
| 107 | * @param integer $countView quantity view by page |
||
| 108 | * |
||
| 109 | *@return array |
||
| 110 | * @deprecated Use find() |
||
| 111 | */ |
||
| 112 | public function getImages($currentPage = null, $countView = 20) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Perform SamsonCMS query and get entity gallery images. |
||
| 119 | * |
||
| 120 | * @param int $page Page number |
||
| 121 | * @param int $size Page size |
||
| 122 | * |
||
| 123 | * @return GalleryField[] Collection of entity gallery images |
||
| 124 | */ |
||
| 125 | 2 | public function find($page = null, $size = null) |
|
| 152 | } |
||
| 153 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.