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 |
||
| 33 | abstract class AbstractCsv implements ByteSequence |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * The stream filter mode (read or write). |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $stream_filter_mode; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * collection of stream filters. |
||
| 44 | * |
||
| 45 | * @var bool[] |
||
| 46 | */ |
||
| 47 | protected $stream_filters = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The CSV document BOM sequence. |
||
| 51 | * |
||
| 52 | * @var string|null |
||
| 53 | */ |
||
| 54 | protected $input_bom = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The Output file BOM character. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $output_bom = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * the field delimiter (one character only). |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $delimiter = ','; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * the field enclosure character (one character only). |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $enclosure = '"'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * the field escape character (one character only). |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $escape = '\\'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The CSV document. |
||
| 86 | * |
||
| 87 | * @var SplFileObject|Stream |
||
| 88 | */ |
||
| 89 | protected $document; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Tells whether the Input BOM must be included or skipped. |
||
| 93 | * |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | protected $is_input_bom_included = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * New instance. |
||
| 100 | * |
||
| 101 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 102 | */ |
||
| 103 | 39 | protected function __construct($document) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Reset dynamic object properties to improve performance. |
||
| 112 | */ |
||
| 113 | 36 | protected function resetProperties() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | 39 | public function __destruct() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | 3 | public function __clone() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Return a new instance from a SplFileObject. |
||
| 135 | * |
||
| 136 | * @return static |
||
| 137 | */ |
||
| 138 | 39 | public static function createFromFileObject(SplFileObject $file) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Return a new instance from a PHP resource stream. |
||
| 145 | * |
||
| 146 | * @param resource $stream |
||
| 147 | * |
||
| 148 | * @return static |
||
| 149 | */ |
||
| 150 | 6 | public static function createFromStream($stream) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Return a new instance from a string. |
||
| 157 | * |
||
| 158 | * @return static |
||
| 159 | */ |
||
| 160 | 6 | public static function createFromString(string $content = '') |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Return a new instance from a file path. |
||
| 167 | * |
||
| 168 | * @param resource|null $context the resource context |
||
| 169 | * |
||
| 170 | * @return static |
||
| 171 | */ |
||
| 172 | 6 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Returns the current field delimiter. |
||
| 179 | */ |
||
| 180 | 15 | public function getDelimiter(): string |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the current field enclosure. |
||
| 187 | */ |
||
| 188 | 3 | public function getEnclosure(): string |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the pathname of the underlying document. |
||
| 195 | */ |
||
| 196 | 12 | public function getPathname(): string |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the current field escape character. |
||
| 203 | */ |
||
| 204 | 3 | public function getEscape(): string |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the BOM sequence in use on Output methods. |
||
| 211 | */ |
||
| 212 | 3 | public function getOutputBOM(): string |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the BOM sequence of the given CSV. |
||
| 219 | */ |
||
| 220 | 57 | public function getInputBOM(): string |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the stream filter mode. |
||
| 235 | */ |
||
| 236 | 3 | public function getStreamFilterMode(): int |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Tells whether the stream filter capabilities can be used. |
||
| 243 | */ |
||
| 244 | 6 | public function supportsStreamFilter(): bool |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 251 | */ |
||
| 252 | 3 | public function hasStreamFilter(string $filtername): bool |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Tells whether the BOM can be stripped if presents. |
||
| 259 | */ |
||
| 260 | 3 | public function isInputBOMIncluded(): bool |
|
| 261 | { |
||
| 262 | 3 | return $this->is_input_bom_included; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Retuns the CSV document as a Generator of string chunk. |
||
| 267 | * |
||
| 268 | * @param int $length number of bytes read |
||
| 269 | * |
||
| 270 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 271 | */ |
||
| 272 | 18 | public function chunk(int $length): Generator |
|
| 290 | |||
| 291 | /** |
||
| 292 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
| 293 | * |
||
| 294 | * @deprecated deprecated since version 9.1.0 |
||
| 295 | * @see AbstractCsv::getContent |
||
| 296 | * |
||
| 297 | * Retrieves the CSV content |
||
| 298 | */ |
||
| 299 | 3 | public function __toString(): string |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieves the CSV content. |
||
| 306 | */ |
||
| 307 | 21 | public function getContent(): string |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Outputs all data on the CSV file. |
||
| 319 | * |
||
| 320 | * @return int Returns the number of characters read from the handle |
||
| 321 | * and passed through to the output. |
||
| 322 | */ |
||
| 323 | 12 | public function output(string $filename = null): int |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Send the CSV headers. |
||
| 341 | * |
||
| 342 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 343 | * |
||
| 344 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
| 345 | * |
||
| 346 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
| 347 | */ |
||
| 348 | 9 | protected function sendHeaders(string $filename) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Sets the field delimiter. |
||
| 374 | * |
||
| 375 | * @throws Exception If the Csv control character is not one character only. |
||
| 376 | * |
||
| 377 | * @return static |
||
| 378 | */ |
||
| 379 | 18 | public function setDelimiter(string $delimiter): self |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Sets the field enclosure. |
||
| 397 | * |
||
| 398 | * @throws Exception If the Csv control character is not one character only. |
||
| 399 | * |
||
| 400 | * @return static |
||
| 401 | */ |
||
| 402 | 3 | public function setEnclosure(string $enclosure): self |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Sets the field escape character. |
||
| 420 | * |
||
| 421 | * @throws Exception If the Csv control character is not one character only. |
||
| 422 | * |
||
| 423 | * @return static |
||
| 424 | */ |
||
| 425 | 3 | public function setEscape(string $escape): self |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Enables BOM Stripping. |
||
| 443 | * |
||
| 444 | * @return static |
||
| 445 | */ |
||
| 446 | 3 | public function skipInputBOM(): self |
|
| 447 | { |
||
| 448 | 3 | $this->is_input_bom_included = false; |
|
| 449 | |||
| 450 | 3 | return $this; |
|
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Disables skipping Input BOM. |
||
| 455 | * |
||
| 456 | * @return static |
||
| 457 | */ |
||
| 458 | 6 | public function includeInputBOM(): self |
|
| 459 | { |
||
| 460 | 6 | $this->is_input_bom_included = true; |
|
| 461 | |||
| 462 | 6 | return $this; |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 467 | * |
||
| 468 | * @return static |
||
| 469 | */ |
||
| 470 | 9 | public function setOutputBOM(string $str): self |
|
| 476 | |||
| 477 | /** |
||
| 478 | * append a stream filter. |
||
| 479 | * |
||
| 480 | * @param null|mixed $params |
||
| 481 | * |
||
| 482 | * @throws Exception If the stream filter API can not be used |
||
| 483 | * |
||
| 484 | * @return static |
||
| 485 | */ |
||
| 486 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 499 | } |
||
| 500 |