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 | */ |
||
| 92 | protected $escape = '\\'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The CSV document. |
||
| 96 | * |
||
| 97 | * @var SplFileObject|Stream |
||
| 98 | */ |
||
| 99 | protected $document; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * New instance. |
||
| 103 | * |
||
| 104 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 105 | */ |
||
| 106 | 33 | protected function __construct($document) |
|
| 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 | 3 | 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 current field escape character. |
||
| 191 | */ |
||
| 192 | 3 | public function getEscape(): string |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the BOM sequence in use on Output methods. |
||
| 199 | */ |
||
| 200 | 3 | public function getOutputBOM(): string |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Returns the BOM sequence of the given CSV. |
||
| 207 | */ |
||
| 208 | 24 | public function getInputBOM(): string |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the stream filter mode. |
||
| 224 | */ |
||
| 225 | 3 | public function getStreamFilterMode(): int |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Tells whether the stream filter capabilities can be used. |
||
| 232 | */ |
||
| 233 | 6 | public function supportsStreamFilter(): bool |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 240 | */ |
||
| 241 | 3 | public function hasStreamFilter(string $filtername): bool |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Retuns the CSV document as a Generator of string chunk. |
||
| 248 | * |
||
| 249 | * @param int $length number of bytes read |
||
| 250 | * |
||
| 251 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 252 | */ |
||
| 253 | 15 | public function chunk(int $length): Generator |
|
| 270 | |||
| 271 | /** |
||
| 272 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
| 273 | * |
||
| 274 | * @deprecated deprecated since version 9.1.0 |
||
| 275 | * @see AbstractCsv::getContent |
||
| 276 | * |
||
| 277 | * Retrieves the CSV content |
||
| 278 | */ |
||
| 279 | 3 | public function __toString(): string |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Retrieves the CSV content. |
||
| 286 | */ |
||
| 287 | 18 | public function getContent(): string |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Outputs all data on the CSV file. |
||
| 299 | * |
||
| 300 | * @param null|string $filename |
||
| 301 | * |
||
| 302 | * @return int Returns the number of characters read from the handle |
||
| 303 | * and passed through to the output. |
||
| 304 | */ |
||
| 305 | 9 | public function output(string $filename = null): int |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Send the CSV headers. |
||
| 320 | * |
||
| 321 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 322 | * |
||
| 323 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
| 324 | * |
||
| 325 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
| 326 | */ |
||
| 327 | 9 | protected function sendHeaders(string $filename) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Sets the field delimiter. |
||
| 353 | * |
||
| 354 | * @throws Exception If the Csv control character is not one character only. |
||
| 355 | * |
||
| 356 | * @return static |
||
| 357 | */ |
||
| 358 | 18 | public function setDelimiter(string $delimiter): self |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Reset dynamic object properties to improve performance. |
||
| 376 | */ |
||
| 377 | protected function resetProperties() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Sets the field enclosure. |
||
| 383 | * |
||
| 384 | * @throws Exception If the Csv control character is not one character only. |
||
| 385 | * |
||
| 386 | * @return static |
||
| 387 | */ |
||
| 388 | 3 | public function setEnclosure(string $enclosure): self |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Sets the field escape character. |
||
| 406 | * |
||
| 407 | * @throws Exception If the Csv control character is not one character only. |
||
| 408 | * |
||
| 409 | * @return static |
||
| 410 | */ |
||
| 411 | 3 | public function setEscape(string $escape): self |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 429 | * |
||
| 430 | * @return static |
||
| 431 | */ |
||
| 432 | 9 | public function setOutputBOM(string $str): self |
|
| 438 | |||
| 439 | /** |
||
| 440 | * append a stream filter. |
||
| 441 | * |
||
| 442 | * @param null|mixed $params |
||
| 443 | * |
||
| 444 | * @throws Exception If the stream filter API can not be used |
||
| 445 | * |
||
| 446 | * @return static |
||
| 447 | */ |
||
| 448 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 461 | } |
||
| 462 |