Complex classes like AbstractCsv 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 AbstractCsv, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class AbstractCsv |
||
| 13 | { |
||
| 14 | const MODE_READING = 'reading'; |
||
| 15 | const MODE_WRITING = 'writing'; |
||
| 16 | /** |
||
| 17 | * |
||
| 18 | * @var Dialect |
||
| 19 | */ |
||
| 20 | protected $dialect; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Transcoder |
||
| 24 | */ |
||
| 25 | protected $transcoder; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $filename; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $fileHandlerMode; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $mode; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * |
||
| 47 | * @var resource |
||
| 48 | */ |
||
| 49 | protected $fileHandler; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * CSV Header row |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $headers = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | * Default Excel configuration |
||
| 61 | * |
||
| 62 | * @param Dialect|array $options default = [] |
||
| 63 | */ |
||
| 64 | 62 | public function __construct($options = []) |
|
| 69 | |||
| 70 | 41 | public function __destruct() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return array compatible file handler modes |
||
| 77 | */ |
||
| 78 | abstract protected function getCompatibleFileHanderModes(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * get CSV first row header if enabled |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 13 | public function getHeaders() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * |
||
| 92 | * check if a file handle mode is allowed |
||
| 93 | * |
||
| 94 | * @param string $mode |
||
| 95 | * |
||
| 96 | * @throws \InvalidArgumentException |
||
| 97 | */ |
||
| 98 | 6 | protected function checkFileHandleMode($mode) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | * @return Dialect |
||
| 111 | */ |
||
| 112 | 1 | public function getDialect() |
|
| 116 | |||
| 117 | 1 | public function setDialect(Dialect $dialect) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @deprecated since version 1.0.2 use setFile instead |
||
| 127 | * |
||
| 128 | * @param string|resource $filename filename or stream resource |
||
| 129 | * @return AbstractCsv |
||
| 130 | */ |
||
| 131 | public function setFilename($filename) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * |
||
| 138 | * @param string|resource $file filename or stream resource |
||
| 139 | * @return AbstractCsv |
||
| 140 | */ |
||
| 141 | 52 | public function setFile($file) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 5 | public function getFilename() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * |
||
| 180 | * @return resource stream |
||
| 181 | */ |
||
| 182 | 43 | public function getFileHandler() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Write UTF-8 BOM code if encoding is UTF-8 and useBom is set to true |
||
| 189 | * |
||
| 190 | * @return AbstractCsv |
||
| 191 | */ |
||
| 192 | 12 | protected function writeBom() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Remove BOM in the provided string |
||
| 207 | * |
||
| 208 | * @param string $str |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 15 | protected function removeBom($str) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * |
||
| 218 | * @param string $str |
||
| 219 | * @param string $from |
||
| 220 | * @param string $to |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 13 | protected function convertEncoding($str, $from, $to) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * |
||
| 230 | * @param string $mode file handler open mode, default = rb |
||
| 231 | * @return resource file handler |
||
| 232 | * |
||
| 233 | * @throws \InvalidArgumentException |
||
| 234 | */ |
||
| 235 | 48 | protected function openFile($mode = 'rb') |
|
| 248 | |||
| 249 | /** |
||
| 250 | * |
||
| 251 | * @return boolean |
||
| 252 | */ |
||
| 253 | 43 | protected function closeFile() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * |
||
| 267 | * check if a file is already opened and is a stream |
||
| 268 | * |
||
| 269 | * @return boolean |
||
| 270 | */ |
||
| 271 | 54 | public function isFileOpened() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * open a csv file to read or write |
||
| 278 | * |
||
| 279 | * @param string|resource $file filename or stream resource, default = null |
||
| 280 | * @return AbstractCsv |
||
| 281 | * |
||
| 282 | * @throws \InvalidArgumentException |
||
| 283 | */ |
||
| 284 | 34 | public function open($file = null) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Open a temp php stream for reading from a CSV string or Writing CSV to a PHP string |
||
| 296 | * |
||
| 297 | * @param string $csvContent |
||
| 298 | * @return \CSanquer\ColibriCsv\AbstractCsv |
||
| 299 | */ |
||
| 300 | 2 | public function createTempStream($csvContent = null) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * get the current stream resource (or file) content |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 1 | public function getFileContent() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * close the current csv file |
||
| 337 | * |
||
| 338 | * @return AbstractCsv |
||
| 339 | */ |
||
| 340 | 30 | public function close() |
|
| 346 | } |
||
| 347 |