Conditions | 3 |
Paths | 3 |
Total Lines | 94 |
Code Lines | 79 |
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 |
||
34 | public function testZeroPriority(): void |
||
35 | { |
||
36 | $spreadsheet = new Spreadsheet(); |
||
37 | $sheet = $spreadsheet->getActiveSheet(); |
||
38 | $sheet->fromArray([ |
||
39 | [1, 1, 1, 1], |
||
40 | [2, 2, 2, 2], |
||
41 | [3, 3, 3, 3], |
||
42 | [4, 4, 4, 4], |
||
43 | [5, 5, 5, 5], |
||
44 | ]); |
||
45 | |||
46 | $range = 'A1:A5'; |
||
47 | $styles = []; |
||
48 | $new = new Conditional(); |
||
49 | $new->setConditionType(Conditional::CONDITION_CELLIS) |
||
50 | ->setOperatorType(Conditional::OPERATOR_EQUAL) |
||
51 | ->setPriority(30) |
||
52 | ->setConditions(['3']) |
||
53 | ->getStyle() |
||
54 | ->getFill() |
||
55 | ->setFillType(Fill::FILL_SOLID) |
||
56 | ->getStartColor() |
||
57 | ->setArgb('FFC00000'); |
||
58 | $styles[] = $new; |
||
59 | $sheet->setConditionalStyles($range, $styles); |
||
60 | |||
61 | $range = 'B1:B5'; |
||
62 | $styles = []; |
||
63 | $new = new Conditional(); |
||
64 | $new->setConditionType(Conditional::CONDITION_EXPRESSION) |
||
65 | ->setConditions('=MOD(A1,2)=0') |
||
66 | ->getStyle() |
||
67 | ->getFill() |
||
68 | ->setFillType(Fill::FILL_SOLID) |
||
69 | ->getStartColor() |
||
70 | ->setArgb('FF00B0F0'); |
||
71 | $styles[] = $new; |
||
72 | $new = new Conditional(); |
||
73 | $new->setConditionType(Conditional::CONDITION_CELLIS) |
||
74 | ->setOperatorType(Conditional::OPERATOR_EQUAL) |
||
75 | ->setPriority(40) |
||
76 | ->setConditions(['4']) |
||
77 | ->getStyle() |
||
78 | ->getFill() |
||
79 | ->setFillType(Fill::FILL_SOLID) |
||
80 | ->getStartColor() |
||
81 | ->setArgb('FFFFC000'); |
||
82 | $styles[] = $new; |
||
83 | $sheet->setConditionalStyles($range, $styles); |
||
84 | |||
85 | $range = 'C1:C5'; |
||
86 | $styles = []; |
||
87 | $new = new Conditional(); |
||
88 | $new->setConditionType(Conditional::CONDITION_CELLIS) |
||
89 | ->setOperatorType(Conditional::OPERATOR_EQUAL) |
||
90 | ->setPriority(20) |
||
91 | ->setConditions(['2']) |
||
92 | ->getStyle() |
||
93 | ->getFill() |
||
94 | ->setFillType(Fill::FILL_SOLID) |
||
95 | ->getStartColor() |
||
96 | ->setArgb('FFFFFF00'); |
||
97 | $styles[] = $new; |
||
98 | $new = new Conditional(); |
||
99 | $new->setConditionType(Conditional::CONDITION_CELLIS) |
||
100 | ->setOperatorType(Conditional::OPERATOR_EQUAL) |
||
101 | ->setConditions(['5']) |
||
102 | ->getStyle() |
||
103 | ->getFill() |
||
104 | ->setFillType(Fill::FILL_SOLID) |
||
105 | ->getStartColor() |
||
106 | ->setArgb('FF008080'); |
||
107 | $styles[] = $new; |
||
108 | $sheet->setConditionalStyles($range, $styles); |
||
109 | |||
110 | $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); |
||
111 | $spreadsheet->disconnectWorksheets(); |
||
112 | $worksheet = $reloadedSpreadsheet->getActiveSheet(); |
||
113 | $priorities = []; |
||
114 | foreach ($worksheet->getConditionalStylesCollection() as $conditionalStyles) { |
||
115 | foreach ($conditionalStyles as $conditional) { |
||
116 | $priorities[] = $conditional->getPriority(); |
||
117 | } |
||
118 | } |
||
119 | // B1:B5 is written in order 41, 40, but Reader sorts them |
||
120 | $expected = [30, 40, 41, 20, 42]; |
||
121 | self::assertSame($expected, $priorities); |
||
122 | $styles = $worksheet->getConditionalStyles('B1:B5'); |
||
123 | self::assertSame(Conditional::CONDITION_CELLIS, $styles[0]->getConditionType()); |
||
124 | self::assertSame(40, $styles[0]->getPriority()); |
||
125 | self::assertSame(Conditional::CONDITION_EXPRESSION, $styles[1]->getConditionType()); |
||
126 | self::assertSame(41, $styles[1]->getPriority()); |
||
127 | $reloadedSpreadsheet->disconnectWorksheets(); |
||
128 | } |
||
130 |