Complex classes like Chart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Chart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Chart |
||
28 | { |
||
29 | /** |
||
30 | * Chart Name. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $name = ''; |
||
35 | |||
36 | /** |
||
37 | * Worksheet. |
||
38 | * |
||
39 | * @var Worksheet |
||
40 | */ |
||
41 | private $worksheet; |
||
42 | |||
43 | /** |
||
44 | * Chart Title. |
||
45 | * |
||
46 | * @var Chart\Title |
||
47 | */ |
||
48 | private $title; |
||
49 | |||
50 | /** |
||
51 | * Chart Legend. |
||
52 | * |
||
53 | * @var Chart\Legend |
||
54 | */ |
||
55 | private $legend; |
||
56 | |||
57 | /** |
||
58 | * X-Axis Label. |
||
59 | * |
||
60 | * @var Chart\Title |
||
61 | */ |
||
62 | private $xAxisLabel; |
||
63 | |||
64 | /** |
||
65 | * Y-Axis Label. |
||
66 | * |
||
67 | * @var Chart\Title |
||
68 | */ |
||
69 | private $yAxisLabel; |
||
70 | |||
71 | /** |
||
72 | * Chart Plot Area. |
||
73 | * |
||
74 | * @var Chart\PlotArea |
||
75 | */ |
||
76 | private $plotArea; |
||
77 | |||
78 | /** |
||
79 | * Plot Visible Only. |
||
80 | * |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $plotVisibleOnly = true; |
||
84 | |||
85 | /** |
||
86 | * Display Blanks as. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $displayBlanksAs = '0'; |
||
91 | |||
92 | /** |
||
93 | * Chart Asix Y as. |
||
94 | * |
||
95 | * @var Chart\Axis |
||
96 | */ |
||
97 | private $yAxis; |
||
98 | |||
99 | /** |
||
100 | * Chart Asix X as. |
||
101 | * |
||
102 | * @var Chart\Axis |
||
103 | */ |
||
104 | private $xAxis; |
||
105 | |||
106 | /** |
||
107 | * Chart Major Gridlines as. |
||
108 | * |
||
109 | * @var Chart\GridLines |
||
110 | */ |
||
111 | private $majorGridlines; |
||
112 | |||
113 | /** |
||
114 | * Chart Minor Gridlines as. |
||
115 | * |
||
116 | * @var Chart\GridLines |
||
117 | */ |
||
118 | private $minorGridlines; |
||
119 | |||
120 | /** |
||
121 | * Top-Left Cell Position. |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | private $topLeftCellRef = 'A1'; |
||
126 | |||
127 | /** |
||
128 | * Top-Left X-Offset. |
||
129 | * |
||
130 | * @var int |
||
131 | */ |
||
132 | private $topLeftXOffset = 0; |
||
133 | |||
134 | /** |
||
135 | * Top-Left Y-Offset. |
||
136 | * |
||
137 | * @var int |
||
138 | */ |
||
139 | private $topLeftYOffset = 0; |
||
140 | |||
141 | /** |
||
142 | * Bottom-Right Cell Position. |
||
143 | * |
||
144 | * @var string |
||
145 | */ |
||
146 | private $bottomRightCellRef = 'A1'; |
||
147 | |||
148 | /** |
||
149 | * Bottom-Right X-Offset. |
||
150 | * |
||
151 | * @var int |
||
152 | */ |
||
153 | private $bottomRightXOffset = 10; |
||
154 | |||
155 | /** |
||
156 | * Bottom-Right Y-Offset. |
||
157 | * |
||
158 | * @var int |
||
159 | */ |
||
160 | private $bottomRightYOffset = 10; |
||
161 | |||
162 | /** |
||
163 | * Create a new Chart. |
||
164 | * |
||
165 | * @param mixed $name |
||
166 | * @param mixed $plotVisibleOnly |
||
167 | * @param mixed $displayBlanksAs |
||
168 | */ |
||
169 | 14 | public function __construct($name, Chart\Title $title = null, Chart\Legend $legend = null, Chart\PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = '0', Chart\Title $xAxisLabel = null, Chart\Title $yAxisLabel = null, Chart\Axis $xAxis = null, Chart\Axis $yAxis = null, Chart\GridLines $majorGridlines = null, Chart\GridLines $minorGridlines = null) |
|
170 | { |
||
171 | 14 | $this->name = $name; |
|
172 | 14 | $this->title = $title; |
|
173 | 14 | $this->legend = $legend; |
|
174 | 14 | $this->xAxisLabel = $xAxisLabel; |
|
175 | 14 | $this->yAxisLabel = $yAxisLabel; |
|
176 | 14 | $this->plotArea = $plotArea; |
|
177 | 14 | $this->plotVisibleOnly = $plotVisibleOnly; |
|
178 | 14 | $this->displayBlanksAs = $displayBlanksAs; |
|
179 | 14 | $this->xAxis = $xAxis; |
|
180 | 14 | $this->yAxis = $yAxis; |
|
181 | 14 | $this->majorGridlines = $majorGridlines; |
|
182 | 14 | $this->minorGridlines = $minorGridlines; |
|
183 | 14 | } |
|
184 | |||
185 | /** |
||
186 | * Get Name. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | 1 | public function getName() |
|
191 | { |
||
192 | 1 | return $this->name; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get Worksheet. |
||
197 | * |
||
198 | * @return Worksheet |
||
199 | */ |
||
200 | 13 | public function getWorksheet() |
|
201 | { |
||
202 | 13 | return $this->worksheet; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Set Worksheet. |
||
207 | * |
||
208 | * @param Worksheet $pValue |
||
209 | * |
||
210 | * @throws Chart\Exception |
||
211 | * |
||
212 | * @return Chart |
||
213 | */ |
||
214 | 14 | public function setWorksheet(Worksheet $pValue = null) |
|
215 | { |
||
216 | 14 | $this->worksheet = $pValue; |
|
217 | |||
218 | 14 | return $this; |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get Title. |
||
223 | * |
||
224 | * @return Chart\Title |
||
225 | */ |
||
226 | 13 | public function getTitle() |
|
227 | { |
||
228 | 13 | return $this->title; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Set Title. |
||
233 | * |
||
234 | * @param Chart\Title $title |
||
235 | * |
||
236 | * @return Chart |
||
237 | */ |
||
238 | public function setTitle(Chart\Title $title) |
||
239 | { |
||
240 | $this->title = $title; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Get Legend. |
||
247 | * |
||
248 | * @return Chart\Legend |
||
249 | */ |
||
250 | 13 | public function getLegend() |
|
251 | { |
||
252 | 13 | return $this->legend; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Set Legend. |
||
257 | * |
||
258 | * @param Chart\Legend $legend |
||
259 | * |
||
260 | * @return Chart |
||
261 | */ |
||
262 | public function setLegend(Chart\Legend $legend) |
||
263 | { |
||
264 | $this->legend = $legend; |
||
265 | |||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get X-Axis Label. |
||
271 | * |
||
272 | * @return Chart\Title |
||
273 | */ |
||
274 | 13 | public function getXAxisLabel() |
|
275 | { |
||
276 | 13 | return $this->xAxisLabel; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * Set X-Axis Label. |
||
281 | * |
||
282 | * @param Chart\Title $label |
||
283 | * |
||
284 | * @return Chart |
||
285 | */ |
||
286 | public function setXAxisLabel(Chart\Title $label) |
||
287 | { |
||
288 | $this->xAxisLabel = $label; |
||
289 | |||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get Y-Axis Label. |
||
295 | * |
||
296 | * @return Chart\Title |
||
297 | */ |
||
298 | 13 | public function getYAxisLabel() |
|
299 | { |
||
300 | 13 | return $this->yAxisLabel; |
|
301 | } |
||
302 | |||
303 | /** |
||
304 | * Set Y-Axis Label. |
||
305 | * |
||
306 | * @param Chart\Title $label |
||
307 | * |
||
308 | * @return Chart |
||
309 | */ |
||
310 | public function setYAxisLabel(Chart\Title $label) |
||
311 | { |
||
312 | $this->yAxisLabel = $label; |
||
313 | |||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Get Plot Area. |
||
319 | * |
||
320 | * @return Chart\PlotArea |
||
321 | */ |
||
322 | 13 | public function getPlotArea() |
|
323 | { |
||
324 | 13 | return $this->plotArea; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * Get Plot Visible Only. |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function getPlotVisibleOnly() |
||
336 | |||
337 | /** |
||
338 | * Set Plot Visible Only. |
||
339 | * |
||
340 | * @param bool $plotVisibleOnly |
||
341 | * |
||
342 | * @return Chart |
||
343 | */ |
||
344 | public function setPlotVisibleOnly($plotVisibleOnly) |
||
345 | { |
||
346 | $this->plotVisibleOnly = $plotVisibleOnly; |
||
347 | |||
348 | return $this; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Get Display Blanks as. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getDisplayBlanksAs() |
||
360 | |||
361 | /** |
||
362 | * Set Display Blanks as. |
||
363 | * |
||
364 | * @param string $displayBlanksAs |
||
365 | * |
||
366 | * @return Chart |
||
367 | */ |
||
368 | public function setDisplayBlanksAs($displayBlanksAs) |
||
372 | |||
373 | /** |
||
374 | * Get yAxis. |
||
375 | * |
||
376 | * @return Chart\Axis |
||
377 | */ |
||
378 | 13 | public function getChartAxisY() |
|
386 | |||
387 | /** |
||
388 | * Get xAxis. |
||
389 | * |
||
390 | * @return Chart\Axis |
||
391 | */ |
||
392 | 13 | public function getChartAxisX() |
|
400 | |||
401 | /** |
||
402 | * Get Major Gridlines. |
||
403 | * |
||
404 | * @return Chart\GridLines |
||
405 | */ |
||
406 | 13 | public function getMajorGridlines() |
|
414 | |||
415 | /** |
||
416 | * Get Minor Gridlines. |
||
417 | * |
||
418 | * @return Chart\GridLines |
||
419 | */ |
||
420 | 13 | public function getMinorGridlines() |
|
428 | |||
429 | /** |
||
430 | * Set the Top Left position for the chart. |
||
431 | * |
||
432 | * @param string $cell |
||
433 | * @param int $xOffset |
||
434 | * @param int $yOffset |
||
435 | * |
||
436 | * @return Chart |
||
437 | */ |
||
438 | 14 | public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null) |
|
439 | { |
||
440 | 14 | $this->topLeftCellRef = $cell; |
|
441 | 14 | if (!is_null($xOffset)) { |
|
442 | 1 | $this->setTopLeftXOffset($xOffset); |
|
443 | } |
||
444 | 14 | if (!is_null($yOffset)) { |
|
445 | 1 | $this->setTopLeftYOffset($yOffset); |
|
446 | } |
||
447 | |||
448 | 14 | return $this; |
|
449 | } |
||
450 | |||
451 | /** |
||
452 | * Get the top left position of the chart. |
||
453 | * |
||
454 | * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell |
||
455 | */ |
||
456 | 13 | public function getTopLeftPosition() |
|
464 | |||
465 | /** |
||
466 | * Get the cell address where the top left of the chart is fixed. |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getTopLeftCell() |
||
474 | |||
475 | /** |
||
476 | * Set the Top Left cell position for the chart. |
||
477 | * |
||
478 | * @param string $cell |
||
479 | * |
||
480 | * @return Chart |
||
481 | */ |
||
482 | public function setTopLeftCell($cell) |
||
488 | |||
489 | /** |
||
490 | * Set the offset position within the Top Left cell for the chart. |
||
491 | * |
||
492 | * @param int $xOffset |
||
493 | * @param int $yOffset |
||
494 | * |
||
495 | * @return Chart |
||
496 | */ |
||
497 | public function setTopLeftOffset($xOffset, $yOffset) |
||
509 | |||
510 | /** |
||
511 | * Get the offset position within the Top Left cell for the chart. |
||
512 | * |
||
513 | * @return int[] |
||
514 | */ |
||
515 | public function getTopLeftOffset() |
||
522 | |||
523 | 1 | public function setTopLeftXOffset($xOffset) |
|
524 | { |
||
525 | 1 | $this->topLeftXOffset = $xOffset; |
|
526 | |||
527 | 1 | return $this; |
|
528 | } |
||
529 | |||
530 | public function getTopLeftXOffset() |
||
534 | |||
535 | 1 | public function setTopLeftYOffset($yOffset) |
|
536 | { |
||
537 | 1 | $this->topLeftYOffset = $yOffset; |
|
538 | |||
539 | 1 | return $this; |
|
540 | } |
||
541 | |||
542 | public function getTopLeftYOffset() |
||
546 | |||
547 | /** |
||
548 | * Set the Bottom Right position of the chart. |
||
549 | * |
||
550 | * @param string $cell |
||
551 | * @param int $xOffset |
||
552 | * @param int $yOffset |
||
553 | * |
||
554 | * @return Chart |
||
555 | */ |
||
556 | 14 | public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null) |
|
557 | { |
||
558 | 14 | $this->bottomRightCellRef = $cell; |
|
559 | 14 | if (!is_null($xOffset)) { |
|
560 | 1 | $this->setBottomRightXOffset($xOffset); |
|
561 | } |
||
562 | 14 | if (!is_null($yOffset)) { |
|
563 | 1 | $this->setBottomRightYOffset($yOffset); |
|
564 | } |
||
565 | |||
566 | 14 | return $this; |
|
567 | } |
||
568 | |||
569 | /** |
||
570 | * Get the bottom right position of the chart. |
||
571 | * |
||
572 | * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell |
||
573 | */ |
||
574 | 13 | public function getBottomRightPosition() |
|
582 | |||
583 | public function setBottomRightCell($cell) |
||
589 | |||
590 | /** |
||
591 | * Get the cell address where the bottom right of the chart is fixed. |
||
592 | * |
||
593 | * @return string |
||
594 | */ |
||
595 | public function getBottomRightCell() |
||
599 | |||
600 | /** |
||
601 | * Set the offset position within the Bottom Right cell for the chart. |
||
602 | * |
||
603 | * @param int $xOffset |
||
604 | * @param int $yOffset |
||
605 | * |
||
606 | * @return Chart |
||
607 | */ |
||
608 | public function setBottomRightOffset($xOffset, $yOffset) |
||
620 | |||
621 | /** |
||
622 | * Get the offset position within the Bottom Right cell for the chart. |
||
623 | * |
||
624 | * @return int[] |
||
625 | */ |
||
626 | public function getBottomRightOffset() |
||
633 | |||
634 | 1 | public function setBottomRightXOffset($xOffset) |
|
635 | { |
||
636 | 1 | $this->bottomRightXOffset = $xOffset; |
|
637 | |||
638 | 1 | return $this; |
|
639 | } |
||
640 | |||
641 | public function getBottomRightXOffset() |
||
645 | |||
646 | 1 | public function setBottomRightYOffset($yOffset) |
|
647 | { |
||
648 | 1 | $this->bottomRightYOffset = $yOffset; |
|
649 | |||
650 | 1 | return $this; |
|
651 | } |
||
652 | |||
653 | public function getBottomRightYOffset() |
||
657 | |||
658 | 13 | public function refresh() |
|
664 | |||
665 | public function render($outputDestination = null) |
||
690 | } |
||
691 |