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 |
||
8 | class Chart |
||
9 | { |
||
10 | /** |
||
11 | * Chart Name. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | private $name = ''; |
||
16 | |||
17 | /** |
||
18 | * Worksheet. |
||
19 | * |
||
20 | * @var Worksheet |
||
21 | */ |
||
22 | private $worksheet; |
||
23 | |||
24 | /** |
||
25 | * Chart Title. |
||
26 | * |
||
27 | * @var Title |
||
28 | */ |
||
29 | private $title; |
||
30 | |||
31 | /** |
||
32 | * Chart Legend. |
||
33 | * |
||
34 | * @var Legend |
||
35 | */ |
||
36 | private $legend; |
||
37 | |||
38 | /** |
||
39 | * X-Axis Label. |
||
40 | * |
||
41 | * @var Title |
||
42 | */ |
||
43 | private $xAxisLabel; |
||
44 | |||
45 | /** |
||
46 | * Y-Axis Label. |
||
47 | * |
||
48 | * @var Title |
||
49 | */ |
||
50 | private $yAxisLabel; |
||
51 | |||
52 | /** |
||
53 | * Chart Plot Area. |
||
54 | * |
||
55 | * @var PlotArea |
||
56 | */ |
||
57 | private $plotArea; |
||
58 | |||
59 | /** |
||
60 | * Plot Visible Only. |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $plotVisibleOnly = true; |
||
65 | |||
66 | /** |
||
67 | * Display Blanks as. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $displayBlanksAs = '0'; |
||
72 | |||
73 | /** |
||
74 | * Chart Asix Y as. |
||
75 | * |
||
76 | * @var Axis |
||
77 | */ |
||
78 | private $yAxis; |
||
79 | |||
80 | /** |
||
81 | * Chart Asix X as. |
||
82 | * |
||
83 | * @var Axis |
||
84 | */ |
||
85 | private $xAxis; |
||
86 | |||
87 | /** |
||
88 | * Chart Major Gridlines as. |
||
89 | * |
||
90 | * @var GridLines |
||
91 | */ |
||
92 | private $majorGridlines; |
||
93 | |||
94 | /** |
||
95 | * Chart Minor Gridlines as. |
||
96 | * |
||
97 | * @var GridLines |
||
98 | */ |
||
99 | private $minorGridlines; |
||
100 | |||
101 | /** |
||
102 | * Top-Left Cell Position. |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | private $topLeftCellRef = 'A1'; |
||
107 | |||
108 | /** |
||
109 | * Top-Left X-Offset. |
||
110 | * |
||
111 | * @var int |
||
112 | */ |
||
113 | private $topLeftXOffset = 0; |
||
114 | |||
115 | /** |
||
116 | * Top-Left Y-Offset. |
||
117 | * |
||
118 | * @var int |
||
119 | */ |
||
120 | private $topLeftYOffset = 0; |
||
121 | |||
122 | /** |
||
123 | * Bottom-Right Cell Position. |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | private $bottomRightCellRef = 'A1'; |
||
128 | |||
129 | /** |
||
130 | * Bottom-Right X-Offset. |
||
131 | * |
||
132 | * @var int |
||
133 | */ |
||
134 | private $bottomRightXOffset = 10; |
||
135 | |||
136 | /** |
||
137 | * Bottom-Right Y-Offset. |
||
138 | * |
||
139 | * @var int |
||
140 | */ |
||
141 | private $bottomRightYOffset = 10; |
||
142 | |||
143 | /** |
||
144 | * Create a new Chart. |
||
145 | * |
||
146 | * @param mixed $name |
||
147 | * @param mixed $plotVisibleOnly |
||
148 | * @param mixed $displayBlanksAs |
||
149 | */ |
||
150 | 15 | public function __construct($name, Title $title = null, Legend $legend = null, PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = '0', Title $xAxisLabel = null, Title $yAxisLabel = null, Axis $xAxis = null, Axis $yAxis = null, GridLines $majorGridlines = null, GridLines $minorGridlines = null) |
|
165 | |||
166 | /** |
||
167 | * Get Name. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | public function getName() |
|
175 | |||
176 | /** |
||
177 | * Get Worksheet. |
||
178 | * |
||
179 | * @return Worksheet |
||
180 | */ |
||
181 | 13 | public function getWorksheet() |
|
185 | |||
186 | /** |
||
187 | * Set Worksheet. |
||
188 | * |
||
189 | * @param Worksheet $pValue |
||
190 | * |
||
191 | * @throws Exception |
||
192 | * |
||
193 | * @return Chart |
||
194 | */ |
||
195 | 15 | public function setWorksheet(Worksheet $pValue = null) |
|
201 | |||
202 | /** |
||
203 | * Get Title. |
||
204 | * |
||
205 | * @return Title |
||
206 | */ |
||
207 | 14 | public function getTitle() |
|
211 | |||
212 | /** |
||
213 | * Set Title. |
||
214 | * |
||
215 | * @param Title $title |
||
216 | * |
||
217 | * @return Chart |
||
218 | */ |
||
219 | public function setTitle(Title $title) |
||
225 | |||
226 | /** |
||
227 | * Get Legend. |
||
228 | * |
||
229 | * @return Legend |
||
230 | */ |
||
231 | 14 | public function getLegend() |
|
235 | |||
236 | /** |
||
237 | * Set Legend. |
||
238 | * |
||
239 | * @param Legend $legend |
||
240 | * |
||
241 | * @return Chart |
||
242 | */ |
||
243 | public function setLegend(Legend $legend) |
||
249 | |||
250 | /** |
||
251 | * Get X-Axis Label. |
||
252 | * |
||
253 | * @return Title |
||
254 | */ |
||
255 | 14 | public function getXAxisLabel() |
|
259 | |||
260 | /** |
||
261 | * Set X-Axis Label. |
||
262 | * |
||
263 | * @param Title $label |
||
264 | * |
||
265 | * @return Chart |
||
266 | */ |
||
267 | public function setXAxisLabel(Title $label) |
||
273 | |||
274 | /** |
||
275 | * Get Y-Axis Label. |
||
276 | * |
||
277 | * @return Title |
||
278 | */ |
||
279 | 14 | public function getYAxisLabel() |
|
283 | |||
284 | /** |
||
285 | * Set Y-Axis Label. |
||
286 | * |
||
287 | * @param Title $label |
||
288 | * |
||
289 | * @return Chart |
||
290 | */ |
||
291 | public function setYAxisLabel(Title $label) |
||
297 | |||
298 | /** |
||
299 | * Get Plot Area. |
||
300 | * |
||
301 | * @return PlotArea |
||
302 | */ |
||
303 | 14 | public function getPlotArea() |
|
307 | |||
308 | /** |
||
309 | * Get Plot Visible Only. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function getPlotVisibleOnly() |
||
317 | |||
318 | /** |
||
319 | * Set Plot Visible Only. |
||
320 | * |
||
321 | * @param bool $plotVisibleOnly |
||
322 | * |
||
323 | * @return Chart |
||
324 | */ |
||
325 | public function setPlotVisibleOnly($plotVisibleOnly) |
||
331 | |||
332 | /** |
||
333 | * Get Display Blanks as. |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getDisplayBlanksAs() |
||
341 | |||
342 | /** |
||
343 | * Set Display Blanks as. |
||
344 | * |
||
345 | * @param string $displayBlanksAs |
||
346 | * |
||
347 | * @return Chart |
||
348 | */ |
||
349 | public function setDisplayBlanksAs($displayBlanksAs) |
||
353 | |||
354 | /** |
||
355 | * Get yAxis. |
||
356 | * |
||
357 | * @return Axis |
||
358 | */ |
||
359 | 13 | public function getChartAxisY() |
|
367 | |||
368 | /** |
||
369 | * Get xAxis. |
||
370 | * |
||
371 | * @return Axis |
||
372 | */ |
||
373 | 13 | public function getChartAxisX() |
|
381 | |||
382 | /** |
||
383 | * Get Major Gridlines. |
||
384 | * |
||
385 | * @return GridLines |
||
386 | */ |
||
387 | 13 | public function getMajorGridlines() |
|
395 | |||
396 | /** |
||
397 | * Get Minor Gridlines. |
||
398 | * |
||
399 | * @return GridLines |
||
400 | */ |
||
401 | 13 | public function getMinorGridlines() |
|
409 | |||
410 | /** |
||
411 | * Set the Top Left position for the chart. |
||
412 | * |
||
413 | * @param string $cell |
||
414 | * @param int $xOffset |
||
415 | * @param int $yOffset |
||
416 | * |
||
417 | * @return Chart |
||
418 | */ |
||
419 | 15 | public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null) |
|
431 | |||
432 | /** |
||
433 | * Get the top left position of the chart. |
||
434 | * |
||
435 | * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell |
||
436 | */ |
||
437 | 13 | public function getTopLeftPosition() |
|
445 | |||
446 | /** |
||
447 | * Get the cell address where the top left of the chart is fixed. |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | public function getTopLeftCell() |
||
455 | |||
456 | /** |
||
457 | * Set the Top Left cell position for the chart. |
||
458 | * |
||
459 | * @param string $cell |
||
460 | * |
||
461 | * @return Chart |
||
462 | */ |
||
463 | public function setTopLeftCell($cell) |
||
469 | |||
470 | /** |
||
471 | * Set the offset position within the Top Left cell for the chart. |
||
472 | * |
||
473 | * @param int $xOffset |
||
474 | * @param int $yOffset |
||
475 | * |
||
476 | * @return Chart |
||
477 | */ |
||
478 | public function setTopLeftOffset($xOffset, $yOffset) |
||
490 | |||
491 | /** |
||
492 | * Get the offset position within the Top Left cell for the chart. |
||
493 | * |
||
494 | * @return int[] |
||
495 | */ |
||
496 | public function getTopLeftOffset() |
||
503 | |||
504 | 2 | public function setTopLeftXOffset($xOffset) |
|
510 | |||
511 | public function getTopLeftXOffset() |
||
515 | |||
516 | 2 | public function setTopLeftYOffset($yOffset) |
|
522 | |||
523 | public function getTopLeftYOffset() |
||
527 | |||
528 | /** |
||
529 | * Set the Bottom Right position of the chart. |
||
530 | * |
||
531 | * @param string $cell |
||
532 | * @param int $xOffset |
||
533 | * @param int $yOffset |
||
534 | * |
||
535 | * @return Chart |
||
536 | */ |
||
537 | 15 | public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null) |
|
549 | |||
550 | /** |
||
551 | * Get the bottom right position of the chart. |
||
552 | * |
||
553 | * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell |
||
554 | */ |
||
555 | 13 | public function getBottomRightPosition() |
|
563 | |||
564 | public function setBottomRightCell($cell) |
||
570 | |||
571 | /** |
||
572 | * Get the cell address where the bottom right of the chart is fixed. |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function getBottomRightCell() |
||
580 | |||
581 | /** |
||
582 | * Set the offset position within the Bottom Right cell for the chart. |
||
583 | * |
||
584 | * @param int $xOffset |
||
585 | * @param int $yOffset |
||
586 | * |
||
587 | * @return Chart |
||
588 | */ |
||
589 | public function setBottomRightOffset($xOffset, $yOffset) |
||
601 | |||
602 | /** |
||
603 | * Get the offset position within the Bottom Right cell for the chart. |
||
604 | * |
||
605 | * @return int[] |
||
606 | */ |
||
607 | public function getBottomRightOffset() |
||
614 | |||
615 | 2 | public function setBottomRightXOffset($xOffset) |
|
621 | |||
622 | public function getBottomRightXOffset() |
||
626 | |||
627 | 2 | public function setBottomRightYOffset($yOffset) |
|
633 | |||
634 | public function getBottomRightYOffset() |
||
638 | |||
639 | 14 | public function refresh() |
|
645 | |||
646 | /** |
||
647 | * Render the chart to given file (or stream). |
||
648 | * |
||
649 | * @param string $outputDestination Name of the file render to |
||
650 | * |
||
651 | * @return bool true on success |
||
652 | */ |
||
653 | 1 | public function render($outputDestination = null) |
|
671 | } |
||
672 |