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 |
||
| 42 | abstract class AbstractCsv implements ByteSequence |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * The stream filter mode (read or write). |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $stream_filter_mode; |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * collection of stream filters. |
||
| 54 | * |
||
| 55 | * @var bool[] |
||
| 56 | */ |
||
| 57 | protected $stream_filters = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The CSV document BOM sequence. |
||
| 61 | * |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | protected $input_bom = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The Output file BOM character. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $output_bom = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * the field delimiter (one character only). |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $delimiter = ','; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * the field enclosure character (one character only). |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $enclosure = '"'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * the field escape character (one character only). |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | 22 | */ |
|
| 92 | protected $escape = '\\'; |
||
| 93 | 22 | ||
| 94 | 22 | /** |
|
| 95 | 22 | * The CSV document. |
|
| 96 | * |
||
| 97 | * @var SplFileObject|Stream |
||
| 98 | */ |
||
| 99 | protected $document; |
||
| 100 | 24 | ||
| 101 | /** |
||
| 102 | 24 | * New instance. |
|
| 103 | 24 | * |
|
| 104 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 105 | */ |
||
| 106 | protected function __construct($document) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function __destruct() |
||
| 119 | |||
| 120 | 24 | /** |
|
| 121 | * {@inheritdoc} |
||
| 122 | 24 | */ |
|
| 123 | public function __clone() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return a new instance from a SplFileObject. |
||
| 130 | * |
||
| 131 | * @return static |
||
| 132 | 2 | */ |
|
| 133 | public static function createFromFileObject(SplFileObject $file) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Return a new instance from a PHP resource stream. |
||
| 140 | * |
||
| 141 | * @param resource $stream |
||
| 142 | * |
||
| 143 | * @return static |
||
| 144 | 4 | */ |
|
| 145 | public static function createFromStream($stream) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return a new instance from a string. |
||
| 152 | * |
||
| 153 | * @return static |
||
| 154 | */ |
||
| 155 | public static function createFromString(string $content) |
||
| 159 | |||
| 160 | 2 | /** |
|
| 161 | * Return a new instance from a file path. |
||
| 162 | * |
||
| 163 | * @param resource|null $context the resource context |
||
| 164 | * |
||
| 165 | * @return static |
||
| 166 | */ |
||
| 167 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the current field delimiter. |
||
| 174 | */ |
||
| 175 | public function getDelimiter(): string |
||
| 179 | |||
| 180 | 2 | /** |
|
| 181 | * Returns the current field enclosure. |
||
| 182 | */ |
||
| 183 | public function getEnclosure(): string |
||
| 187 | |||
| 188 | 2 | /** |
|
| 189 | * Returns the current field escape character. |
||
| 190 | 2 | */ |
|
| 191 | public function getEscape(): string |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the BOM sequence in use on Output methods. |
||
| 198 | 2 | */ |
|
| 199 | public function getOutputBOM(): string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the BOM sequence of the given CSV. |
||
| 206 | */ |
||
| 207 | public function getInputBOM(): string |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the stream filter mode. |
||
| 223 | */ |
||
| 224 | public function getStreamFilterMode(): int |
||
| 228 | |||
| 229 | 2 | /** |
|
| 230 | * Tells whether the stream filter capabilities can be used. |
||
| 231 | */ |
||
| 232 | public function supportsStreamFilter(): bool |
||
| 236 | |||
| 237 | 4 | /** |
|
| 238 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 239 | 4 | */ |
|
| 240 | public function hasStreamFilter(string $filtername): bool |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Retuns the CSV document as a Generator of string chunk. |
||
| 247 | * |
||
| 248 | * @param int $length number of bytes read |
||
| 249 | 2 | * |
|
| 250 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 251 | 2 | */ |
|
| 252 | public function chunk(int $length): Generator |
||
| 269 | 8 | ||
| 270 | 8 | /** |
|
| 271 | 8 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
|
| 272 | 8 | * |
|
| 273 | 8 | * @deprecated deprecated since version 9.1.0 |
|
| 274 | * @see AbstractCsv::getContent |
||
| 275 | * |
||
| 276 | 8 | * Retrieves the CSV content |
|
| 277 | 3 | */ |
|
| 278 | public function __toString(): string |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Retrieves the CSV content. |
||
| 285 | */ |
||
| 286 | public function getContent(): string |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Outputs all data on the CSV file. |
||
| 298 | * |
||
| 299 | * @param null|string $filename |
||
| 300 | * |
||
| 301 | 12 | * @return int Returns the number of characters read from the handle |
|
| 302 | * and passed through to the output. |
||
| 303 | 12 | */ |
|
| 304 | 12 | public function output(string $filename = null): int |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Send the CSV headers. |
||
| 319 | 6 | * |
|
| 320 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 321 | 6 | * |
|
| 322 | 6 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
|
| 323 | * |
||
| 324 | 4 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
|
| 325 | 4 | */ |
|
| 326 | 4 | protected function sendHeaders(string $filename) |
|
| 349 | 4 | ||
| 350 | 4 | /** |
|
| 351 | 2 | * Sets the field delimiter. |
|
| 352 | * |
||
| 353 | * @throws Exception If the Csv control character is not one character only. |
||
| 354 | 4 | * |
|
| 355 | * @return static |
||
| 356 | 4 | */ |
|
| 357 | 4 | public function setDelimiter(string $delimiter): self |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Reset dynamic object properties to improve performance. |
||
| 375 | */ |
||
| 376 | 12 | protected function resetProperties() |
|
| 379 | 6 | ||
| 380 | /** |
||
| 381 | * Sets the field enclosure. |
||
| 382 | 10 | * |
|
| 383 | 10 | * @throws Exception If the Csv control character is not one character only. |
|
| 384 | 10 | * |
|
| 385 | * @return static |
||
| 386 | 10 | */ |
|
| 387 | public function setEnclosure(string $enclosure): self |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Sets the field escape character. |
||
| 405 | * |
||
| 406 | * @throws Exception If the Csv control character is not one character only. |
||
| 407 | * |
||
| 408 | 2 | * @return static |
|
| 409 | */ |
||
| 410 | 2 | public function setEscape(string $escape): self |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 428 | * |
||
| 429 | * @return static |
||
| 430 | */ |
||
| 431 | public function setOutputBOM(string $str): self |
||
| 437 | |||
| 438 | /** |
||
| 439 | 2 | * append a stream filter. |
|
| 440 | 2 | * |
|
| 441 | 2 | * |
|
| 442 | * @throws Exception If the stream filter API can not be used |
||
| 443 | 2 | * |
|
| 444 | * @return static |
||
| 445 | */ |
||
| 446 | 2 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 459 | } |
||
| 460 |