Complex classes like AbstractCsv 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 AbstractCsv, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | abstract class AbstractCsv implements ByteSequence |
||
35 | { |
||
36 | /** |
||
37 | * The stream filter mode (read or write). |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $stream_filter_mode; |
||
42 | |||
43 | /** |
||
44 | * collection of stream filters. |
||
45 | * |
||
46 | * @var bool[] |
||
47 | */ |
||
48 | protected $stream_filters = []; |
||
49 | |||
50 | /** |
||
51 | * The CSV document BOM sequence. |
||
52 | * |
||
53 | * @var string|null |
||
54 | */ |
||
55 | protected $input_bom = null; |
||
56 | |||
57 | /** |
||
58 | * The Output file BOM character. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $output_bom = ''; |
||
63 | |||
64 | /** |
||
65 | * the field delimiter (one character only). |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $delimiter = ','; |
||
70 | |||
71 | /** |
||
72 | * the field enclosure character (one character only). |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $enclosure = '"'; |
||
77 | |||
78 | /** |
||
79 | * the field escape character (one character only). |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $escape = '\\'; |
||
84 | |||
85 | /** |
||
86 | * The CSV document. |
||
87 | * |
||
88 | * @var SplFileObject|Stream |
||
89 | */ |
||
90 | protected $document; |
||
91 | |||
92 | /** |
||
93 | * Tells whether the Input BOM must be included or skipped. |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | protected $is_input_bom_included = false; |
||
98 | |||
99 | /** |
||
100 | * New instance. |
||
101 | * |
||
102 | * @param SplFileObject|Stream $document The CSV Object instance |
||
103 | */ |
||
104 | 51 | protected function __construct($document) |
|
110 | |||
111 | /** |
||
112 | * Reset dynamic object properties to improve performance. |
||
113 | */ |
||
114 | 48 | protected function resetProperties(): void |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 51 | public function __destruct() |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 3 | public function __clone() |
|
133 | |||
134 | /** |
||
135 | * Return a new instance from a SplFileObject. |
||
136 | * |
||
137 | * @return static |
||
138 | */ |
||
139 | 48 | public static function createFromFileObject(SplFileObject $file) |
|
143 | |||
144 | /** |
||
145 | * Return a new instance from a PHP resource stream. |
||
146 | * |
||
147 | * @param resource $stream |
||
148 | * |
||
149 | * @return static |
||
150 | */ |
||
151 | 6 | public static function createFromStream($stream) |
|
155 | |||
156 | /** |
||
157 | * Return a new instance from a string. |
||
158 | * |
||
159 | * @return static |
||
160 | */ |
||
161 | 18 | public static function createFromString(string $content = '') |
|
165 | |||
166 | /** |
||
167 | * Return a new instance from a file path. |
||
168 | * |
||
169 | * @param resource|null $context the resource context |
||
170 | * |
||
171 | * @return static |
||
172 | */ |
||
173 | 6 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
|
177 | |||
178 | /** |
||
179 | * Returns the current field delimiter. |
||
180 | */ |
||
181 | 21 | public function getDelimiter(): string |
|
185 | |||
186 | /** |
||
187 | * Returns the current field enclosure. |
||
188 | */ |
||
189 | 3 | public function getEnclosure(): string |
|
193 | |||
194 | /** |
||
195 | * Returns the pathname of the underlying document. |
||
196 | */ |
||
197 | 12 | public function getPathname(): string |
|
201 | |||
202 | /** |
||
203 | * Returns the current field escape character. |
||
204 | */ |
||
205 | 3 | public function getEscape(): string |
|
209 | |||
210 | /** |
||
211 | * Returns the BOM sequence in use on Output methods. |
||
212 | */ |
||
213 | 3 | public function getOutputBOM(): string |
|
217 | |||
218 | /** |
||
219 | * Returns the BOM sequence of the given CSV. |
||
220 | */ |
||
221 | 69 | public function getInputBOM(): string |
|
233 | |||
234 | /** |
||
235 | * Returns the stream filter mode. |
||
236 | */ |
||
237 | 3 | public function getStreamFilterMode(): int |
|
241 | |||
242 | /** |
||
243 | * Tells whether the stream filter capabilities can be used. |
||
244 | */ |
||
245 | 6 | public function supportsStreamFilter(): bool |
|
249 | |||
250 | /** |
||
251 | * Tell whether the specify stream filter is attach to the current stream. |
||
252 | */ |
||
253 | 3 | public function hasStreamFilter(string $filtername): bool |
|
257 | |||
258 | /** |
||
259 | * Tells whether the BOM can be stripped if presents. |
||
260 | */ |
||
261 | 3 | public function isInputBOMIncluded(): bool |
|
265 | |||
266 | /** |
||
267 | * Retuns the CSV document as a Generator of string chunk. |
||
268 | * |
||
269 | * @param int $length number of bytes read |
||
270 | * |
||
271 | * @throws Exception if the number of bytes is lesser than 1 |
||
272 | */ |
||
273 | 18 | public function chunk(int $length): Generator |
|
293 | |||
294 | /** |
||
295 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
296 | * |
||
297 | * @deprecated deprecated since version 9.1.0 |
||
298 | * @see AbstractCsv::getContent |
||
299 | * |
||
300 | * Retrieves the CSV content |
||
301 | */ |
||
302 | 3 | public function __toString(): string |
|
306 | |||
307 | /** |
||
308 | * Retrieves the CSV content. |
||
309 | */ |
||
310 | 21 | public function getContent(): string |
|
319 | |||
320 | /** |
||
321 | * Outputs all data on the CSV file. |
||
322 | * |
||
323 | * @return int Returns the number of characters read from the handle |
||
324 | * and passed through to the output. |
||
325 | */ |
||
326 | 12 | public function output(string $filename = null): int |
|
341 | |||
342 | /** |
||
343 | * Send the CSV headers. |
||
344 | * |
||
345 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
346 | * |
||
347 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
348 | * |
||
349 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
350 | */ |
||
351 | 9 | protected function sendHeaders(string $filename): void |
|
376 | |||
377 | /** |
||
378 | * Sets the field delimiter. |
||
379 | * |
||
380 | * @throws Exception If the Csv control character is not one character only. |
||
381 | * |
||
382 | * @return static |
||
383 | */ |
||
384 | 21 | public function setDelimiter(string $delimiter): self |
|
399 | |||
400 | /** |
||
401 | * Sets the field enclosure. |
||
402 | * |
||
403 | * @throws Exception If the Csv control character is not one character only. |
||
404 | * |
||
405 | * @return static |
||
406 | */ |
||
407 | 3 | public function setEnclosure(string $enclosure): self |
|
422 | |||
423 | /** |
||
424 | * Sets the field escape character. |
||
425 | * |
||
426 | * @throws Exception If the Csv control character is not one character only. |
||
427 | * |
||
428 | * @return static |
||
429 | */ |
||
430 | 3 | public function setEscape(string $escape): self |
|
445 | |||
446 | /** |
||
447 | * Enables BOM Stripping. |
||
448 | * |
||
449 | * @return static |
||
450 | */ |
||
451 | 3 | public function skipInputBOM(): self |
|
457 | |||
458 | /** |
||
459 | * Disables skipping Input BOM. |
||
460 | * |
||
461 | * @return static |
||
462 | */ |
||
463 | 6 | public function includeInputBOM(): self |
|
469 | |||
470 | /** |
||
471 | * Sets the BOM sequence to prepend the CSV on output. |
||
472 | * |
||
473 | * @return static |
||
474 | */ |
||
475 | 9 | public function setOutputBOM(string $str): self |
|
481 | |||
482 | /** |
||
483 | * append a stream filter. |
||
484 | * |
||
485 | * @param null|mixed $params |
||
486 | * |
||
487 | * @throws Exception If the stream filter API can not be used |
||
488 | * |
||
489 | * @return static |
||
490 | */ |
||
491 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
504 | } |
||
505 |