Complex classes like Document 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 Document, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Document implements SeekableIterator |
||
| 34 | { |
||
| 35 | use CsvControlTrait; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Attached filters |
||
| 39 | * |
||
| 40 | * @var resource[] |
||
| 41 | */ |
||
| 42 | protected $filters = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * stream resource |
||
| 46 | * |
||
| 47 | * @var resource |
||
| 48 | */ |
||
| 49 | protected $stream; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Tell whether the stream should be closed on object destruction |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $should_close_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 Document |
||
| 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 | * @param resource $resource stream type resource |
||
| 104 | * |
||
| 105 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
| 106 | */ |
||
| 107 | 34 | public function __construct($resource) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritdoc |
||
| 126 | */ |
||
| 127 | public function __destruct() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @inheritdoc |
||
| 144 | */ |
||
| 145 | 2 | public function __clone() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritdoc |
||
| 152 | */ |
||
| 153 | 2 | public function __debugInfo() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Return a new instance from a file path |
||
| 165 | * |
||
| 166 | * @param string $path file path |
||
| 167 | * @param string $open_mode the file open mode flag |
||
| 168 | * @param resource|null $context the resource context |
||
| 169 | * |
||
| 170 | * @throws RuntimeException if the stream resource can not be created |
||
| 171 | * |
||
| 172 | * @return static |
||
| 173 | */ |
||
| 174 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Return a new instance from a string |
||
| 194 | * |
||
| 195 | * @param string $content the CSV document as a string |
||
| 196 | * |
||
| 197 | * @return static |
||
| 198 | */ |
||
| 199 | 10 | public static function createFromString(string $content): self |
|
| 209 | |||
| 210 | /** |
||
| 211 | * append a filter |
||
| 212 | * |
||
| 213 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
| 214 | * |
||
| 215 | * @param string $filtername |
||
| 216 | * @param int $read_write |
||
| 217 | * @param mixed $params |
||
| 218 | * |
||
| 219 | * @throws RuntimeException if the filter can not be appended |
||
| 220 | */ |
||
| 221 | 10 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Set CSV control |
||
| 234 | * |
||
| 235 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
| 236 | * |
||
| 237 | * @param string $delimiter |
||
| 238 | * @param string $enclosure |
||
| 239 | * @param string $escape |
||
| 240 | */ |
||
| 241 | 22 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Set CSV control |
||
| 250 | * |
||
| 251 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
| 252 | * |
||
| 253 | * @return string[] |
||
| 254 | */ |
||
| 255 | 28 | public function getCsvControl() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Set Document Flags |
||
| 262 | * |
||
| 263 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
| 264 | * |
||
| 265 | * @param int $flags |
||
| 266 | */ |
||
| 267 | 22 | public function setFlags(int $flags) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Write a field array as a CSV line |
||
| 274 | * |
||
| 275 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
| 276 | * |
||
| 277 | * @param array $fields |
||
| 278 | * @param string $delimiter |
||
| 279 | * @param string $enclosure |
||
| 280 | * @param string $escape |
||
| 281 | * |
||
| 282 | * @return int|bool |
||
| 283 | */ |
||
| 284 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Get line number |
||
| 297 | * |
||
| 298 | * @see http://php.net/manual/en/splfileobject.key.php |
||
| 299 | * |
||
| 300 | * @return int |
||
| 301 | */ |
||
| 302 | 12 | public function key() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Read next line |
||
| 309 | * |
||
| 310 | * @see http://php.net/manual/en/splfileobject.next.php |
||
| 311 | * |
||
| 312 | */ |
||
| 313 | 12 | public function next() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Rewind the file to the first line |
||
| 321 | * |
||
| 322 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
| 323 | * |
||
| 324 | */ |
||
| 325 | 22 | public function rewind() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Not at EOF |
||
| 337 | * |
||
| 338 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 20 | public function valid() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Retrieves the current line of the file. |
||
| 353 | * |
||
| 354 | * @see http://php.net/manual/en/splfileobject.current.php |
||
| 355 | * |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | 24 | public function current() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Retrieves the current line as a CSV Record |
||
| 371 | * |
||
| 372 | * @return array|bool |
||
| 373 | */ |
||
| 374 | 22 | protected function getCurrentRecord() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Seek to specified line |
||
| 385 | * |
||
| 386 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
| 387 | * |
||
| 388 | * @param int $position |
||
| 389 | */ |
||
| 390 | 6 | public function seek($position) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Output all remaining data on a file pointer |
||
| 407 | * |
||
| 408 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
| 409 | * |
||
| 410 | * @return int |
||
| 411 | */ |
||
| 412 | 2 | public function fpassthru() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Read from file |
||
| 419 | * |
||
| 420 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
| 421 | * |
||
| 422 | * @param int $length The number of bytes to read |
||
| 423 | * |
||
| 424 | * @return string|false |
||
| 425 | */ |
||
| 426 | 8 | public function fread($length) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Seek to a position |
||
| 433 | * |
||
| 434 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
| 435 | * |
||
| 436 | * @param int $offset |
||
| 437 | * @param int $whence |
||
| 438 | * |
||
| 439 | * @return int |
||
| 440 | */ |
||
| 441 | 4 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Write to stream |
||
| 448 | * |
||
| 449 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 450 | * |
||
| 451 | * @param string $str |
||
| 452 | * @param int $length |
||
| 453 | * |
||
| 454 | * @return int|bool |
||
| 455 | */ |
||
| 456 | 2 | public function fwrite(string $str, int $length = 0) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Flushes the output to a file |
||
| 463 | * |
||
| 464 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | 8 | public function fflush() |
|
| 472 | } |
||
| 473 |