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 |
||
| 29 | class StreamIterator implements Iterator |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Attached filters |
||
| 33 | * |
||
| 34 | * @var resource[] |
||
| 35 | */ |
||
| 36 | protected $filters; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Stream pointer |
||
| 40 | * |
||
| 41 | * @var resource |
||
| 42 | */ |
||
| 43 | protected $stream; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Current iterator value |
||
| 47 | * |
||
| 48 | * @var mixed |
||
| 49 | */ |
||
| 50 | protected $current_line; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Current iterator key |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $current_line_number; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Flags for the StreamIterator |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $flags = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * the field delimiter (one character only) |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $delimiter = ','; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * the field enclosure character (one character only) |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $enclosure = '"'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * the field escape character (one character only) |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $escape = '\\'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * New instance |
||
| 89 | * |
||
| 90 | * @param resource $stream stream type resource |
||
| 91 | */ |
||
| 92 | 58 | public function __construct($stream) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * close the file pointer |
||
| 111 | */ |
||
| 112 | 58 | public function __destruct() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Set CSV control |
||
| 119 | * |
||
| 120 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 121 | * |
||
| 122 | * @param string $delimiter |
||
| 123 | * @param string $enclosure |
||
| 124 | * @param string $escape |
||
| 125 | */ |
||
| 126 | 20 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Filter Csv control character |
||
| 135 | * |
||
| 136 | * @param string $char Csv control character |
||
| 137 | * @param string $type Csv control character type |
||
| 138 | * |
||
| 139 | * @throws InvalidArgumentException If the Csv control character is not one character only. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 34 | protected function filterControl(string $char, string $type) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Set Flags |
||
| 154 | * |
||
| 155 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 156 | * |
||
| 157 | * @param int $flags |
||
| 158 | */ |
||
| 159 | 40 | public function setFlags(int $flags) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Write a field array as a CSV line |
||
| 166 | * |
||
| 167 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 168 | * |
||
| 169 | * @param array $fields |
||
| 170 | * @param string $delimiter |
||
| 171 | * @param string $enclosure |
||
| 172 | * @param string $escape |
||
| 173 | * |
||
| 174 | * @return int|false |
||
| 175 | */ |
||
| 176 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 177 | { |
||
| 178 | 14 | return fputcsv( |
|
| 179 | 14 | $this->stream, |
|
| 180 | $fields, |
||
| 181 | 14 | $this->filterControl($delimiter, 'delimiter'), |
|
| 182 | 14 | $this->filterControl($enclosure, 'enclosure'), |
|
| 183 | 14 | $this->filterControl($escape, 'escape') |
|
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Retrieves the current line of the file. |
||
| 189 | * |
||
| 190 | * @return mixed |
||
| 191 | */ |
||
| 192 | 24 | public function current() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Retrieves the current line as a CSV Record |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 22 | protected function getCurrentRecord() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Retrieves the current line as a string |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 34 | protected function getCurrentLine() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get line number |
||
| 239 | * |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | 20 | public function key() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Read next line |
||
| 249 | */ |
||
| 250 | 20 | public function next() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Rewind the file to the first line |
||
| 258 | */ |
||
| 259 | 40 | public function rewind() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Not at EOF |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | 22 | public function valid() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Gets line from file |
||
| 285 | * |
||
| 286 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 34 | public function fgets() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Output all remaining data on a file pointer |
||
| 300 | * |
||
| 301 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 302 | * |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | 16 | public function fpassthru() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Seek to a position |
||
| 312 | * |
||
| 313 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 314 | * |
||
| 315 | * @param int $offset |
||
| 316 | * @param int $whence |
||
| 317 | * |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | 4 | public function fseek($offset, $whence = SEEK_SET) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Seek a specified line |
||
| 327 | * |
||
| 328 | * @param int $line_pos |
||
| 329 | * |
||
| 330 | * @throws LogicException if the line positon is negative |
||
| 331 | */ |
||
| 332 | 6 | public function seek(int $line_pos) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Write to stream |
||
| 350 | * |
||
| 351 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 352 | * |
||
| 353 | * @param string $str |
||
| 354 | * @param int $length |
||
| 355 | * |
||
| 356 | * @return int |
||
| 357 | */ |
||
| 358 | 2 | public function fwrite($str, $length = 0) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * append a filter |
||
| 365 | * |
||
| 366 | * @param string $filter_name |
||
| 367 | * |
||
| 368 | * @return resource |
||
| 369 | */ |
||
| 370 | 10 | public function appendFilter(string $filter_name, int $read_write) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * prepend a filter |
||
| 377 | * |
||
| 378 | * @param string $filter_name |
||
| 379 | * |
||
| 380 | * @return resource |
||
| 381 | */ |
||
| 382 | 2 | public function prependFilter(string $filter_name, int $read_write) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * remove all attached filters |
||
| 389 | */ |
||
| 390 | 10 | public function removeFilter($resource) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Flushes the output to a file |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | 2 | public function fflush() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @inheritdoc |
||
| 407 | */ |
||
| 408 | 2 | public function __clone() |
|
| 412 | } |
||
| 413 |