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 |
||
7 | class DataSeries |
||
8 | { |
||
9 | const TYPE_BARCHART = 'barChart'; |
||
10 | const TYPE_BARCHART_3D = 'bar3DChart'; |
||
11 | const TYPE_LINECHART = 'lineChart'; |
||
12 | const TYPE_LINECHART_3D = 'line3DChart'; |
||
13 | const TYPE_AREACHART = 'areaChart'; |
||
14 | const TYPE_AREACHART_3D = 'area3DChart'; |
||
15 | const TYPE_PIECHART = 'pieChart'; |
||
16 | const TYPE_PIECHART_3D = 'pie3DChart'; |
||
17 | const TYPE_DOUGHTNUTCHART = 'doughnutChart'; |
||
18 | const TYPE_DONUTCHART = self::TYPE_DOUGHTNUTCHART; // Synonym |
||
19 | const TYPE_SCATTERCHART = 'scatterChart'; |
||
20 | const TYPE_SURFACECHART = 'surfaceChart'; |
||
21 | const TYPE_SURFACECHART_3D = 'surface3DChart'; |
||
22 | const TYPE_RADARCHART = 'radarChart'; |
||
23 | const TYPE_BUBBLECHART = 'bubbleChart'; |
||
24 | const TYPE_STOCKCHART = 'stockChart'; |
||
25 | const TYPE_CANDLECHART = self::TYPE_STOCKCHART; // Synonym |
||
26 | |||
27 | const GROUPING_CLUSTERED = 'clustered'; |
||
28 | const GROUPING_STACKED = 'stacked'; |
||
29 | const GROUPING_PERCENT_STACKED = 'percentStacked'; |
||
30 | const GROUPING_STANDARD = 'standard'; |
||
31 | |||
32 | const DIRECTION_BAR = 'bar'; |
||
33 | const DIRECTION_HORIZONTAL = self::DIRECTION_BAR; |
||
34 | const DIRECTION_COL = 'col'; |
||
35 | const DIRECTION_COLUMN = self::DIRECTION_COL; |
||
36 | const DIRECTION_VERTICAL = self::DIRECTION_COL; |
||
37 | |||
38 | const STYLE_LINEMARKER = 'lineMarker'; |
||
39 | const STYLE_SMOOTHMARKER = 'smoothMarker'; |
||
40 | const STYLE_MARKER = 'marker'; |
||
41 | const STYLE_FILLED = 'filled'; |
||
42 | |||
43 | /** |
||
44 | * Series Plot Type. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $plotType; |
||
49 | |||
50 | /** |
||
51 | * Plot Grouping Type. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $plotGrouping; |
||
56 | |||
57 | /** |
||
58 | * Plot Direction. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $plotDirection; |
||
63 | |||
64 | /** |
||
65 | * Plot Style. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $plotStyle; |
||
70 | |||
71 | /** |
||
72 | * Order of plots in Series. |
||
73 | * |
||
74 | * @var array of integer |
||
75 | */ |
||
76 | private $plotOrder = []; |
||
77 | |||
78 | /** |
||
79 | * Plot Label. |
||
80 | * |
||
81 | * @var array of DataSeriesValues |
||
82 | */ |
||
83 | private $plotLabel = []; |
||
84 | |||
85 | /** |
||
86 | * Plot Category. |
||
87 | * |
||
88 | * @var array of DataSeriesValues |
||
89 | */ |
||
90 | private $plotCategory = []; |
||
91 | |||
92 | /** |
||
93 | * Smooth Line. |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | private $smoothLine; |
||
98 | |||
99 | /** |
||
100 | * Plot Values. |
||
101 | * |
||
102 | * @var array of DataSeriesValues |
||
103 | */ |
||
104 | private $plotValues = []; |
||
105 | |||
106 | /** |
||
107 | * Create a new DataSeries. |
||
108 | * |
||
109 | * @param null|mixed $plotType |
||
110 | * @param null|mixed $plotGrouping |
||
111 | * @param mixed $plotOrder |
||
112 | * @param mixed $plotLabel |
||
113 | * @param mixed $plotCategory |
||
114 | * @param mixed $plotValues |
||
115 | * @param null|mixed $plotDirection |
||
116 | * @param null|mixed $smoothLine |
||
117 | * @param null|mixed $plotStyle |
||
118 | */ |
||
119 | 14 | public function __construct($plotType = null, $plotGrouping = null, $plotOrder = [], $plotLabel = [], $plotCategory = [], $plotValues = [], $plotDirection = null, $smoothLine = null, $plotStyle = null) |
|
120 | { |
||
121 | 14 | $this->plotType = $plotType; |
|
122 | 14 | $this->plotGrouping = $plotGrouping; |
|
123 | 14 | $this->plotOrder = $plotOrder; |
|
|
|||
124 | 14 | $keys = array_keys($plotValues); |
|
125 | 14 | $this->plotValues = $plotValues; |
|
126 | 14 | if ((count($plotLabel) == 0) || ($plotLabel[$keys[0]] === null)) { |
|
127 | 1 | $plotLabel[$keys[0]] = new DataSeriesValues(); |
|
128 | } |
||
129 | |||
130 | 14 | $this->plotLabel = $plotLabel; |
|
131 | 14 | if ((count($plotCategory) == 0) || ($plotCategory[$keys[0]] === null)) { |
|
132 | 2 | $plotCategory[$keys[0]] = new DataSeriesValues(); |
|
133 | } |
||
134 | 14 | $this->plotCategory = $plotCategory; |
|
135 | 14 | $this->smoothLine = $smoothLine; |
|
136 | 14 | $this->plotStyle = $plotStyle; |
|
137 | |||
138 | 14 | if ($plotDirection === null) { |
|
139 | 13 | $plotDirection = self::DIRECTION_COL; |
|
140 | } |
||
141 | 14 | $this->plotDirection = $plotDirection; |
|
142 | 14 | } |
|
143 | |||
144 | /** |
||
145 | * Get Plot Type. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 13 | public function getPlotType() |
|
150 | { |
||
151 | 13 | return $this->plotType; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set Plot Type. |
||
156 | * |
||
157 | * @param string $plotType |
||
158 | * |
||
159 | * @return DataSeries |
||
160 | */ |
||
161 | public function setPlotType($plotType) |
||
162 | { |
||
163 | $this->plotType = $plotType; |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get Plot Grouping Type. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | 13 | public function getPlotGrouping() |
|
174 | { |
||
175 | 13 | return $this->plotGrouping; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Set Plot Grouping Type. |
||
180 | * |
||
181 | * @param string $groupingType |
||
182 | * |
||
183 | * @return DataSeries |
||
184 | */ |
||
185 | public function setPlotGrouping($groupingType) |
||
186 | { |
||
187 | $this->plotGrouping = $groupingType; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get Plot Direction. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 7 | public function getPlotDirection() |
|
198 | { |
||
199 | 7 | return $this->plotDirection; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set Plot Direction. |
||
204 | * |
||
205 | * @param string $plotDirection |
||
206 | * |
||
207 | * @return DataSeries |
||
208 | */ |
||
209 | 8 | public function setPlotDirection($plotDirection) |
|
210 | { |
||
211 | 8 | $this->plotDirection = $plotDirection; |
|
212 | |||
213 | 8 | return $this; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get Plot Order. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 13 | public function getPlotOrder() |
|
222 | { |
||
223 | 13 | return $this->plotOrder; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Get Plot Labels. |
||
228 | * |
||
229 | * @return array of DataSeriesValues |
||
230 | */ |
||
231 | public function getPlotLabels() |
||
232 | { |
||
233 | return $this->plotLabel; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Get Plot Label by Index. |
||
238 | * |
||
239 | * @param mixed $index |
||
240 | * |
||
241 | * @return DataSeriesValues |
||
242 | */ |
||
243 | 13 | View Code Duplication | public function getPlotLabelByIndex($index) |
244 | { |
||
245 | 13 | $keys = array_keys($this->plotLabel); |
|
246 | 13 | if (in_array($index, $keys)) { |
|
247 | 13 | return $this->plotLabel[$index]; |
|
248 | 1 | } elseif (isset($keys[$index])) { |
|
249 | 1 | return $this->plotLabel[$keys[$index]]; |
|
250 | } |
||
251 | |||
252 | 1 | return false; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get Plot Categories. |
||
257 | * |
||
258 | * @return array of DataSeriesValues |
||
259 | */ |
||
260 | public function getPlotCategories() |
||
261 | { |
||
262 | return $this->plotCategory; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Get Plot Category by Index. |
||
267 | * |
||
268 | * @param mixed $index |
||
269 | * |
||
270 | * @return DataSeriesValues |
||
271 | */ |
||
272 | 13 | View Code Duplication | public function getPlotCategoryByIndex($index) |
273 | { |
||
274 | 13 | $keys = array_keys($this->plotCategory); |
|
275 | 13 | if (in_array($index, $keys)) { |
|
276 | 13 | return $this->plotCategory[$index]; |
|
277 | 10 | } elseif (isset($keys[$index])) { |
|
278 | 1 | return $this->plotCategory[$keys[$index]]; |
|
279 | } |
||
280 | |||
281 | 10 | return false; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get Plot Style. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 13 | public function getPlotStyle() |
|
290 | { |
||
291 | 13 | return $this->plotStyle; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Set Plot Style. |
||
296 | * |
||
297 | * @param string $plotStyle |
||
298 | * |
||
299 | * @return DataSeries |
||
300 | */ |
||
301 | 1 | public function setPlotStyle($plotStyle) |
|
302 | { |
||
303 | 1 | $this->plotStyle = $plotStyle; |
|
304 | |||
305 | 1 | return $this; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get Plot Values. |
||
310 | * |
||
311 | * @return array of DataSeriesValues |
||
312 | */ |
||
313 | public function getPlotValues() |
||
314 | { |
||
315 | return $this->plotValues; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Get Plot Values by Index. |
||
320 | * |
||
321 | * @param mixed $index |
||
322 | * |
||
323 | * @return DataSeriesValues |
||
324 | */ |
||
325 | 13 | View Code Duplication | public function getPlotValuesByIndex($index) |
336 | |||
337 | /** |
||
338 | * Get Number of Plot Series. |
||
339 | * |
||
340 | * @return int |
||
341 | */ |
||
342 | public function getPlotSeriesCount() |
||
346 | |||
347 | /** |
||
348 | * Get Smooth Line. |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 3 | public function getSmoothLine() |
|
356 | |||
357 | /** |
||
358 | * Set Smooth Line. |
||
359 | * |
||
360 | * @param bool $smoothLine |
||
361 | * |
||
362 | * @return DataSeries |
||
363 | */ |
||
364 | public function setSmoothLine($smoothLine) |
||
365 | { |
||
366 | $this->smoothLine = $smoothLine; |
||
367 | |||
368 | return $this; |
||
369 | } |
||
370 | |||
371 | 13 | public function refresh(Worksheet $worksheet) |
|
389 | } |
||
390 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..