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 |
||
22 | class DrawChartHelper extends Helper |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Primary function responsible for the generation of every chart |
||
27 | * |
||
28 | * @param Traces $traces Traces object is an array of Trace objects |
||
29 | * @see \CakeCharts\Utility\Traces |
||
30 | * @param array $layout Any layout option accepted by Plotly.js |
||
31 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
32 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
33 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
34 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
35 | * @return string The generated chart |
||
36 | */ |
||
37 | private function makeChart(Traces $traces, array $layout = [], array $configuration = [], string $id = null) |
||
48 | |||
49 | /** |
||
50 | * Function used to generate single series charts |
||
51 | * |
||
52 | * @param array $x Values to be placed on X axis |
||
53 | * @param array $y Values to be placed on Y axis |
||
54 | * @param string $type Type of trace : can be either "bar", "pie" or "scatter" |
||
55 | * @param string|null $mode Line type : can be either "markers", "lines" or "markers+line" |
||
56 | * @param array $layout Any layout option accepted by Plotly.js |
||
57 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
58 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
59 | * @return string The generated chart |
||
60 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
61 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
62 | */ |
||
63 | public function makeSingleTraceChart( |
||
74 | |||
75 | /** |
||
76 | * Function used to generate multi series charts |
||
77 | * |
||
78 | * @param array $series Multi-dimensional array of series |
||
79 | * $series = [ |
||
80 | * [ |
||
81 | * (array) X values, |
||
82 | * (array) Y values, |
||
83 | * (opt. string) Name of the series, |
||
84 | * (opt. string) Line type ("markers", "lines" or "markers+line") |
||
85 | * ], [...], [...] |
||
86 | * ] |
||
87 | * @param string $type Type of trace : can be either "bar", "pie" or "scatter" |
||
88 | * @param array $layout Any layout option accepted by Plotly.js |
||
89 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
90 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
91 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
92 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
93 | * @return string The generated chart |
||
94 | */ |
||
95 | public function makeMultiTraceChart( |
||
118 | |||
119 | /** |
||
120 | * Every functions below are just syntactic sugar |
||
121 | */ |
||
122 | |||
123 | /** |
||
124 | * BAR CHARTS FUNCTIONS |
||
125 | */ |
||
126 | |||
127 | /** |
||
128 | * Function used to generate a simple bar chart |
||
129 | * |
||
130 | * @param array $x Values to be placed on X axis |
||
131 | * @param array $y Values to be placed on Y axis |
||
132 | * @param array $layout Any layout option accepted by Plotly.js |
||
133 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
134 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
135 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
136 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
137 | * @return string The generated chart |
||
138 | */ |
||
139 | public function simpleBarChart( |
||
148 | |||
149 | /** |
||
150 | * Function used to generate a grouped bar chart |
||
151 | * |
||
152 | * @param array $series Multi-dimensional array of series |
||
153 | * $series = [ |
||
154 | * [ |
||
155 | * (array) X values, |
||
156 | * (array) Y values, |
||
157 | * (opt. string) Name of the series, |
||
158 | * (opt. string) Line type ("markers", "lines" or "markers+line") |
||
159 | * ], [...], [...] |
||
160 | * ] |
||
161 | * @param array $layout Any layout option accepted by Plotly.js |
||
162 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
163 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
164 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
165 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
166 | * @return string The generated chart |
||
167 | */ |
||
168 | View Code Duplication | public function groupedBarChart( |
|
178 | |||
179 | /** |
||
180 | * Function used to generate a stacked bar chart |
||
181 | * |
||
182 | * @param array $series Multi-dimensional array of series |
||
183 | * $series = [ |
||
184 | * [ |
||
185 | * (array) X values, |
||
186 | * (array) Y values, |
||
187 | * (opt. string) Name of the series, |
||
188 | * (opt. string) Line type ("markers", "lines" or "markers+line") |
||
189 | * ], [...], [...] |
||
190 | * ] |
||
191 | * @param array $layout Any layout option accepted by Plotly.js |
||
192 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
193 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
194 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
195 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
196 | * @return string The generated chart |
||
197 | */ |
||
198 | View Code Duplication | public function stackedBarChart( |
|
208 | |||
209 | /** |
||
210 | * PIE CHART FUNCTION |
||
211 | */ |
||
212 | |||
213 | /** |
||
214 | * Function used to generate a simple pie chart |
||
215 | * |
||
216 | * @param array $x Values to be placed on X axis |
||
217 | * @param array $y Values to be placed on Y axis |
||
218 | * @param array $layout Any layout option accepted by Plotly.js |
||
219 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
220 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
221 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
222 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
223 | * @return string The generated chart |
||
224 | */ |
||
225 | public function pieChart( |
||
234 | |||
235 | /** |
||
236 | * LINE CHARTS FUNCTIONS |
||
237 | */ |
||
238 | |||
239 | /** |
||
240 | * Function used to generate a single line chart |
||
241 | * |
||
242 | * @param array $x Values to be placed on X axis |
||
243 | * @param array $y Values to be placed on Y axis |
||
244 | * @param string $mode Can be either "markers", "lines" or "markers+line" |
||
245 | * @param array $layout Any layout option accepted by Plotly.js |
||
246 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
247 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
248 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
249 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
250 | * @return string The generated chart |
||
251 | */ |
||
252 | public function singleLineChart( |
||
262 | |||
263 | /** |
||
264 | * Function used to generate a multiline chart |
||
265 | * |
||
266 | * @param array $series Multi-dimensional array of series |
||
267 | * $series = [ |
||
268 | * [ |
||
269 | * (array) X values, |
||
270 | * (array) Y values, |
||
271 | * (opt. string) Name of the series, |
||
272 | * (opt. string) Line type ("markers", "lines" or "markers+line") |
||
273 | * ], [...], [...] |
||
274 | * ] |
||
275 | * @param array $layout Any layout option accepted by Plotly.js |
||
276 | * @see https://plot.ly/javascript/#layout-options for possible values and examples |
||
277 | * @param array $configuration Any configuration option accepted by Plotly.js |
||
278 | * @see https://plot.ly/javascript/configuration-options for possible values and examples |
||
279 | * @param string|null $id HTML identifier of div where chart will be drawed |
||
280 | * @return string The generated chart |
||
281 | */ |
||
282 | public function multilineChart( |
||
290 | } |
||
291 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.