Complex classes like Stream 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 Stream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class Stream implements SeekableIterator |
||
| 57 | { |
||
| 58 | /** |
||
| 59 | * Attached filters. |
||
| 60 | * |
||
| 61 | * @var resource[] |
||
| 62 | */ |
||
| 63 | protected $filters = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * stream resource. |
||
| 67 | * |
||
| 68 | * @var resource |
||
| 69 | */ |
||
| 70 | protected $stream; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Tell whether the stream should be closed on object destruction. |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $should_close_stream = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Current iterator value. |
||
| 81 | * |
||
| 82 | * @var mixed |
||
| 83 | */ |
||
| 84 | protected $value; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Current iterator key. |
||
| 88 | * |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | protected $offset; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Flags for the Document. |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected $flags = 0; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * the field delimiter (one character only). |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $delimiter = ','; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * the field enclosure character (one character only). |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $enclosure = '"'; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * the field escape character (one character only). |
||
| 116 | * |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | protected $escape = '\\'; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Tell whether the current stream is seekable;. |
||
| 123 | * |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $is_seekable = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * New instance. |
||
| 130 | * |
||
| 131 | * @param resource $resource stream type resource |
||
| 132 | */ |
||
| 133 | 48 | public function __construct($resource) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function __destruct() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 3 | public function __clone() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 3 | public function __debugInfo() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Return a new instance from a file path. |
||
| 188 | * |
||
| 189 | * @param resource|null $context |
||
| 190 | * |
||
| 191 | * @throws Exception if the stream resource can not be created |
||
| 192 | * |
||
| 193 | * @return static |
||
| 194 | */ |
||
| 195 | 21 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Return a new instance from a string. |
||
| 215 | * |
||
| 216 | * |
||
| 217 | * @return static |
||
| 218 | */ |
||
| 219 | 15 | public static function createFromString(string $content) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * append a filter. |
||
| 232 | * |
||
| 233 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 234 | * |
||
| 235 | * @param null|mixed $params |
||
| 236 | * @throws Exception if the filter can not be appended |
||
| 237 | */ |
||
| 238 | 18 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Set CSV control. |
||
| 251 | * |
||
| 252 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 253 | */ |
||
| 254 | 21 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Filter Csv control characters. |
||
| 261 | * |
||
| 262 | * |
||
| 263 | * @throws Exception If the Csv control character is not one character only. |
||
| 264 | */ |
||
| 265 | 42 | protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Set CSV control. |
||
| 279 | * |
||
| 280 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
| 281 | * |
||
| 282 | * @return string[] |
||
| 283 | */ |
||
| 284 | 45 | public function getCsvControl() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set CSV stream flags. |
||
| 291 | * |
||
| 292 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 293 | */ |
||
| 294 | 33 | public function setFlags(int $flags) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Write a field array as a CSV line. |
||
| 301 | * |
||
| 302 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 303 | * |
||
| 304 | * @return int|bool |
||
| 305 | */ |
||
| 306 | 21 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Get line number. |
||
| 315 | * |
||
| 316 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 317 | * |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | 18 | public function key() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Read next line. |
||
| 327 | * |
||
| 328 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 329 | */ |
||
| 330 | 18 | public function next() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Rewind the file to the first line. |
||
| 338 | * |
||
| 339 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 340 | * |
||
| 341 | * @throws Exception if the stream resource is not seekable |
||
| 342 | */ |
||
| 343 | 36 | public function rewind() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Not at EOF. |
||
| 359 | * |
||
| 360 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | 30 | public function valid() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Retrieves the current line of the file. |
||
| 375 | * |
||
| 376 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 377 | */ |
||
| 378 | 21 | public function current() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Retrieves the current line as a CSV Record. |
||
| 391 | * |
||
| 392 | * @return array|bool |
||
| 393 | */ |
||
| 394 | 21 | protected function getCurrentRecord() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Seek to specified line. |
||
| 405 | * |
||
| 406 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 407 | * |
||
| 408 | * @param int $position |
||
| 409 | * @throws Exception if the position is negative |
||
| 410 | */ |
||
| 411 | 9 | public function seek($position) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Output all remaining data on a file pointer. |
||
| 429 | * |
||
| 430 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 431 | * |
||
| 432 | * @return int |
||
| 433 | */ |
||
| 434 | 3 | public function fpassthru() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Read from file. |
||
| 441 | * |
||
| 442 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 443 | * |
||
| 444 | * @param int $length The number of bytes to read |
||
| 445 | * |
||
| 446 | * @return string|false |
||
| 447 | */ |
||
| 448 | 33 | public function fread($length) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Gets a line from file. |
||
| 455 | * |
||
| 456 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 457 | * |
||
| 458 | * @return string|false |
||
| 459 | */ |
||
| 460 | 6 | public function fgets() |
|
| 461 | { |
||
| 462 | 6 | return fgets($this->stream); |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Seek to a position. |
||
| 467 | * |
||
| 468 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 469 | * |
||
| 470 | * |
||
| 471 | * @throws Exception if the stream resource is not seekable |
||
| 472 | * |
||
| 473 | * @return int |
||
| 474 | */ |
||
| 475 | 18 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Write to stream. |
||
| 486 | * |
||
| 487 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 488 | * |
||
| 489 | * @return int|bool |
||
| 490 | */ |
||
| 491 | 3 | public function fwrite(string $str, int $length = null) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Flushes the output to a file. |
||
| 503 | * |
||
| 504 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | 3 | public function fflush() |
|
| 512 | } |
||
| 513 |