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 |
||
15 | trait TDataGridAggregationFunction |
||
16 | 1 | { |
|
17 | |||
18 | /** |
||
19 | * @var IAggregationFunction[] |
||
20 | */ |
||
21 | private $aggregationFunctions = []; |
||
22 | |||
23 | /** |
||
24 | * @var IAggregationFunction|NULL |
||
25 | */ |
||
26 | private $multipleAggregationFunction; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * @param string $key |
||
31 | * @param IAggregationFunction $aggregationFunction |
||
32 | * @return static |
||
33 | * @throws DataGridException |
||
34 | */ |
||
35 | public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
||
61 | |||
62 | |||
63 | /** |
||
64 | * @param IMultipleAggregationFunction $multipleAggregationFunction |
||
65 | */ |
||
66 | public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction) |
||
82 | |||
83 | |||
84 | /** |
||
85 | * @param IDataSource $dataSource |
||
86 | * @return void |
||
87 | */ |
||
88 | View Code Duplication | public function beforeDataModelFilter(IDataSource $dataSource) |
|
104 | |||
105 | |||
106 | /** |
||
107 | * @param IDataSource $dataSource |
||
108 | * @return void |
||
109 | */ |
||
110 | View Code Duplication | public function afterDataModelFilter(IDataSource $dataSource) |
|
126 | |||
127 | |||
128 | /** |
||
129 | * @param IDataSource $dataSource |
||
130 | * @return void |
||
131 | */ |
||
132 | View Code Duplication | public function afterDataModelPaginated(IDataSource $dataSource) |
|
148 | |||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function hasSomeAggregationFunction() |
||
157 | |||
158 | |||
159 | /** |
||
160 | * @return IAggregationFunction[] |
||
161 | */ |
||
162 | public function getAggregationFunctions() |
||
166 | |||
167 | |||
168 | /** |
||
169 | * @return MultipleAggregationFunction |
||
170 | */ |
||
171 | public function getMultipleAggregationFunction() |
||
175 | |||
176 | } |
||
177 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.