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 SiteTreeCheckboxSetField extends CheckboxSetField |
||
|
|||
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='', $form=null, $emptyString=null) |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @param Int $id |
||
49 | */ |
||
50 | public function setSiteTreeParentID($id) |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * @param Int $id |
||
58 | * @param Int | string $array |
||
59 | */ |
||
60 | public function setSiteTreeParentAndChildClassNames($id, $array) |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * @param Array | str $array |
||
69 | */ |
||
70 | public function setClassNamesForItems($array) |
||
80 | |||
81 | /** |
||
82 | * |
||
83 | * @param String $str |
||
84 | */ |
||
85 | public function addClassNameForItems($str) |
||
89 | |||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @return Array |
||
94 | */ |
||
95 | public function getSource() |
||
105 | |||
106 | /** |
||
107 | * @param Int |
||
108 | * @return Array |
||
109 | */ |
||
110 | View Code Duplication | private function getAllChildrenForSiteTreeParent($parentID) |
|
127 | } |
||
128 |
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.