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