Conditions | 1 |
Paths | 1 |
Total Lines | 66 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function testLabelFont(): void |
||
31 | { |
||
32 | $spreadsheet = new Spreadsheet(); |
||
33 | $worksheet = $spreadsheet->getActiveSheet(); |
||
34 | |||
35 | // Sample data for pie chart |
||
36 | $data = [ |
||
37 | ['Category', 'Value'], |
||
38 | ['Category A', 40], |
||
39 | ['Category B', 30], |
||
40 | ['Category C', 20], |
||
41 | ['Category D', 10], |
||
42 | ]; |
||
43 | $worksheet->fromArray($data, null, 'A1'); |
||
44 | $worksheet->getColumnDimension('A')->setAutoSize(true); |
||
45 | |||
46 | // Create data series for the pie chart |
||
47 | $categories = [new DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4)]; |
||
48 | $values = [new DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4)]; |
||
49 | |||
50 | // Set layout for data labels |
||
51 | $font = new Font(); |
||
52 | $font->setName('Times New Roman'); |
||
53 | $font->setSize(8); |
||
54 | $layout = new Layout(); |
||
55 | $layout->setShowVal(true); // Display values |
||
56 | $layout->setShowCatName(true); // Display category names |
||
57 | $layout->setLabelFont($font); |
||
58 | |||
59 | $series = new DataSeries( |
||
60 | DataSeries::TYPE_PIECHART, // Chart type: Pie chart |
||
61 | null, |
||
62 | range(0, count($values) - 1), |
||
63 | [], |
||
64 | $categories, |
||
65 | $values |
||
66 | ); |
||
67 | |||
68 | $plotArea = new PlotArea($layout, [$series]); |
||
69 | $chart = new Chart('Pie Chart', null, null, $plotArea); |
||
70 | $chart->setTopLeftPosition('A7'); |
||
71 | $chart->setBottomRightPosition('H20'); |
||
72 | $worksheet->addChart($chart); |
||
73 | |||
74 | /** @var callable */ |
||
75 | $callableReader = [$this, 'readCharts']; |
||
76 | /** @var callable */ |
||
77 | $callableWriter = [$this, 'writeCharts']; |
||
78 | $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter); |
||
79 | $spreadsheet->disconnectWorksheets(); |
||
80 | |||
81 | $sheet = $reloadedSpreadsheet->getActiveSheet(); |
||
82 | $charts = $sheet->getChartCollection(); |
||
83 | self::assertCount(1, $charts); |
||
84 | $chart2 = $charts[0]; |
||
85 | self::assertNotNull($chart2); |
||
86 | $plotArea2 = $chart2->getPlotArea(); |
||
87 | self::assertNotNull($plotArea2); |
||
88 | $layout2 = $plotArea2->getLayout(); |
||
89 | self::assertNotNull($layout2); |
||
90 | $font2 = $layout2->getLabelFont(); |
||
91 | self::assertNotNull($font2); |
||
92 | self::assertSame('Times New Roman', $font2->getLatin()); |
||
93 | self::assertSame(8.0, $font2->getSize()); |
||
94 | |||
95 | $reloadedSpreadsheet->disconnectWorksheets(); |
||
96 | } |
||
98 |