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 | 1 | trait TDataGridAggregationFunction |
|
16 | { |
||
17 | /** |
||
18 | * @var IAggregationFunction[] |
||
19 | */ |
||
20 | private $aggregationFunctions = []; |
||
21 | |||
22 | /** |
||
23 | * @var IAggregationFunction|NULL |
||
24 | */ |
||
25 | private $multipleAggregationFunction; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @param string $key |
||
30 | * @param IAggregationFunction $aggregationFunction |
||
31 | * @return static |
||
32 | * @throws DataGridException |
||
33 | */ |
||
34 | public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @param IMultipleAggregationFunction $multipleAggregationFunction |
||
60 | * @throws DataGridException |
||
61 | * @return static |
||
62 | */ |
||
63 | public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction) |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @param IDataSource $dataSource |
||
81 | * @throws DataGridException |
||
82 | * @return void |
||
83 | */ |
||
84 | View Code Duplication | public function beforeDataModelFilter(IDataSource $dataSource) |
|
85 | { |
||
86 | 1 | if (!$this->hasSomeAggregationFunction()) { |
|
87 | 1 | return; |
|
88 | } |
||
89 | |||
90 | if (!$dataSource instanceof IAggregatable) { |
||
91 | throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
||
92 | } |
||
93 | |||
94 | if ($this->multipleAggregationFunction) { |
||
95 | if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
||
96 | $dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
||
97 | } |
||
98 | |||
99 | return; |
||
100 | } |
||
101 | |||
102 | foreach ($this->aggregationFunctions as $aggregationFunction) { |
||
103 | if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
||
104 | $dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @param IDataSource $dataSource |
||
112 | * @throws DataGridException |
||
113 | * @return void |
||
114 | */ |
||
115 | View Code Duplication | public function afterDataModelFilter(IDataSource $dataSource) |
|
116 | { |
||
117 | 1 | if (!$this->hasSomeAggregationFunction()) { |
|
118 | 1 | return; |
|
119 | } |
||
120 | |||
121 | if (!$dataSource instanceof IAggregatable) { |
||
122 | throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
||
123 | } |
||
124 | |||
125 | if ($this->multipleAggregationFunction) { |
||
126 | if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
||
127 | $dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
||
128 | } |
||
129 | |||
130 | return; |
||
131 | } |
||
132 | |||
133 | foreach ($this->aggregationFunctions as $aggregationFunction) { |
||
134 | if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
||
135 | $dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @param IDataSource $dataSource |
||
143 | * @throws DataGridException |
||
144 | * @return void |
||
145 | */ |
||
146 | View Code Duplication | public function afterDataModelPaginated(IDataSource $dataSource) |
|
147 | { |
||
148 | if (!$this->hasSomeAggregationFunction()) { |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | if (!$dataSource instanceof IAggregatable) { |
||
153 | throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
||
154 | } |
||
155 | |||
156 | if ($this->multipleAggregationFunction) { |
||
157 | if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
||
158 | $dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
||
159 | } |
||
160 | |||
161 | return; |
||
162 | } |
||
163 | |||
164 | foreach ($this->aggregationFunctions as $aggregationFunction) { |
||
165 | if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
||
166 | $dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function hasSomeAggregationFunction() |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @return IAggregationFunction[] |
||
183 | */ |
||
184 | public function getAggregationFunctions() |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @return MultipleAggregationFunction |
||
192 | */ |
||
193 | public function getMultipleAggregationFunction() |
||
197 | } |
||
198 |
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.