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 | * New instance. |
||
| 93 | * |
||
| 94 | * @param SplFileObject|Stream $document The CSV Object instance |
||
| 95 | */ |
||
| 96 | 36 | protected function __construct($document) |
|
| 97 | { |
||
| 98 | 36 | $this->document = $document; |
|
| 99 | 36 | list($this->delimiter, $this->enclosure, $this->escape) = $this->document->getCsvControl(); |
|
| 100 | 36 | $this->resetProperties(); |
|
| 101 | 36 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Reset dynamic object properties to improve performance. |
||
| 105 | */ |
||
| 106 | 33 | protected function resetProperties() |
|
| 107 | { |
||
| 108 | 33 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | 36 | public function __destruct() |
|
| 114 | { |
||
| 115 | 36 | unset($this->document); |
|
| 116 | 36 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | 3 | public function __clone() |
|
| 122 | { |
||
| 123 | 3 | throw new Exception(sprintf('An object of class %s cannot be cloned', static::class)); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return a new instance from a SplFileObject. |
||
| 128 | * |
||
| 129 | * @return static |
||
| 130 | */ |
||
| 131 | 36 | public static function createFromFileObject(SplFileObject $file) |
|
| 132 | { |
||
| 133 | 36 | return new static($file); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return a new instance from a PHP resource stream. |
||
| 138 | * |
||
| 139 | * @param resource $stream |
||
| 140 | * |
||
| 141 | * @return static |
||
| 142 | */ |
||
| 143 | 3 | public static function createFromStream($stream) |
|
| 144 | { |
||
| 145 | 3 | return new static(new Stream($stream)); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return a new instance from a string. |
||
| 150 | * |
||
| 151 | * @return static |
||
| 152 | */ |
||
| 153 | 6 | public static function createFromString(string $content = '') |
|
| 154 | { |
||
| 155 | 6 | return new static(Stream::createFromString($content)); |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return a new instance from a file path. |
||
| 160 | * |
||
| 161 | * @param resource|null $context the resource context |
||
| 162 | * |
||
| 163 | * @return static |
||
| 164 | */ |
||
| 165 | 6 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null) |
|
| 166 | { |
||
| 167 | 6 | return new static(Stream::createFromPath($path, $open_mode, $context)); |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the current field delimiter. |
||
| 172 | */ |
||
| 173 | 15 | public function getDelimiter(): string |
|
| 174 | { |
||
| 175 | 15 | return $this->delimiter; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the current field enclosure. |
||
| 180 | */ |
||
| 181 | 3 | public function getEnclosure(): string |
|
| 182 | { |
||
| 183 | 3 | return $this->enclosure; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the pathname of the underlying document. |
||
| 188 | */ |
||
| 189 | 12 | public function getPathname(): string |
|
| 190 | { |
||
| 191 | 12 | return $this->document->getPathname(); |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the current field escape character. |
||
| 196 | */ |
||
| 197 | 3 | public function getEscape(): string |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the BOM sequence in use on Output methods. |
||
| 204 | */ |
||
| 205 | 3 | public function getOutputBOM(): string |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the BOM sequence of the given CSV. |
||
| 212 | */ |
||
| 213 | 54 | public function getInputBOM(): string |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the stream filter mode. |
||
| 228 | */ |
||
| 229 | 3 | public function getStreamFilterMode(): int |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Tells whether the stream filter capabilities can be used. |
||
| 236 | */ |
||
| 237 | 6 | public function supportsStreamFilter(): bool |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Tell whether the specify stream filter is attach to the current stream. |
||
| 244 | */ |
||
| 245 | 3 | public function hasStreamFilter(string $filtername): bool |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Retuns the CSV document as a Generator of string chunk. |
||
| 252 | * |
||
| 253 | * @param int $length number of bytes read |
||
| 254 | * |
||
| 255 | * @throws Exception if the number of bytes is lesser than 1 |
||
| 256 | */ |
||
| 257 | 15 | public function chunk(int $length): Generator |
|
| 274 | |||
| 275 | /** |
||
| 276 | * DEPRECATION WARNING! This method will be removed in the next major point release. |
||
| 277 | * |
||
| 278 | * @deprecated deprecated since version 9.1.0 |
||
| 279 | * @see AbstractCsv::getContent |
||
| 280 | * |
||
| 281 | * Retrieves the CSV content |
||
| 282 | */ |
||
| 283 | 3 | public function __toString(): string |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Retrieves the CSV content. |
||
| 290 | */ |
||
| 291 | 18 | public function getContent(): string |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Outputs all data on the CSV file. |
||
| 303 | * |
||
| 304 | * @return int Returns the number of characters read from the handle |
||
| 305 | * and passed through to the output. |
||
| 306 | */ |
||
| 307 | 9 | public function output(string $filename = null): int |
|
| 308 | { |
||
| 309 | 9 | if (null !== $filename) { |
|
| 310 | 9 | $this->sendHeaders($filename); |
|
| 311 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Send the CSV headers. |
||
| 322 | * |
||
| 323 | * Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition |
||
| 324 | * |
||
| 325 | * @throws Exception if the submitted header is invalid according to RFC 6266 |
||
| 326 | * |
||
| 327 | * @see https://tools.ietf.org/html/rfc6266#section-4.3 |
||
| 328 | */ |
||
| 329 | 9 | protected function sendHeaders(string $filename) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Sets the field delimiter. |
||
| 355 | * |
||
| 356 | * @throws Exception If the Csv control character is not one character only. |
||
| 357 | * |
||
| 358 | * @return static |
||
| 359 | */ |
||
| 360 | 18 | public function setDelimiter(string $delimiter): self |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Sets the field enclosure. |
||
| 378 | * |
||
| 379 | * @throws Exception If the Csv control character is not one character only. |
||
| 380 | * |
||
| 381 | * @return static |
||
| 382 | */ |
||
| 383 | 3 | public function setEnclosure(string $enclosure): self |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Sets the field escape character. |
||
| 401 | * |
||
| 402 | * @throws Exception If the Csv control character is not one character only. |
||
| 403 | * |
||
| 404 | * @return static |
||
| 405 | */ |
||
| 406 | 3 | public function setEscape(string $escape): self |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Sets the BOM sequence to prepend the CSV on output. |
||
| 424 | * |
||
| 425 | * @return static |
||
| 426 | */ |
||
| 427 | 9 | public function setOutputBOM(string $str): self |
|
| 433 | |||
| 434 | /** |
||
| 435 | * append a stream filter. |
||
| 436 | * |
||
| 437 | * @param null|mixed $params |
||
| 438 | * |
||
| 439 | * @throws Exception If the stream filter API can not be used |
||
| 440 | * |
||
| 441 | * @return static |
||
| 442 | */ |
||
| 443 | 15 | public function addStreamFilter(string $filtername, $params = null): self |
|
| 456 | } |
||
| 457 |