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 |
||
| 41 | abstract class AbstractCsv implements ByteSequence |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * The stream filter mode (read or write). |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $stream_filter_mode; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * collection of stream filters. |
||
| 53 | * |
||
| 54 | * @var bool[] |
||
| 55 | */ |
||
| 56 | protected $stream_filters = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The CSV document BOM sequence. |
||
| 60 | * |
||
| 61 | * @var string|null |
||
| 62 | */ |
||
| 63 | protected $input_bom = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The Output file BOM character. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $output_bom = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * the field delimiter (one character only). |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $delimiter = ','; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * the field enclosure character (one character only). |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $enclosure = '"'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * the field escape character (one character only). |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $escape = '\\'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The CSV document. |
||
| 95 | * |
||
| 96 | * @var SplFileObject|Stream |
||
| 97 | */ |
||
| 98 | protected $document; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * New instance. |
||
| 102 | * |
||
| 103 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 104 | */ |
||
| 105 | 36 | protected function __construct($document) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Reset dynamic object properties to improve performance. |
||
| 114 | */ |
||
| 115 | 33 | protected function resetProperties() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 36 | public function __destruct() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | 3 | public function __clone() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Return a new instance from a SplFileObject. |
||
| 137 | * |
||
| 138 | * @return static |
||
| 139 | */ |
||
| 140 | 36 | public static function createFromFileObject(SplFileObject $file) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Return a new instance from a PHP resource stream. |
||
| 147 | * |
||
| 148 | * @param resource $stream |
||
| 149 | * |
||
| 150 | * @return static |
||
| 151 | */ |
||
| 152 | 3 | public static function createFromStream($stream) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Return a new instance from a string. |
||
| 159 | * |
||
| 160 | * @return static |
||
| 161 | */ |
||
| 162 | 6 | public static function createFromString(string $content = '') |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Return a new instance from a file path. |
||
| 169 | * |
||
| 170 | * @param resource|null $context the resource context |
||
| 171 | * |
||
| 172 | * @return static |
||
| 173 | */ |
||
| 174 | 6 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the current field delimiter. |
||
| 181 | */ |
||
| 182 | 15 | public function getDelimiter(): string |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the current field enclosure. |
||
| 189 | */ |
||
| 190 | 3 | public function getEnclosure(): string |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the current field escape character. |
||
| 197 | */ |
||
| 198 | 3 | public function getEscape(): string |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Returns the BOM sequence in use on Output methods. |
||
| 205 | */ |
||
| 206 | 3 | public function getOutputBOM(): string |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the BOM sequence of the given CSV. |
||
| 213 | */ |
||
| 214 | 51 | public function getInputBOM(): string |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the stream filter mode. |
||
| 229 | */ |
||
| 230 | 3 | public function getStreamFilterMode(): int |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Tells whether the stream filter capabilities can be used. |
||
| 237 | */ |
||
| 238 | 6 | public function supportsStreamFilter(): bool |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 245 | */ |
||
| 246 | 3 | public function hasStreamFilter(string $filtername): bool |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Retuns the CSV document as a Generator of string chunk. |
||
| 253 | * |
||
| 254 | * @param int $length number of bytes read |
||
| 255 | * |
||
| 256 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 257 | */ |
||
| 258 | 15 | public function chunk(int $length): Generator |
|
| 275 | |||
| 276 | /** |
||
| 277 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
| 278 | * |
||
| 279 | * @deprecated deprecated since version 9.1.0 |
||
| 280 | * @see AbstractCsv::getContent |
||
| 281 | * |
||
| 282 | * Retrieves the CSV content |
||
| 283 | */ |
||
| 284 | 3 | public function __toString(): string |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Retrieves the CSV content. |
||
| 291 | */ |
||
| 292 | 18 | public function getContent(): string |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Outputs all data on the CSV file. |
||
| 304 | * |
||
| 305 | * |
||
| 306 | * @return int Returns the number of characters read from the handle |
||
| 307 | * and passed through to the output. |
||
| 308 | */ |
||
| 309 | 9 | public function output(string $filename = null): int |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Send the CSV headers. |
||
| 324 | * |
||
| 325 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 326 | * |
||
| 327 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
| 328 | * |
||
| 329 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
| 330 | */ |
||
| 331 | 9 | protected function sendHeaders(string $filename) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Sets the field delimiter. |
||
| 357 | * |
||
| 358 | * @throws Exception If the Csv control character is not one character only. |
||
| 359 | * |
||
| 360 | * @return static |
||
| 361 | */ |
||
| 362 | 18 | public function setDelimiter(string $delimiter): self |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Sets the field enclosure. |
||
| 380 | * |
||
| 381 | * @throws Exception If the Csv control character is not one character only. |
||
| 382 | * |
||
| 383 | * @return static |
||
| 384 | */ |
||
| 385 | 3 | public function setEnclosure(string $enclosure): self |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Sets the field escape character. |
||
| 403 | * |
||
| 404 | * @throws Exception If the Csv control character is not one character only. |
||
| 405 | * |
||
| 406 | * @return static |
||
| 407 | */ |
||
| 408 | 3 | public function setEscape(string $escape): self |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 426 | * |
||
| 427 | * @return static |
||
| 428 | */ |
||
| 429 | 9 | public function setOutputBOM(string $str): self |
|
| 435 | |||
| 436 | /** |
||
| 437 | * append a stream filter. |
||
| 438 | * |
||
| 439 | * @param null|mixed $params |
||
| 440 | * |
||
| 441 | * @throws Exception If the stream filter API can not be used |
||
| 442 | * |
||
| 443 | * @return static |
||
| 444 | */ |
||
| 445 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 458 | } |
||
| 459 |