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 |
||
| 3 | class SiteTreeListboxField extends ListboxField |
||
|
|
|||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * |
||
| 8 | * @var Int |
||
| 9 | */ |
||
| 10 | protected $siteTreeParentID = 0; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * |
||
| 14 | * @var Array |
||
| 15 | */ |
||
| 16 | protected $arrayOfAllowedIDs = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $classNamesForItems = array("SiteTree"); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * @var Array |
||
| 27 | */ |
||
| 28 | protected $siteTreeParentAllChildren = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new dropdown field. |
||
| 32 | * |
||
| 33 | * @param string $name The field name |
||
| 34 | * @param string $title The field title |
||
| 35 | * @param array $source An map of the dropdown items |
||
| 36 | * @param string|array $value You can pass an array of values or a single value like a drop down to be selected |
||
| 37 | * @param int $size Optional size of the select element |
||
| 38 | * @param form The parent form |
||
| 39 | */ |
||
| 40 | public function __construct($name, $title = '', $source = array(), $value = '', $size = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * |
||
| 47 | * @param Int $id |
||
| 48 | */ |
||
| 49 | public function setSiteTreeParentID($id) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * |
||
| 56 | * @param Int $id |
||
| 57 | * @param Int | string $array |
||
| 58 | */ |
||
| 59 | public function setSiteTreeParentAndChildClassNames($id, $array) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @param Array | str $array |
||
| 68 | */ |
||
| 69 | public function setClassNamesForItems($array) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @param String $str |
||
| 82 | */ |
||
| 83 | public function addClassNameForItems($str) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param Array | SS_Map |
||
| 91 | */ |
||
| 92 | public function setSource($source) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | * @return Array |
||
| 113 | */ |
||
| 114 | public function getSource() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Int |
||
| 137 | * @return Array |
||
| 138 | */ |
||
| 139 | View Code Duplication | private function getAllChildrenForSiteTreeParent($parentID) |
|
| 156 | } |
||
| 157 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.