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 |
||
| 30 | class Stream implements SeekableIterator |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Attached filters |
||
| 34 | * |
||
| 35 | * @var resource[] |
||
| 36 | */ |
||
| 37 | protected $filters = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * stream resource |
||
| 41 | * |
||
| 42 | * @var resource |
||
| 43 | */ |
||
| 44 | protected $stream; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Tell whether the stream support seek operations |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $seekable; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Tell whether the stream should be closed on object destruction |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $should_close_stream = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Current iterator value |
||
| 62 | * |
||
| 63 | * @var mixed |
||
| 64 | */ |
||
| 65 | protected $value; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Current iterator key |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $offset; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Flags for the Document |
||
| 76 | * |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $flags = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * the field delimiter (one character only) |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $delimiter = ','; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * the field enclosure character (one character only) |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $enclosure = '"'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * the field escape character (one character only) |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $escape = '\\'; |
||
| 101 | |||
| 102 | 34 | /** |
|
| 103 | * New instance |
||
| 104 | 34 | * |
|
| 105 | 2 | * @param resource $resource stream type resource |
|
| 106 | * |
||
| 107 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
| 108 | 32 | */ |
|
| 109 | 2 | public function __construct($resource) |
|
| 122 | |||
| 123 | /** |
||
| 124 | 28 | * @inheritdoc |
|
| 125 | 8 | */ |
|
| 126 | 28 | public function __destruct() |
|
| 140 | 2 | ||
| 141 | /** |
||
| 142 | 2 | * @inheritdoc |
|
| 143 | */ |
||
| 144 | public function __clone() |
||
| 148 | 2 | ||
| 149 | /** |
||
| 150 | 2 | * @inheritdoc |
|
| 151 | 2 | */ |
|
| 152 | 2 | public function __debugInfo() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Return a new instance from a file path |
||
| 164 | * |
||
| 165 | * @param string $path file path |
||
| 166 | * @param string $open_mode the file open mode flag |
||
| 167 | * @param resource|null $context the resource context |
||
| 168 | * |
||
| 169 | 12 | * @throws Exception if the stream resource can not be created |
|
| 170 | * |
||
| 171 | 12 | * @return static |
|
| 172 | 12 | */ |
|
| 173 | 2 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Return a new instance from a string |
||
| 193 | * |
||
| 194 | 10 | * @param string $content the CSV document as a string |
|
| 195 | * |
||
| 196 | 10 | * @return static |
|
| 197 | 10 | */ |
|
| 198 | public static function createFromString(string $content): self |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Assert that stream resource is seekable |
||
| 211 | * |
||
| 212 | * @throws Exception |
||
| 213 | */ |
||
| 214 | private function assertSeekable() { |
||
| 219 | 10 | ||
| 220 | 8 | /** |
|
| 221 | 8 | * Tell whether the stream support seek operations |
|
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | 2 | */ |
|
| 225 | public function isSeekable(): bool |
||
| 229 | |||
| 230 | /** |
||
| 231 | * append a filter |
||
| 232 | * |
||
| 233 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 234 | * |
||
| 235 | * @param string $filtername |
||
| 236 | 24 | * @param int $read_write |
|
| 237 | * @param mixed $params |
||
| 238 | 24 | * |
|
| 239 | 24 | * @throws Exception if the filter can not be appended |
|
| 240 | */ |
||
| 241 | public function appendFilter(string $filtername, int $read_write, $params = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | 30 | * Set CSV control |
|
| 254 | * |
||
| 255 | 30 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
|
| 256 | 30 | * |
|
| 257 | 30 | * @param string $delimiter |
|
| 258 | 30 | * @param string $enclosure |
|
| 259 | * @param string $escape |
||
| 260 | */ |
||
| 261 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Filter Csv control characters |
||
| 268 | * |
||
| 269 | * @param string $delimiter CSV delimiter character |
||
| 270 | * @param string $enclosure CSV enclosure character |
||
| 271 | * @param string $escape CSV escape character |
||
| 272 | 30 | * @param string $caller caller |
|
| 273 | * |
||
| 274 | 30 | * @throws Exception If the Csv control character is not one character only. |
|
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Set CSV control |
||
| 292 | * |
||
| 293 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
| 294 | * |
||
| 295 | * @return string[] |
||
| 296 | */ |
||
| 297 | public function getCsvControl() |
||
| 301 | 14 | ||
| 302 | /** |
||
| 303 | 14 | * Set CSV stream flags |
|
| 304 | * |
||
| 305 | 8 | * @see http://php.net/manual/en/splfileobject.setflags.php |
|
| 306 | * |
||
| 307 | * @param int $flags |
||
| 308 | */ |
||
| 309 | public function setFlags(int $flags) |
||
| 313 | |||
| 314 | /** |
||
| 315 | 12 | * Write a field array as a CSV line |
|
| 316 | * |
||
| 317 | 12 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
|
| 318 | * |
||
| 319 | * @param array $fields |
||
| 320 | * @param string $delimiter |
||
| 321 | * @param string $enclosure |
||
| 322 | * @param string $escape |
||
| 323 | * |
||
| 324 | * @return int|bool |
||
| 325 | */ |
||
| 326 | 12 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get line number |
||
| 335 | * |
||
| 336 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 337 | * |
||
| 338 | 22 | * @return int |
|
| 339 | */ |
||
| 340 | 22 | public function key() |
|
| 344 | 12 | ||
| 345 | /** |
||
| 346 | 22 | * Read next line |
|
| 347 | * |
||
| 348 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 349 | * |
||
| 350 | */ |
||
| 351 | public function next() |
||
| 356 | |||
| 357 | 20 | /** |
|
| 358 | 12 | * Rewind the file to the first line |
|
| 359 | * |
||
| 360 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 361 | 8 | * |
|
| 362 | */ |
||
| 363 | public function rewind() |
||
| 373 | 24 | ||
| 374 | 14 | /** |
|
| 375 | * Not at EOF |
||
| 376 | * |
||
| 377 | 24 | * @see http://php.net/manual/en/splfileobject.valid.php |
|
| 378 | * |
||
| 379 | 24 | * @return bool |
|
| 380 | */ |
||
| 381 | public function valid() |
||
| 389 | |||
| 390 | 22 | /** |
|
| 391 | 22 | * Retrieves the current line of the file. |
|
| 392 | * |
||
| 393 | 22 | * @see http://php.net/manual/en/splfileobject.current.php |
|
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | public function current() |
||
| 407 | 8 | ||
| 408 | 2 | /** |
|
| 409 | * Retrieves the current line as a CSV Record |
||
| 410 | * |
||
| 411 | 6 | * @return array|bool |
|
| 412 | 6 | */ |
|
| 413 | 2 | protected function getCurrentRecord() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Seek to specified line |
||
| 424 | * |
||
| 425 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 426 | * |
||
| 427 | * |
||
| 428 | 2 | * @param int $position |
|
| 429 | * @throws Exception if the position is negative |
||
| 430 | 2 | */ |
|
| 431 | public function seek($position) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Output all remaining data on a file pointer |
||
| 450 | * |
||
| 451 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 452 | * |
||
| 453 | * @return int |
||
| 454 | */ |
||
| 455 | public function fpassthru() |
||
| 459 | 10 | ||
| 460 | /** |
||
| 461 | * Read from file |
||
| 462 | * |
||
| 463 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 464 | * |
||
| 465 | * @param int $length The number of bytes to read |
||
| 466 | * |
||
| 467 | * @return string|false |
||
| 468 | */ |
||
| 469 | public function fread($length) |
||
| 473 | |||
| 474 | 2 | /** |
|
| 475 | * Seek to a position |
||
| 476 | * |
||
| 477 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 478 | * |
||
| 479 | * @param int $offset |
||
| 480 | * @param int $whence |
||
| 481 | * |
||
| 482 | * @return int |
||
| 483 | */ |
||
| 484 | 2 | 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 | * @param string $str |
||
| 496 | * @param int $length |
||
| 497 | * |
||
| 498 | * @return int|bool |
||
| 499 | */ |
||
| 500 | public function fwrite(string $str, int $length = 0) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Flushes the output to a file |
||
| 507 | * |
||
| 508 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 509 | * |
||
| 510 | * @return bool |
||
| 511 | */ |
||
| 512 | public function fflush() |
||
| 516 | } |
||
| 517 |