Complex classes like Csv 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 Csv, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 18 | class Csv implements Iterator | ||
| 19 | { | ||
| 20 | |||
| 21 | /** | ||
| 22 | * File handler | ||
| 23 | * @var resource | ||
| 24 | */ | ||
| 25 | protected $handle; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Current CSV line | ||
| 29 | * @var string | ||
| 30 | */ | ||
| 31 | protected $line; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * @var int | ||
| 35 | */ | ||
| 36 | protected $key; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Current offset in bytes | ||
| 40 | * @var integer | ||
| 41 | */ | ||
| 42 | protected $position; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Total CSV file size in bytes | ||
| 46 | * @var integer | ||
| 47 | */ | ||
| 48 | protected $total; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Path to CSV file | ||
| 52 | * @var string | ||
| 53 | */ | ||
| 54 | protected $file; | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Max number of rows to parse | ||
| 58 | * @var integer | ||
| 59 | */ | ||
| 60 | protected $limit; | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Final offset in bytes | ||
| 64 | * @var integer | ||
| 65 | */ | ||
| 66 | protected $last = 0; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Starting offset in bytes | ||
| 70 | * @var integer | ||
| 71 | */ | ||
| 72 | protected $offset = 0; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Array of header names | ||
| 76 | * @var array | ||
| 77 | */ | ||
| 78 | protected $header = array(); | ||
| 79 | |||
| 80 | /** | ||
| 81 | * Skip or not first line | ||
| 82 | * @var boolean | ||
| 83 | */ | ||
| 84 | protected $skip_header = false; | ||
| 85 | |||
| 86 | /** | ||
| 87 | * CSV delimiter | ||
| 88 | * @var string | ||
| 89 | */ | ||
| 90 | protected $delimiter = ","; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Constructor | ||
| 94 | */ | ||
| 95 | public function __construct() | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Closes file handler | ||
| 102 | */ | ||
| 103 | public function __destruct() | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Sets file to parse | ||
| 112 | * @param string $file | ||
| 113 | * @param integer $filesize | ||
| 114 | * @return $this | ||
| 115 | */ | ||
| 116 | public function open($file, $filesize = null) | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Sets max lines to parse | ||
| 131 | * @param integer $limit | ||
| 132 | * @return $this | ||
| 133 | */ | ||
| 134 | public function setLimit($limit) | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Sets separator between columns | ||
| 142 | * @param string $character | ||
| 143 | * @return $this | ||
| 144 | */ | ||
| 145 | public function setDelimiter($character) | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Get first line of CSV file | ||
| 153 | * @return array | ||
| 154 | */ | ||
| 155 | public function getHeader() | ||
| 162 | |||
| 163 | /** | ||
| 164 | * Sets header (first line) | ||
| 165 | * @param array $header | ||
| 166 | * @return $this | ||
| 167 | */ | ||
| 168 | public function setHeader(array $header) | ||
| 173 | |||
| 174 | /** | ||
| 175 | * CSV reader | ||
| 176 | * @return array | ||
| 177 | */ | ||
| 178 | public function read() | ||
| 288 | |||
| 289 | /** | ||
| 290 | * Moves pointer to a certain position | ||
| 291 | * @param integer $position | ||
| 292 | */ | ||
| 293 | public function rewind($position = 0) | ||
| 300 | |||
| 301 | /** | ||
| 302 | * Sets current string and offset | ||
| 303 | * @return null|integer | ||
| 304 | */ | ||
| 305 | public function next() | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Determines if CSV line is valid | ||
| 318 | * @return boolean | ||
| 319 | */ | ||
| 320 | public function valid() | ||
| 324 | |||
| 325 | /** | ||
| 326 | * Gets current CSV row | ||
| 327 | * @return string | ||
| 328 | */ | ||
| 329 | public function current() | ||
| 333 | |||
| 334 | /** | ||
| 335 | * Row key | ||
| 336 | * @return int | ||
| 337 | */ | ||
| 338 | public function key() | ||
| 342 | |||
| 343 | /** | ||
| 344 | * Gets current offset in bytes | ||
| 345 | * @return integer | ||
| 346 | */ | ||
| 347 | public function getPosition() | ||
| 351 | |||
| 352 | /** | ||
| 353 | * Get latest file pointer offset in bytes | ||
| 354 | * @return integer | ||
| 355 | */ | ||
| 356 | public function getOffset() | ||
| 360 | |||
| 361 | /** | ||
| 362 | * Sets initial file offset in bytes | ||
| 363 | * @param integer $offset | ||
| 364 | * @return $this | ||
| 365 | */ | ||
| 366 | public function setOffset($offset) | ||
| 371 | |||
| 372 | /** | ||
| 373 | * Force to skip first row (header) | ||
| 374 | * @return $this | ||
| 375 | */ | ||
| 376 | public function skipHeader() | ||
| 381 | |||
| 382 | /** | ||
| 383 | * Parses CSV into multidimensional array | ||
| 384 | * @return array | ||
| 385 | */ | ||
| 386 | public function parse() | ||
| 394 | |||
| 395 | } | ||
| 396 |