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 |
||
| 36 | abstract class AbstractCsv implements ByteSequence |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * The stream filter mode (read or write). |
||
| 40 | * |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $stream_filter_mode; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * collection of stream filters. |
||
| 47 | * |
||
| 48 | * @var bool[] |
||
| 49 | */ |
||
| 50 | protected $stream_filters = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The CSV document BOM sequence. |
||
| 54 | * |
||
| 55 | * @var string|null |
||
| 56 | */ |
||
| 57 | protected $input_bom = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The Output file BOM character. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $output_bom = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * the field delimiter (one character only). |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $delimiter = ','; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * the field enclosure character (one character only). |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $enclosure = '"'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * the field escape character (one character only). |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $escape = '\\'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The CSV document. |
||
| 89 | * |
||
| 90 | * @var SplFileObject|Stream |
||
| 91 | */ |
||
| 92 | protected $document; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * New instance. |
||
| 96 | * |
||
| 97 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 98 | */ |
||
| 99 | 36 | protected function __construct($document) |
|
| 100 | { |
||
| 101 | 36 | $this->document = $document; |
|
| 102 | 36 | list($this->delimiter, $this->enclosure, $this->escape) = $this->document->getCsvControl(); |
|
| 103 | 36 | $this->resetProperties(); |
|
| 104 | 36 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Reset dynamic object properties to improve performance. |
||
| 108 | */ |
||
| 109 | 33 | protected function resetProperties() |
|
| 110 | { |
||
| 111 | 33 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | 36 | public function __destruct() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 3 | public function __clone() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Return a new instance from a SplFileObject. |
||
| 131 | * |
||
| 132 | * @return static |
||
| 133 | */ |
||
| 134 | 36 | public static function createFromFileObject(SplFileObject $file) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Return a new instance from a PHP resource stream. |
||
| 141 | * |
||
| 142 | * @param resource $stream |
||
| 143 | * |
||
| 144 | * @return static |
||
| 145 | */ |
||
| 146 | 3 | public static function createFromStream($stream) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Return a new instance from a string. |
||
| 153 | * |
||
| 154 | * @return static |
||
| 155 | */ |
||
| 156 | 6 | public static function createFromString(string $content = '') |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Return a new instance from a file path. |
||
| 163 | * |
||
| 164 | * @param resource|null $context the resource context |
||
| 165 | * |
||
| 166 | * @return static |
||
| 167 | */ |
||
| 168 | 6 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the current field delimiter. |
||
| 175 | */ |
||
| 176 | 15 | public function getDelimiter(): string |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the current field enclosure. |
||
| 183 | */ |
||
| 184 | 3 | public function getEnclosure(): string |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the pathname of the underlying document. |
||
| 191 | */ |
||
| 192 | 12 | public function getPathname(): string |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the current field escape character. |
||
| 199 | */ |
||
| 200 | 3 | public function getEscape(): string |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Returns the BOM sequence in use on Output methods. |
||
| 207 | */ |
||
| 208 | 3 | public function getOutputBOM(): string |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Returns the BOM sequence of the given CSV. |
||
| 215 | */ |
||
| 216 | 54 | public function getInputBOM(): string |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the stream filter mode. |
||
| 231 | */ |
||
| 232 | 3 | public function getStreamFilterMode(): int |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Tells whether the stream filter capabilities can be used. |
||
| 239 | */ |
||
| 240 | 6 | public function supportsStreamFilter(): bool |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 247 | */ |
||
| 248 | 3 | public function hasStreamFilter(string $filtername): bool |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Retuns the CSV document as a Generator of string chunk. |
||
| 255 | * |
||
| 256 | * @param int $length number of bytes read |
||
| 257 | * |
||
| 258 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 259 | */ |
||
| 260 | 15 | public function chunk(int $length): Generator |
|
| 277 | |||
| 278 | /** |
||
| 279 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
| 280 | * |
||
| 281 | * @deprecated deprecated since version 9.1.0 |
||
| 282 | * @see AbstractCsv::getContent |
||
| 283 | * |
||
| 284 | * Retrieves the CSV content |
||
| 285 | */ |
||
| 286 | 3 | public function __toString(): string |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Retrieves the CSV content. |
||
| 293 | */ |
||
| 294 | 18 | public function getContent(): string |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Outputs all data on the CSV file. |
||
| 306 | * |
||
| 307 | * @return int Returns the number of characters read from the handle |
||
| 308 | * and passed through to the output. |
||
| 309 | */ |
||
| 310 | 9 | public function output(string $filename = null): int |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Send the CSV headers. |
||
| 325 | * |
||
| 326 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 327 | * |
||
| 328 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
| 329 | * |
||
| 330 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
| 331 | */ |
||
| 332 | 9 | protected function sendHeaders(string $filename) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Sets the field delimiter. |
||
| 358 | * |
||
| 359 | * @throws Exception If the Csv control character is not one character only. |
||
| 360 | * |
||
| 361 | * @return static |
||
| 362 | */ |
||
| 363 | 18 | public function setDelimiter(string $delimiter): self |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the field enclosure. |
||
| 381 | * |
||
| 382 | * @throws Exception If the Csv control character is not one character only. |
||
| 383 | * |
||
| 384 | * @return static |
||
| 385 | */ |
||
| 386 | 3 | public function setEnclosure(string $enclosure): self |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Sets the field escape character. |
||
| 404 | * |
||
| 405 | * @throws Exception If the Csv control character is not one character only. |
||
| 406 | * |
||
| 407 | * @return static |
||
| 408 | */ |
||
| 409 | 3 | public function setEscape(string $escape): self |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 427 | * |
||
| 428 | * @return static |
||
| 429 | */ |
||
| 430 | 9 | public function setOutputBOM(string $str): self |
|
| 436 | |||
| 437 | /** |
||
| 438 | * append a stream filter. |
||
| 439 | * |
||
| 440 | * @param null|mixed $params |
||
| 441 | * |
||
| 442 | * @throws Exception If the stream filter API can not be used |
||
| 443 | * |
||
| 444 | * @return static |
||
| 445 | */ |
||
| 446 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 459 | } |
||
| 460 |