Complex classes like StreamIterator 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 StreamIterator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class StreamIterator implements SeekableIterator |
||
| 32 | { |
||
| 33 | use ValidatorTrait; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Attached filters |
||
| 37 | * |
||
| 38 | * @var resource[] |
||
| 39 | */ |
||
| 40 | protected $filters; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Stream pointer |
||
| 44 | * |
||
| 45 | * @var resource |
||
| 46 | */ |
||
| 47 | protected $stream; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Current iterator value |
||
| 51 | * |
||
| 52 | * @var mixed |
||
| 53 | */ |
||
| 54 | protected $value; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Current iterator key |
||
| 58 | * |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $offset; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Flags for the StreamIterator |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $flags = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * the field delimiter (one character only) |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $delimiter = ','; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * the field enclosure character (one character only) |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $enclosure = '"'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * the field escape character (one character only) |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $escape = '\\'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * New instance |
||
| 93 | * |
||
| 94 | * @param resource $stream stream type resource |
||
| 95 | */ |
||
| 96 | 78 | public function __construct($stream) |
|
| 97 | { |
||
| 98 | 78 | if (!is_resource($stream)) { |
|
| 99 | 4 | throw new InvalidArgumentException(sprintf('Argument passed must be a resource, %s given', is_object($stream) ? get_class($stream) : gettype($stream))); |
|
| 100 | } |
||
| 101 | |||
| 102 | 74 | if ('stream' !== ($type = get_resource_type($stream))) { |
|
| 103 | 2 | throw new InvalidArgumentException(sprintf('Argument passed must be a stream resource, %s resource given', $type)); |
|
| 104 | } |
||
| 105 | |||
| 106 | 72 | if (!stream_get_meta_data($stream)['seekable']) { |
|
| 107 | 2 | throw new InvalidArgumentException(sprintf('Argument passed must be a seekable stream resource')); |
|
| 108 | } |
||
| 109 | |||
| 110 | 70 | $this->stream = $stream; |
|
| 111 | 70 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * close the file pointer |
||
| 115 | */ |
||
| 116 | 70 | public function __destruct() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Set CSV control |
||
| 123 | * |
||
| 124 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 125 | * |
||
| 126 | * @param string $delimiter |
||
| 127 | * @param string $enclosure |
||
| 128 | * @param string $escape |
||
| 129 | */ |
||
| 130 | 30 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Set StreamIterator Flags |
||
| 139 | * |
||
| 140 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 141 | * |
||
| 142 | * @param int $flags |
||
| 143 | */ |
||
| 144 | 56 | public function setFlags(int $flags) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Write a field array as a CSV line |
||
| 151 | * |
||
| 152 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 153 | * |
||
| 154 | * @param array $fields |
||
| 155 | * @param string $delimiter |
||
| 156 | * @param string $enclosure |
||
| 157 | * @param string $escape |
||
| 158 | * |
||
| 159 | * @return int|bool |
||
| 160 | */ |
||
| 161 | 12 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get line number |
||
| 174 | * |
||
| 175 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 176 | * |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | 32 | public function key() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Read next line |
||
| 186 | * |
||
| 187 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 188 | * |
||
| 189 | */ |
||
| 190 | 30 | public function next() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Rewind the file to the first line |
||
| 198 | * |
||
| 199 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 200 | * |
||
| 201 | */ |
||
| 202 | 56 | public function rewind() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Not at EOF |
||
| 214 | * |
||
| 215 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 52 | public function valid() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Retrieves the current line of the file. |
||
| 230 | * |
||
| 231 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 232 | * |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | 34 | public function current() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Retrieves the current line as a CSV Record |
||
| 254 | * |
||
| 255 | * @return array|bool |
||
| 256 | */ |
||
| 257 | 32 | protected function getCurrentRecord() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Retrieves the current line as a string |
||
| 268 | * |
||
| 269 | * @return string|bool |
||
| 270 | */ |
||
| 271 | 52 | protected function getCurrentLine() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Seek to specified line |
||
| 282 | * |
||
| 283 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 284 | * |
||
| 285 | * @param int $position |
||
| 286 | */ |
||
| 287 | 20 | public function seek($position) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Gets line from file |
||
| 302 | * |
||
| 303 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 304 | * |
||
| 305 | * @return string|bool |
||
| 306 | */ |
||
| 307 | 52 | public function fgets() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Output all remaining data on a file pointer |
||
| 318 | * |
||
| 319 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | 2 | public function fpassthru() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Read from file |
||
| 330 | * |
||
| 331 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 332 | * |
||
| 333 | * @param int $length The number of bytes to read |
||
| 334 | * |
||
| 335 | * @return string|false |
||
| 336 | */ |
||
| 337 | 20 | public function fread(int $length) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Seek to a position |
||
| 344 | * |
||
| 345 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 346 | * |
||
| 347 | * @param int $offset |
||
| 348 | * @param int $whence |
||
| 349 | * |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | 8 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Write to stream |
||
| 359 | * |
||
| 360 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 361 | * |
||
| 362 | * @param string $str |
||
| 363 | * @param int $length |
||
| 364 | * |
||
| 365 | * @return int|bool |
||
| 366 | */ |
||
| 367 | 2 | public function fwrite(string $str, int $length = 0) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * append a filter |
||
| 374 | * |
||
| 375 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 376 | * |
||
| 377 | * @param string $filter_name |
||
| 378 | * @param int $read_write |
||
| 379 | * @param mixed $params |
||
| 380 | * |
||
| 381 | * @throws InvalidArgumentException if the filter can not be appended |
||
| 382 | * |
||
| 383 | * @return resource |
||
| 384 | */ |
||
| 385 | 14 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Removes a registered filter |
||
| 397 | * |
||
| 398 | * @see http://php.net/manual/en/function.stream-filter-remove.php |
||
| 399 | * |
||
| 400 | * @param resource $resource |
||
| 401 | */ |
||
| 402 | 14 | public function removeFilter($resource) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Flushes the output to a file |
||
| 409 | * |
||
| 410 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | 20 | public function fflush() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @inheritdoc |
||
| 421 | */ |
||
| 422 | 2 | public function __clone() |
|
| 426 | } |
||
| 427 |