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 |
||
| 32 | class StreamIterator implements SeekableIterator |
||
| 33 | { |
||
| 34 | use ValidatorTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Attached filters |
||
| 38 | * |
||
| 39 | * @var resource[] |
||
| 40 | */ |
||
| 41 | protected $filters; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Stream pointer |
||
| 45 | * |
||
| 46 | * @var resource |
||
| 47 | */ |
||
| 48 | protected $stream; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Tell whether the stream is only accessible |
||
| 52 | * by the current object |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $is_inner_stream = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Current iterator value |
||
| 60 | * |
||
| 61 | * @var mixed |
||
| 62 | */ |
||
| 63 | protected $value; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Current iterator key |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $offset; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Flags for the StreamIterator |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $flags = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * the field delimiter (one character only) |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $delimiter = ','; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * the field enclosure character (one character only) |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $enclosure = '"'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * the field escape character (one character only) |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $escape = '\\'; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * New instance |
||
| 102 | * |
||
| 103 | * |
||
| 104 | * @param resource $stream stream type resource |
||
| 105 | * @throws InvalidArgumentException if the argument passed is not a seeakable stream resource |
||
| 106 | */ |
||
| 107 | 78 | public function __construct($stream) |
|
| 108 | { |
||
| 109 | 78 | if (!is_resource($stream)) { |
|
| 110 | 4 | throw new InvalidArgumentException(sprintf('Argument passed must be a resource, %s given', is_object($stream) ? get_class($stream) : gettype($stream))); |
|
| 111 | } |
||
| 112 | |||
| 113 | 74 | if ('stream' !== ($type = get_resource_type($stream))) { |
|
| 114 | 2 | throw new InvalidArgumentException(sprintf('Argument passed must be a stream resource, %s resource given', $type)); |
|
| 115 | } |
||
| 116 | |||
| 117 | 72 | if (!stream_get_meta_data($stream)['seekable']) { |
|
| 118 | 2 | throw new InvalidArgumentException(sprintf('Argument passed must be a seekable stream resource')); |
|
| 119 | } |
||
| 120 | |||
| 121 | 70 | $this->stream = $stream; |
|
| 122 | 70 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * close the file pointer |
||
| 126 | */ |
||
| 127 | 70 | public function __destruct() |
|
| 128 | { |
||
| 129 | 70 | if ($this->is_inner_stream) { |
|
| 130 | 48 | fclose($this->stream); |
|
| 131 | } |
||
| 132 | |||
| 133 | 70 | $this->stream = null; |
|
| 134 | 70 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Return a new instance from a file path |
||
| 138 | * |
||
| 139 | * @param string $path file path |
||
| 140 | * @param string $open_mode the file open mode flag |
||
| 141 | * |
||
| 142 | * @throws RuntimeException if the stream resource can not be created |
||
| 143 | * |
||
| 144 | * @return static |
||
| 145 | */ |
||
| 146 | 26 | public static function createFromPath(string $path, string $open_mode = 'r'): self |
|
| 147 | { |
||
| 148 | 26 | if (!$stream = @fopen($path, $open_mode)) { |
|
| 149 | 2 | throw new RuntimeException(error_get_last()['message']); |
|
| 150 | } |
||
| 151 | |||
| 152 | 24 | $instance = new static($stream); |
|
| 153 | 24 | $instance->is_inner_stream = true; |
|
| 154 | |||
| 155 | 24 | return $instance; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return a new instance from a string |
||
| 160 | * |
||
| 161 | * @param string $content the CSV document as a string |
||
| 162 | * |
||
| 163 | * @return static |
||
| 164 | */ |
||
| 165 | 24 | public static function createFromString(string $content): self |
|
| 166 | { |
||
| 167 | 24 | $stream = fopen('php://temp', 'r+'); |
|
| 168 | 24 | fwrite($stream, $content); |
|
| 169 | |||
| 170 | 24 | $instance = new static($stream); |
|
| 171 | 24 | $instance->is_inner_stream = true; |
|
| 172 | |||
| 173 | 24 | return $instance; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set CSV control |
||
| 178 | * |
||
| 179 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 180 | * |
||
| 181 | * @param string $delimiter |
||
| 182 | * @param string $enclosure |
||
| 183 | * @param string $escape |
||
| 184 | */ |
||
| 185 | 30 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 186 | { |
||
| 187 | 30 | $this->delimiter = $this->filterControl($delimiter, 'delimiter'); |
|
| 188 | 28 | $this->enclosure = $this->filterControl($enclosure, 'enclosure'); |
|
| 189 | 28 | $this->escape = $this->filterControl($escape, 'escape'); |
|
| 190 | 28 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Set StreamIterator Flags |
||
| 194 | * |
||
| 195 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 196 | * |
||
| 197 | * @param int $flags |
||
| 198 | */ |
||
| 199 | 56 | public function setFlags(int $flags) |
|
| 200 | { |
||
| 201 | 56 | $this->flags = $flags; |
|
| 202 | 56 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Write a field array as a CSV line |
||
| 206 | * |
||
| 207 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 208 | * |
||
| 209 | * @param array $fields |
||
| 210 | * @param string $delimiter |
||
| 211 | * @param string $enclosure |
||
| 212 | * @param string $escape |
||
| 213 | * |
||
| 214 | * @return int|bool |
||
| 215 | */ |
||
| 216 | 12 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 217 | { |
||
| 218 | 12 | return fputcsv( |
|
| 219 | 12 | $this->stream, |
|
| 220 | $fields, |
||
| 221 | 12 | $this->filterControl($delimiter, 'delimiter'), |
|
| 222 | 12 | $this->filterControl($enclosure, 'enclosure'), |
|
| 223 | 12 | $this->filterControl($escape, 'escape') |
|
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get line number |
||
| 229 | * |
||
| 230 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 231 | * |
||
| 232 | * @return int |
||
| 233 | */ |
||
| 234 | 32 | public function key() |
|
| 235 | { |
||
| 236 | 32 | return $this->offset; |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Read next line |
||
| 241 | * |
||
| 242 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | 30 | public function next() |
|
| 246 | { |
||
| 247 | 30 | $this->value = false; |
|
| 248 | 30 | $this->offset++; |
|
| 249 | 30 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Rewind the file to the first line |
||
| 253 | * |
||
| 254 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 255 | * |
||
| 256 | */ |
||
| 257 | 56 | public function rewind() |
|
| 258 | { |
||
| 259 | 56 | rewind($this->stream); |
|
| 260 | 56 | $this->offset = 0; |
|
| 261 | 56 | $this->value = false; |
|
| 262 | 56 | if ($this->flags & SplFileObject::READ_AHEAD) { |
|
| 263 | 32 | $this->current(); |
|
| 264 | } |
||
| 265 | 56 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Not at EOF |
||
| 269 | * |
||
| 270 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | 52 | public function valid() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Retrieves the current line of the file. |
||
| 285 | * |
||
| 286 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | 34 | public function current() |
|
| 291 | { |
||
| 292 | 34 | if (false !== $this->value) { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Retrieves the current line as a CSV Record |
||
| 309 | * |
||
| 310 | * @return array|bool |
||
| 311 | */ |
||
| 312 | 32 | protected function getCurrentRecord() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Retrieves the current line as a string |
||
| 323 | * |
||
| 324 | * @return string|bool |
||
| 325 | */ |
||
| 326 | 52 | protected function getCurrentLine() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Seek to specified line |
||
| 337 | * |
||
| 338 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 339 | * |
||
| 340 | * @param int $position |
||
| 341 | */ |
||
| 342 | 20 | public function seek($position) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Gets line from file |
||
| 357 | * |
||
| 358 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 359 | * |
||
| 360 | * @return string|bool |
||
| 361 | */ |
||
| 362 | 52 | public function fgets() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Output all remaining data on a file pointer |
||
| 373 | * |
||
| 374 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 375 | * |
||
| 376 | * @return int |
||
| 377 | */ |
||
| 378 | 2 | public function fpassthru() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Read from file |
||
| 385 | * |
||
| 386 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 387 | * |
||
| 388 | * @param int $length The number of bytes to read |
||
| 389 | * |
||
| 390 | * @return string|false |
||
| 391 | */ |
||
| 392 | 20 | public function fread($length) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Seek to a position |
||
| 399 | * |
||
| 400 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 401 | * |
||
| 402 | * @param int $offset |
||
| 403 | * @param int $whence |
||
| 404 | * |
||
| 405 | * @return int |
||
| 406 | */ |
||
| 407 | 8 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Write to stream |
||
| 414 | * |
||
| 415 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 416 | * |
||
| 417 | * @param string $str |
||
| 418 | * @param int $length |
||
| 419 | * |
||
| 420 | * @return int|bool |
||
| 421 | */ |
||
| 422 | 2 | public function fwrite(string $str, int $length = 0) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * append a filter |
||
| 429 | * |
||
| 430 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 431 | * |
||
| 432 | * @param string $filter_name |
||
| 433 | * @param int $read_write |
||
| 434 | * @param mixed $params |
||
| 435 | * |
||
| 436 | * @throws InvalidArgumentException if the filter can not be appended |
||
| 437 | * |
||
| 438 | * @return resource |
||
| 439 | */ |
||
| 440 | 14 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Removes a registered filter |
||
| 452 | * |
||
| 453 | * @see http://php.net/manual/en/function.stream-filter-remove.php |
||
| 454 | * |
||
| 455 | * @param resource $resource |
||
| 456 | */ |
||
| 457 | 14 | public function removeFilter($resource) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Flushes the output to a file |
||
| 464 | * |
||
| 465 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 466 | * |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | 20 | public function fflush() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @inheritdoc |
||
| 476 | */ |
||
| 477 | 2 | public function __clone() |
|
| 481 | } |
||
| 482 |