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 |
||
| 31 | class Stream implements SeekableIterator |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Attached filters |
||
| 35 | * |
||
| 36 | * @var resource[] |
||
| 37 | */ |
||
| 38 | protected $filters = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * stream resource |
||
| 42 | * |
||
| 43 | * @var resource |
||
| 44 | */ |
||
| 45 | protected $stream; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Tell whether the stream should be closed on object destruction |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $should_close_stream = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Current iterator value |
||
| 56 | * |
||
| 57 | * @var mixed |
||
| 58 | */ |
||
| 59 | protected $value; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Current iterator key |
||
| 63 | * |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $offset; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Flags for the Document |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $flags = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * the field delimiter (one character only) |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $delimiter = ','; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * the field enclosure character (one character only) |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $enclosure = '"'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * the field escape character (one character only) |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $escape = '\\'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * New instance |
||
| 98 | * |
||
| 99 | * @param resource $resource stream type resource |
||
| 100 | * |
||
| 101 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
| 102 | */ |
||
| 103 | 34 | public function __construct($resource) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | public function __destruct() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritdoc |
||
| 140 | */ |
||
| 141 | 2 | public function __clone() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | 2 | public function __debugInfo() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Return a new instance from a file path |
||
| 161 | * |
||
| 162 | * @param string $path file path |
||
| 163 | * @param string $open_mode the file open mode flag |
||
| 164 | * @param resource|null $context the resource context |
||
| 165 | * |
||
| 166 | * @throws Exception if the stream resource can not be created |
||
| 167 | * |
||
| 168 | * @return static |
||
| 169 | */ |
||
| 170 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Return a new instance from a string |
||
| 190 | * |
||
| 191 | * @param string $content the CSV document as a string |
||
| 192 | * |
||
| 193 | * @return static |
||
| 194 | */ |
||
| 195 | 10 | public static function createFromString(string $content): self |
|
| 205 | |||
| 206 | /** |
||
| 207 | * append a filter |
||
| 208 | * |
||
| 209 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 210 | * |
||
| 211 | * @param string $filtername |
||
| 212 | * @param int $read_write |
||
| 213 | * @param mixed $params |
||
| 214 | * |
||
| 215 | * @throws Exception if the filter can not be appended |
||
| 216 | */ |
||
| 217 | 10 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Set CSV control |
||
| 230 | * |
||
| 231 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 232 | * |
||
| 233 | * @param string $delimiter |
||
| 234 | * @param string $enclosure |
||
| 235 | * @param string $escape |
||
| 236 | */ |
||
| 237 | 24 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Filter Csv control character |
||
| 246 | * |
||
| 247 | * @param string $char Csv control character |
||
| 248 | * @param string $type Csv control character type |
||
| 249 | * @param string $caller public API method calling the method |
||
| 250 | * |
||
| 251 | * @throws Exception If the Csv control character is not one character only. |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 24 | protected function filterControl(string $char, string $type, string $caller): string |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Set CSV control |
||
| 266 | * |
||
| 267 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
| 268 | * |
||
| 269 | * @return string[] |
||
| 270 | */ |
||
| 271 | 30 | public function getCsvControl() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Set CSV stream flags |
||
| 278 | * |
||
| 279 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 280 | * |
||
| 281 | * @param int $flags |
||
| 282 | */ |
||
| 283 | 22 | public function setFlags(int $flags) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Write a field array as a CSV line |
||
| 290 | * |
||
| 291 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 292 | * |
||
| 293 | * @param array $fields |
||
| 294 | * @param string $delimiter |
||
| 295 | * @param string $enclosure |
||
| 296 | * @param string $escape |
||
| 297 | * |
||
| 298 | * @return int|bool |
||
| 299 | */ |
||
| 300 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get line number |
||
| 313 | * |
||
| 314 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 315 | * |
||
| 316 | * @return int |
||
| 317 | */ |
||
| 318 | 12 | public function key() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Read next line |
||
| 325 | * |
||
| 326 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 327 | * |
||
| 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 | */ |
||
| 341 | 22 | public function rewind() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Not at EOF |
||
| 353 | * |
||
| 354 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 20 | public function valid() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Retrieves the current line of the file. |
||
| 369 | * |
||
| 370 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 24 | public function current() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Retrieves the current line as a CSV Record |
||
| 387 | * |
||
| 388 | * @return array|bool |
||
| 389 | */ |
||
| 390 | 22 | protected function getCurrentRecord() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Seek to specified line |
||
| 401 | * |
||
| 402 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 403 | * |
||
| 404 | * |
||
| 405 | * @param int $position |
||
| 406 | * @throws Exception if the position is negative |
||
| 407 | */ |
||
| 408 | 8 | public function seek($position) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Output all remaining data on a file pointer |
||
| 426 | * |
||
| 427 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 428 | * |
||
| 429 | * @return int |
||
| 430 | */ |
||
| 431 | 2 | public function fpassthru() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Read from file |
||
| 438 | * |
||
| 439 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 440 | * |
||
| 441 | * @param int $length The number of bytes to read |
||
| 442 | * |
||
| 443 | * @return string|false |
||
| 444 | */ |
||
| 445 | 8 | public function fread($length) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Seek to a position |
||
| 452 | * |
||
| 453 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 454 | * |
||
| 455 | * @param int $offset |
||
| 456 | * @param int $whence |
||
| 457 | * |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | 10 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Write to stream |
||
| 467 | * |
||
| 468 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 469 | * |
||
| 470 | * @param string $str |
||
| 471 | * @param int $length |
||
| 472 | * |
||
| 473 | * @return int|bool |
||
| 474 | */ |
||
| 475 | 2 | public function fwrite(string $str, int $length = 0) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Flushes the output to a file |
||
| 482 | * |
||
| 483 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | 2 | public function fflush() |
|
| 491 | } |
||
| 492 |