Complex classes like WriterAbstract 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 WriterAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | abstract class WriterAbstract implements WriterInterface |
||
26 | { |
||
27 | /** @var string Path to the output file */ |
||
28 | protected $outputFilePath; |
||
29 | |||
30 | /** @var resource Pointer to the file/stream we will write to */ |
||
31 | protected $filePointer; |
||
32 | |||
33 | /** @var bool Indicates whether the writer has been opened or not */ |
||
34 | protected $isWriterOpened = false; |
||
35 | |||
36 | /** @var GlobalFunctionsHelper Helper to work with global functions */ |
||
37 | protected $globalFunctionsHelper; |
||
38 | |||
39 | /** @var OptionsManagerInterface Writer options manager */ |
||
40 | protected $optionsManager; |
||
41 | |||
42 | /** @var StyleMerger Helps merge styles together */ |
||
43 | protected $styleMerger; |
||
44 | |||
45 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
46 | protected static $headerContentType; |
||
47 | |||
48 | /** |
||
49 | * @param OptionsManagerInterface $optionsManager |
||
50 | * @param StyleMerger $styleMerger |
||
51 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
52 | */ |
||
53 | 112 | public function __construct( |
|
62 | |||
63 | /** |
||
64 | * Opens the streamer and makes it ready to accept data. |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
68 | */ |
||
69 | abstract protected function openWriter(); |
||
70 | |||
71 | /** |
||
72 | * Adds a row to the currently opened writer. |
||
73 | * |
||
74 | * @param Row $row The row containing cells and styles |
||
75 | * @return void |
||
76 | */ |
||
77 | abstract protected function addRowToWriter(Row $row); |
||
78 | |||
79 | /** |
||
80 | * Closes the streamer, preventing any additional writing. |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | abstract protected function closeWriter(); |
||
85 | |||
86 | /** |
||
87 | * Sets the default styles for all rows added with "addRow". |
||
88 | * Overriding the default style instead of using "addRowWithStyle" improves performance by 20%. |
||
89 | * @see https://github.com/box/spout/issues/272 |
||
90 | * |
||
91 | * @param Style $defaultStyle |
||
92 | * @return WriterAbstract |
||
93 | */ |
||
94 | 2 | public function setDefaultRowStyle($defaultStyle) |
|
99 | |||
100 | /** |
||
101 | * Inits the writer and opens it to accept data. |
||
102 | * By using this method, the data will be written to a file. |
||
103 | * |
||
104 | * @api |
||
105 | * @param string $outputFilePath Path of the output file that will contain the data |
||
106 | * @return WriterAbstract |
||
107 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable |
||
108 | */ |
||
109 | 101 | public function openToFile($outputFilePath) |
|
121 | |||
122 | /** |
||
123 | * Inits the writer and opens it to accept data. |
||
124 | * By using this method, the data will be outputted directly to the browser. |
||
125 | * |
||
126 | * @codeCoverageIgnore |
||
127 | * |
||
128 | * @api |
||
129 | * @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept |
||
130 | * @return WriterAbstract |
||
131 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
132 | */ |
||
133 | public function openToBrowser($outputFileName) |
||
163 | |||
164 | /** |
||
165 | * Checks if the pointer to the file/stream to write to is available. |
||
166 | * Will throw an exception if not available. |
||
167 | * |
||
168 | * @return void |
||
169 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
170 | */ |
||
171 | 101 | protected function throwIfFilePointerIsNotAvailable() |
|
177 | |||
178 | /** |
||
179 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
180 | * Throws an exception if already opened. |
||
181 | * |
||
182 | * @param string $message Error message |
||
183 | * @return void |
||
184 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
185 | */ |
||
186 | 53 | protected function throwIfWriterAlreadyOpened($message) |
|
192 | |||
193 | /** |
||
194 | * Write given data to the output. New data will be appended to end of stream. |
||
195 | * |
||
196 | * @param array|\Box\Spout\Writer\Common\Entity\Row $row The row to be appended to the stream |
||
197 | * @return WriterInterface |
||
198 | * @internal param array $row Array containing data to be streamed. |
||
199 | * Example $row= ['data1', 1234, null, '', 'data5']; |
||
200 | * @internal param \Box\Spout\Writer\Common\Entity\Row $row A Row object with cells and styles |
||
201 | * Example $row = (new Row())->addCell('data1'); |
||
202 | * |
||
203 | * @throws SpoutException If anything else goes wrong while writing data |
||
204 | * @throws WriterNotOpenedException If this function is called before opening the writer |
||
205 | * |
||
206 | * @api |
||
207 | */ |
||
208 | 82 | public function addRow($row) |
|
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | * |
||
240 | * @api |
||
241 | */ |
||
242 | public function withRow(\Closure $callback) |
||
246 | |||
247 | /** |
||
248 | * Write given data to the output and apply the given style. |
||
249 | * @see addRow |
||
250 | * |
||
251 | * @param array|\Box\Spout\Writer\Common\Entity\Row $row The row to be appended to the stream |
||
252 | * @param Style $style Style to be applied to the row. |
||
253 | * @return WriterInterface |
||
254 | * @internal param array $row Array containing data to be streamed. |
||
255 | * Example $row= ['data1', 1234, null, '', 'data5']; |
||
256 | * @internal param \Box\Spout\Writer\Common\Entity\Row $row A Row object with cells and styles |
||
257 | * Example $row = (new Row())->addCell('data1'); |
||
258 | * @api |
||
259 | * @throws InvalidArgumentException If the input param is not valid |
||
260 | */ |
||
261 | 19 | public function addRowWithStyle($row, $style) |
|
278 | |||
279 | /** |
||
280 | * @param array $dataRows |
||
281 | * @param Style|null $style |
||
282 | * @return Row |
||
283 | */ |
||
284 | protected function createRowFromArray(array $dataRows, Style $style = null) |
||
299 | |||
300 | /** |
||
301 | * Write given data to the output. New data will be appended to end of stream. |
||
302 | * |
||
303 | * @api |
||
304 | * @param array $dataRows Array of array containing data to be streamed. |
||
305 | * If a row is empty, it won't be added (i.e. not even as a blank row) |
||
306 | * Example: $dataRows = [ |
||
307 | * ['data11', 12, , '', 'data13'], |
||
308 | * ['data21', 'data22', null, false], |
||
309 | * ]; |
||
310 | * @return WriterAbstract |
||
311 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid |
||
312 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
313 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
314 | */ |
||
315 | 51 | public function addRows(array $dataRows) |
|
328 | |||
329 | /** |
||
330 | * Write given data to the output and apply the given style. |
||
331 | * @see addRows |
||
332 | * |
||
333 | * @api |
||
334 | * @param array $dataRows Array of array containing data to be streamed. |
||
335 | * @param Style $style Style to be applied to the rows. |
||
336 | * @return WriterAbstract |
||
337 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid |
||
338 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
339 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
340 | */ |
||
341 | 16 | public function addRowsWithStyle(array $dataRows, $style) |
|
362 | |||
363 | /** |
||
364 | * @TODO: Move this into styleMerger |
||
365 | * |
||
366 | * @param Row $row |
||
367 | * @return $this |
||
368 | */ |
||
369 | 72 | private function applyDefaultRowStyle(Row $row) |
|
378 | |||
379 | /** |
||
380 | * Closes the writer. This will close the streamer as well, preventing new data |
||
381 | * to be written to the file. |
||
382 | * |
||
383 | * @api |
||
384 | * @return void |
||
385 | */ |
||
386 | 79 | public function close() |
|
400 | |||
401 | /** |
||
402 | * Closes the writer and attempts to cleanup all files that were |
||
403 | * created during the writing process (temp files & final file). |
||
404 | * |
||
405 | * @return void |
||
406 | */ |
||
407 | 5 | private function closeAndAttemptToCleanupAllFiles() |
|
419 | } |
||
420 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.