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 |
||
| 17 | class Csv |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * File handler |
||
| 22 | * @var resource |
||
| 23 | */ |
||
| 24 | protected $handle; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Current CSV line |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $current_line; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Current offset in bytes |
||
| 34 | * @var integer |
||
| 35 | */ |
||
| 36 | protected $current_position; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Total CSV file size in bytes |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | protected $total; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Path to CSV file |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $file; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Max number of rows to parse |
||
| 52 | * @var integer |
||
| 53 | */ |
||
| 54 | protected $limit; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Final offset in bytes |
||
| 58 | * @var integer |
||
| 59 | */ |
||
| 60 | protected $last_position = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Starting offset in bytes |
||
| 64 | * @var integer |
||
| 65 | */ |
||
| 66 | protected $offset = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Array of header names |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $header = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Skip or not first line |
||
| 76 | * @var boolean |
||
| 77 | */ |
||
| 78 | protected $skip_header = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * CSV delimiter |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $delimiter = ","; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Constructor |
||
| 88 | */ |
||
| 89 | public function __construct() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Closes file handler |
||
| 96 | */ |
||
| 97 | public function __destruct() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Sets file to parse |
||
| 104 | * @param string $file |
||
| 105 | * @param integer $filesize |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function open($file, $filesize = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Close file handle |
||
| 123 | */ |
||
| 124 | public function close() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Sets max lines to parse |
||
| 133 | * @param integer $limit |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setLimit($limit) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Sets separator between columns |
||
| 144 | * @param string $character |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function setDelimiter($character) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get first line of CSV file |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getHeader() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sets header (first line) |
||
| 166 | * @param array $header |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setHeader(array $header) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * CSV reader |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function read() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Moves pointer to a certain position |
||
| 288 | * @param integer $position |
||
| 289 | */ |
||
| 290 | protected function rewind($position = 0) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sets current string and offset |
||
| 300 | * @return null|integer |
||
| 301 | */ |
||
| 302 | protected function next() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Determines if CSV line is valid |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | protected function valid() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets current CSV row |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | protected function current() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Gets current offset in bytes |
||
| 333 | * @return integer |
||
| 334 | */ |
||
| 335 | protected function currentPosition() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get latest file pointer offset in bytes |
||
| 342 | * @return integer |
||
| 343 | */ |
||
| 344 | public function getOffset() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Sets initial file offset in bytes |
||
| 351 | * @param integer $offset |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function setOffset($offset) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Force to skip first row (header) |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function skipHeader() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Parses CSV into multidimensional array |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | public function parse() |
||
| 382 | |||
| 383 | } |
||
| 384 |