Complex classes like StreamIterator 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 StreamIterator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class StreamIterator implements SeekableIterator |
||
| 33 | { |
||
| 34 | use ValidatorTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Attached filters |
||
| 38 | * |
||
| 39 | * @var resource[] |
||
| 40 | */ |
||
| 41 | protected $filters = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * stream resource |
||
| 45 | * |
||
| 46 | * @var resource |
||
| 47 | */ |
||
| 48 | protected $stream; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Tell whether the stream should be closed on object destruction |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $should_close_stream = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Current iterator value |
||
| 59 | * |
||
| 60 | * @var mixed |
||
| 61 | */ |
||
| 62 | protected $value; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Current iterator key |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $offset; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Flags for the StreamIterator |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected $flags = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * the field delimiter (one character only) |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $delimiter = ','; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * the field enclosure character (one character only) |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $enclosure = '"'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * the field escape character (one character only) |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $escape = '\\'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * New instance |
||
| 101 | * |
||
| 102 | * @param resource $resource stream type resource |
||
| 103 | * |
||
| 104 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
| 105 | */ |
||
| 106 | 32 | public function __construct($resource) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | public function __destruct() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 2 | public function __clone() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Return a new instance from a file path |
||
| 151 | * |
||
| 152 | * @param string $path file path |
||
| 153 | * @param string $open_mode the file open mode flag |
||
| 154 | * @param resource|null $context the resource context |
||
| 155 | * |
||
| 156 | * @throws RuntimeException if the stream resource can not be created |
||
| 157 | * |
||
| 158 | * @return static |
||
| 159 | */ |
||
| 160 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Return a new instance from a string |
||
| 180 | * |
||
| 181 | * @param string $content the CSV document as a string |
||
| 182 | * |
||
| 183 | * @return static |
||
| 184 | */ |
||
| 185 | 8 | public static function createFromString(string $content): self |
|
| 195 | |||
| 196 | /** |
||
| 197 | * append a filter |
||
| 198 | * |
||
| 199 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 200 | * |
||
| 201 | * @param string $filter_name |
||
| 202 | * @param int $read_write |
||
| 203 | * @param mixed $params |
||
| 204 | * |
||
| 205 | * @throws RuntimeException if the filter can not be appended |
||
| 206 | */ |
||
| 207 | 10 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set CSV control |
||
| 220 | * |
||
| 221 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 222 | * |
||
| 223 | * @param string $delimiter |
||
| 224 | * @param string $enclosure |
||
| 225 | * @param string $escape |
||
| 226 | */ |
||
| 227 | 12 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Set StreamIterator Flags |
||
| 236 | * |
||
| 237 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 238 | * |
||
| 239 | * @param int $flags |
||
| 240 | */ |
||
| 241 | 22 | public function setFlags(int $flags) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Write a field array as a CSV line |
||
| 248 | * |
||
| 249 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 250 | * |
||
| 251 | * @param array $fields |
||
| 252 | * @param string $delimiter |
||
| 253 | * @param string $enclosure |
||
| 254 | * @param string $escape |
||
| 255 | * |
||
| 256 | * @return int|bool |
||
| 257 | */ |
||
| 258 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 259 | { |
||
| 260 | 14 | return fputcsv( |
|
| 261 | 14 | $this->stream, |
|
| 262 | 14 | $fields, |
|
| 263 | 14 | $this->filterControl($delimiter, 'delimiter', __METHOD__), |
|
| 264 | 12 | $this->filterControl($enclosure, 'enclosure', __METHOD__), |
|
| 265 | 10 | $this->filterControl($escape, 'escape', __METHOD__) |
|
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get line number |
||
| 271 | * |
||
| 272 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 273 | * |
||
| 274 | * @return int |
||
| 275 | */ |
||
| 276 | 12 | public function key() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Read next line |
||
| 283 | * |
||
| 284 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 285 | * |
||
| 286 | */ |
||
| 287 | 12 | public function next() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Rewind the file to the first line |
||
| 295 | * |
||
| 296 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 297 | * |
||
| 298 | */ |
||
| 299 | 22 | public function rewind() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Not at EOF |
||
| 311 | * |
||
| 312 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | 20 | public function valid() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Retrieves the current line of the file. |
||
| 327 | * |
||
| 328 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | 16 | public function current() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Retrieves the current line as a CSV Record |
||
| 351 | * |
||
| 352 | * @return array|bool |
||
| 353 | */ |
||
| 354 | 12 | protected function getCurrentRecord() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Retrieves the current line as a string |
||
| 365 | * |
||
| 366 | * @return string|bool |
||
| 367 | */ |
||
| 368 | 22 | protected function getCurrentLine() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Seek to specified line |
||
| 379 | * |
||
| 380 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 381 | * |
||
| 382 | * @param int $position |
||
| 383 | */ |
||
| 384 | 4 | public function seek($position) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Gets line from file |
||
| 399 | * |
||
| 400 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 401 | * |
||
| 402 | * @return string|bool |
||
| 403 | */ |
||
| 404 | 26 | public function fgets() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Output all remaining data on a file pointer |
||
| 415 | * |
||
| 416 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 417 | * |
||
| 418 | * @return int |
||
| 419 | */ |
||
| 420 | 2 | public function fpassthru() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Read from file |
||
| 427 | * |
||
| 428 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 429 | * |
||
| 430 | * @param int $length The number of bytes to read |
||
| 431 | * |
||
| 432 | * @return string|false |
||
| 433 | */ |
||
| 434 | 8 | public function fread($length) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Seek to a position |
||
| 441 | * |
||
| 442 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 443 | * |
||
| 444 | * @param int $offset |
||
| 445 | * @param int $whence |
||
| 446 | * |
||
| 447 | * @return int |
||
| 448 | */ |
||
| 449 | 4 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Write to stream |
||
| 456 | * |
||
| 457 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 458 | * |
||
| 459 | * @param string $str |
||
| 460 | * @param int $length |
||
| 461 | * |
||
| 462 | * @return int|bool |
||
| 463 | */ |
||
| 464 | 2 | public function fwrite(string $str, int $length = 0) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Flushes the output to a file |
||
| 471 | * |
||
| 472 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 473 | * |
||
| 474 | * @return bool |
||
| 475 | */ |
||
| 476 | 8 | public function fflush() |
|
| 480 | } |
||
| 481 |