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 |
||
| 13 | class CSV { |
||
| 14 | use Module; |
||
| 15 | |||
| 16 | const AUTO = null, |
||
| 17 | STANDARD = ',', |
||
| 18 | EXCEL = ';', |
||
| 19 | TAB = "\t", |
||
| 20 | READ = 'r', |
||
| 21 | WRITE = 'w'; |
||
| 22 | |||
| 23 | protected $file, |
||
| 24 | $headers = [], |
||
| 25 | $template = [], |
||
| 26 | $mode = self::WRITE, |
||
| 27 | $format = self::STANDARD, |
||
| 28 | $savedheaders = false; |
||
| 29 | |||
| 30 | public static function open($file, $format=self::AUTO){ |
||
| 33 | |||
| 34 | public static function create($file, $format=self::STANDARD){ |
||
| 37 | |||
| 38 | public function SQL($sql){ |
||
| 45 | |||
| 46 | public static function fromSQL($sql, $format=self::AUTO){ |
||
| 49 | |||
| 50 | public static function fromTable($table, $format=self::AUTO){ |
||
| 57 | |||
| 58 | public function __construct($file, $mode=self::READ, $format=self::AUTO){ |
||
| 70 | |||
| 71 | private function guessSeparator($checkLines = 2){ |
||
| 93 | |||
| 94 | public function write($row){ |
||
| 106 | |||
| 107 | public function read(){ |
||
| 120 | |||
| 121 | public function each(callable $looper = null){ |
||
| 131 | |||
| 132 | public function convert($filename, $format=self::STANDARD){ |
||
| 141 | |||
| 142 | public function flush(){ |
||
| 147 | |||
| 148 | public function schema($schema=null){ |
||
| 161 | |||
| 162 | public function asString(){ |
||
| 166 | |||
| 167 | public function __toString(){ |
||
| 170 | |||
| 171 | } |
||
| 172 |