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 |
||
| 50 | class Stream implements SeekableIterator |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Attached filters. |
||
| 54 | * |
||
| 55 | * @var resource[] |
||
| 56 | */ |
||
| 57 | protected $filters = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * stream resource. |
||
| 61 | * |
||
| 62 | * @var resource |
||
| 63 | */ |
||
| 64 | protected $stream; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Tell whether the stream should be closed on object destruction. |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $should_close_stream = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Current iterator value. |
||
| 75 | * |
||
| 76 | * @var mixed |
||
| 77 | */ |
||
| 78 | protected $value; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Current iterator key. |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $offset; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Flags for the Document. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $flags = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * the field delimiter (one character only). |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $delimiter = ','; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * the field enclosure character (one character only). |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected $enclosure = '"'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * the field escape character (one character only). |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | protected $escape = '\\'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Tell whether the current stream is seekable;. |
||
| 117 | * |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $is_seekable = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * New instance. |
||
| 124 | * |
||
| 125 | * @param resource $resource stream type resource |
||
| 126 | */ |
||
| 127 | 51 | public function __construct($resource) |
|
| 128 | { |
||
| 129 | 51 | if (!is_resource($resource)) { |
|
| 130 | 3 | throw new TypeError(sprintf('Argument passed must be a stream resource, %s given', gettype($resource))); |
|
| 131 | } |
||
| 132 | |||
| 133 | 48 | if ('stream' !== ($type = get_resource_type($resource))) { |
|
| 134 | 3 | throw new TypeError(sprintf('Argument passed must be a stream resource, %s resource given', $type)); |
|
| 135 | } |
||
| 136 | |||
| 137 | 45 | $this->is_seekable = stream_get_meta_data($resource)['seekable']; |
|
| 138 | 45 | $this->stream = $resource; |
|
| 139 | 45 | } |
|
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function __destruct() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 3 | public function __clone() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | 3 | public function __debugInfo() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Return a new instance from a file path. |
||
| 182 | * |
||
| 183 | * @param resource|null $context |
||
| 184 | * |
||
| 185 | * @throws Exception if the stream resource can not be created |
||
| 186 | * |
||
| 187 | * @return static |
||
| 188 | */ |
||
| 189 | 21 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Return a new instance from a string. |
||
| 209 | * |
||
| 210 | * @return static |
||
| 211 | */ |
||
| 212 | 18 | public static function createFromString(string $content = '') |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Return the URI of the underlying stream. |
||
| 225 | */ |
||
| 226 | 12 | public function getPathname(): string |
|
| 230 | |||
| 231 | /** |
||
| 232 | * append a filter. |
||
| 233 | * |
||
| 234 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 235 | * |
||
| 236 | * @param null|mixed $params |
||
| 237 | * @throws Exception if the filter can not be appended |
||
| 238 | */ |
||
| 239 | 18 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Set CSV control. |
||
| 252 | * |
||
| 253 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 254 | */ |
||
| 255 | 24 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Filter Csv control characters. |
||
| 262 | * |
||
| 263 | * @throws Exception If the Csv control character is not one character only. |
||
| 264 | */ |
||
| 265 | 45 | protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Set CSV control. |
||
| 283 | * |
||
| 284 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
| 285 | * |
||
| 286 | * @return string[] |
||
| 287 | */ |
||
| 288 | 45 | public function getCsvControl() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Set CSV stream flags. |
||
| 295 | * |
||
| 296 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 297 | */ |
||
| 298 | 33 | public function setFlags(int $flags) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Write a field array as a CSV line. |
||
| 305 | * |
||
| 306 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 307 | * |
||
| 308 | * @return int|bool |
||
| 309 | */ |
||
| 310 | 21 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get line number. |
||
| 319 | * |
||
| 320 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | 21 | public function key() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Read next line. |
||
| 331 | * |
||
| 332 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 333 | */ |
||
| 334 | 18 | public function next() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Rewind the file to the first line. |
||
| 342 | * |
||
| 343 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 344 | * |
||
| 345 | * @throws Exception if the stream resource is not seekable |
||
| 346 | */ |
||
| 347 | 36 | public function rewind() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Not at EOF. |
||
| 363 | * |
||
| 364 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | 30 | public function valid() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Retrieves the current line of the file. |
||
| 379 | * |
||
| 380 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 381 | */ |
||
| 382 | 21 | public function current() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieves the current line as a CSV Record. |
||
| 395 | * |
||
| 396 | * @return array|bool |
||
| 397 | */ |
||
| 398 | 21 | protected function getCurrentRecord() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Seek to specified line. |
||
| 409 | * |
||
| 410 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 411 | * |
||
| 412 | * @param int $position |
||
| 413 | * @throws Exception if the position is negative |
||
| 414 | */ |
||
| 415 | 12 | public function seek($position) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Output all remaining data on a file pointer. |
||
| 436 | * |
||
| 437 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 438 | * |
||
| 439 | * @return int |
||
| 440 | */ |
||
| 441 | 3 | public function fpassthru() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Read from file. |
||
| 448 | * |
||
| 449 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 450 | * |
||
| 451 | * @param int $length The number of bytes to read |
||
| 452 | * |
||
| 453 | * @return string|false |
||
| 454 | */ |
||
| 455 | 33 | public function fread($length) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Gets a line from file. |
||
| 462 | * |
||
| 463 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 464 | * |
||
| 465 | * @return string|false |
||
| 466 | */ |
||
| 467 | 6 | public function fgets() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Seek to a position. |
||
| 474 | * |
||
| 475 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 476 | * |
||
| 477 | * @throws Exception if the stream resource is not seekable |
||
| 478 | * |
||
| 479 | * @return int |
||
| 480 | */ |
||
| 481 | 18 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Write to stream. |
||
| 492 | * |
||
| 493 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 494 | * |
||
| 495 | * @return int|bool |
||
| 496 | */ |
||
| 497 | 3 | public function fwrite(string $str, int $length = null) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Flushes the output to a file. |
||
| 509 | * |
||
| 510 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | 3 | public function fflush() |
|
| 518 | } |
||
| 519 |