Total Complexity | 42 |
Total Lines | 220 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AllSetupTeardown 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.
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 AllSetupTeardown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class AllSetupTeardown extends TestCase |
||
17 | { |
||
18 | private string $compatibilityMode; |
||
19 | |||
20 | private ?Spreadsheet $spreadsheet = null; |
||
21 | |||
22 | private ?Worksheet $sheet = null; |
||
23 | |||
24 | protected string $returnArrayAs; |
||
25 | |||
26 | protected function setUp(): void |
||
30 | } |
||
31 | |||
32 | protected function tearDown(): void |
||
33 | { |
||
34 | Functions::setCompatibilityMode($this->compatibilityMode); |
||
35 | $this->sheet = null; |
||
36 | if ($this->spreadsheet !== null) { |
||
37 | $this->spreadsheet->disconnectWorksheets(); |
||
38 | $this->spreadsheet = null; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | protected static function setOpenOffice(): void |
||
43 | { |
||
44 | Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); |
||
45 | } |
||
46 | |||
47 | protected static function setGnumeric(): void |
||
48 | { |
||
49 | Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC); |
||
50 | } |
||
51 | |||
52 | protected function mightHaveException(mixed $expectedResult): void |
||
53 | { |
||
54 | if ($expectedResult === 'exception') { |
||
55 | $this->expectException(CalcException::class); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | protected function setCell(string $cell, mixed $value): void |
||
60 | { |
||
61 | if ($value !== null) { |
||
62 | if (is_string($value) && is_numeric($value)) { |
||
63 | $this->getSheet()->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING); |
||
64 | } else { |
||
65 | $this->getSheet()->getCell($cell)->setValue($value); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | protected function getSpreadsheet(): Spreadsheet |
||
78 | } |
||
79 | |||
80 | protected function getSheet(): Worksheet |
||
81 | { |
||
82 | if ($this->sheet !== null) { |
||
83 | return $this->sheet; |
||
84 | } |
||
85 | $this->sheet = $this->getSpreadsheet()->getActiveSheet(); |
||
86 | |||
87 | return $this->sheet; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Excel handles text/logical/empty cells differently when |
||
92 | * passed directly as arguments as opposed to cell references or arrays. |
||
93 | * This function will test both approaches. |
||
94 | */ |
||
95 | protected function runTestCases(string $functionName, mixed $expectedResult, mixed ...$args): void |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Excel handles text/logical/empty cells differently when |
||
108 | * passed directly as arguments as opposed to cell references or arrays. |
||
109 | * This functions tests passing as arrays. |
||
110 | */ |
||
111 | protected function runTestCaseReference(string $functionName, mixed $expectedResult, mixed ...$args): void |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Excel handles text/logical/empty cells differently when |
||
154 | * passed directly as arguments as opposed to cell references or arrays. |
||
155 | * This functions tests passing as direct arguments. |
||
156 | */ |
||
157 | protected function runTestCaseDirect(string $functionName, mixed $expectedResult, mixed ...$args): void |
||
158 | { |
||
159 | $this->mightHaveException($expectedResult); |
||
160 | $sheet = $this->getSheet(); |
||
161 | $formula = "=$functionName("; |
||
162 | $comma = ''; |
||
163 | foreach ($args as $arg) { |
||
164 | if (is_array($arg)) { |
||
165 | foreach ($arg as $arrayItem) { |
||
166 | $formula .= $comma; |
||
167 | $comma = ','; |
||
168 | if ($arrayItem !== null && !is_scalar($arrayItem) && !($arrayItem instanceof Stringable)) { |
||
169 | self::fail('non-stringable item'); |
||
170 | } |
||
171 | $formula .= $this->convertToString($arrayItem); |
||
172 | } |
||
173 | } else { |
||
174 | $formula .= $comma; |
||
175 | $comma = ','; |
||
176 | /** @var string */ |
||
177 | $argx = $arg; |
||
178 | $formula .= $this->convertToString($argx); |
||
179 | } |
||
180 | } |
||
181 | $formula .= ')'; |
||
182 | $this->setCell('B2', $formula); |
||
183 | self::assertEqualsWithDelta($expectedResult, $sheet->getCell('B2')->getCalculatedValue(), 1.0e-8, 'arguments supplied directly'); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Excel seems to reject bracket notation for literal arrays |
||
188 | * for some functions. |
||
189 | */ |
||
190 | protected function runTestCaseNoBracket(string $functionName, mixed $expectedResult, mixed ...$args): void |
||
224 | } |
||
225 | |||
226 | private function convertToString(null|bool|float|int|string|Stringable $arg): string |
||
238 |