Complex classes like CsvFormat 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 CsvFormat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CsvFormat implements CsvFormatInterface |
||
| 20 | { |
||
| 21 | use GetOptionTrait; |
||
| 22 | |||
| 23 | const DEFAULT_DELIMITER = ','; |
||
| 24 | const DEFAULT_NULL = '\\N'; |
||
| 25 | const DEFAULT_HEADER_ROW = -1; |
||
| 26 | const DEFAULT_DATA_START = 1; |
||
| 27 | const DEFAULT_QUOTE = '"'; |
||
| 28 | const DEFAULT_ESCAPE = '\\'; |
||
| 29 | const DEFAULT_LIMIT = -1; |
||
| 30 | const DEFAULT_DOUBLE_QUOTE = false; |
||
| 31 | const DEFAULT_ENCODING = 'UTF-8'; |
||
| 32 | const DEFAULT_NEW_LINE = "\n"; |
||
| 33 | const DEFAULT_BOM = null; |
||
| 34 | |||
| 35 | const OPTION_DELIMITER = 'delimiter'; |
||
| 36 | const OPTION_NULL = 'null'; |
||
| 37 | const OPTION_HEADER_ROW = 'headerRow'; |
||
| 38 | const OPTION_DATA_START = 'dataStart'; |
||
| 39 | const OPTION_NEW_LINE = 'newLine'; |
||
| 40 | const OPTION_QUOTE = 'quote'; |
||
| 41 | const OPTION_ESCAPE = 'escape'; |
||
| 42 | const OPTION_LIMIT = 'limit'; |
||
| 43 | const OPTION_DOUBLE_QUOTE = 'doubleQuote'; |
||
| 44 | const OPTION_BOM = 'bom'; |
||
| 45 | const OPTION_ENCODING = 'encoding'; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | protected $delimiter; |
||
| 49 | /** @var string */ |
||
| 50 | protected $quote; |
||
| 51 | /** @var string */ |
||
| 52 | protected $nullValue; |
||
| 53 | /** @var int */ |
||
| 54 | protected $headerRow; |
||
| 55 | /** @var string[] */ |
||
| 56 | protected $newLines; |
||
| 57 | /** @var string */ |
||
| 58 | protected $escape; |
||
| 59 | /** @var int */ |
||
| 60 | protected $limit; |
||
| 61 | /** @var bool */ |
||
| 62 | protected $doubleQuote; |
||
| 63 | /** @var int */ |
||
| 64 | protected $dataStart; |
||
| 65 | /** @var string[]|string|null */ |
||
| 66 | protected $boms; |
||
| 67 | /** @var string */ |
||
| 68 | protected $encoding; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param array $options -delimiter <string> (Default: ,) Character to use between fields |
||
| 72 | * -quoteCharacter <string> (Default: ") |
||
| 73 | * -nullOutput <string> (Default: \N) |
||
| 74 | * -headerRow <int> (Default: -1) -1 for no header row. (1 is the first line of the file) |
||
| 75 | * -dataStart <int> (Default: 1) The line where the data starts (1 is the first list of the |
||
| 76 | * file) |
||
| 77 | * -lineTerminator <array> (Default: ["\n","\r","\r\n"]) |
||
| 78 | * -escape <string> (Default: \\) Character to use for escaping |
||
| 79 | * -limit <int> Total number of data rows to return |
||
| 80 | * -doubleQuote <bool> instances of quote in fields are indicated by a double quote |
||
| 81 | * -bom <array> (Default: BOM_ALL) Specify a ByteOrderMark for this file (see Bom::BOM_*) |
||
| 82 | * -encoding <string> (Default: UTF-8) Specify the encoding of the csv file |
||
| 83 | */ |
||
| 84 | 21 | public function __construct(array $options = []) |
|
| 85 | { |
||
| 86 | 21 | $this->options = $options; |
|
| 87 | 21 | $this->delimiter = $this->getOption(static::OPTION_DELIMITER, static::DEFAULT_DELIMITER); |
|
| 88 | 21 | $this->quote = $this->getOption(static::OPTION_QUOTE, static::DEFAULT_QUOTE); |
|
| 89 | 21 | $this->nullValue = $this->getOption(static::OPTION_NULL, static::DEFAULT_NULL); |
|
| 90 | 21 | $this->headerRow = $this->getOption(static::OPTION_HEADER_ROW, static::DEFAULT_HEADER_ROW); |
|
| 91 | 21 | $this->dataStart = $this->getOption(static::OPTION_DATA_START, static::DEFAULT_DATA_START); |
|
| 92 | 21 | $this->escape = $this->getOption(static::OPTION_ESCAPE, static::DEFAULT_ESCAPE); |
|
| 93 | 21 | $this->limit = $this->getOption(static::OPTION_LIMIT, static::DEFAULT_LIMIT); |
|
| 94 | 21 | $this->doubleQuote = $this->getOption(static::OPTION_DOUBLE_QUOTE, static::DEFAULT_DOUBLE_QUOTE); |
|
| 95 | 21 | $this->encoding = $this->getOption(static::OPTION_ENCODING, static::DEFAULT_ENCODING); |
|
| 96 | 21 | $this->setBom($this->getOption(static::OPTION_BOM, static::DEFAULT_BOM)); |
|
| 97 | 21 | $this->setNewLine($this->getOption(static::OPTION_NEW_LINE, ["\n", "\r", "\r\n"])); |
|
| 98 | 21 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 35 | public function getDelimiter() |
|
| 104 | { |
||
| 105 | 35 | return $this->delimiter; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $delimiter |
||
| 110 | * |
||
| 111 | * @return static |
||
| 112 | */ |
||
| 113 | 2 | public function setDelimiter($delimiter) |
|
| 114 | { |
||
| 115 | 2 | $this->delimiter = $delimiter; |
|
| 116 | 2 | return $this; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 34 | public function getQuote() |
|
| 123 | { |
||
| 124 | 34 | return $this->quote; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | 25 | public function hasQuote() |
|
| 131 | { |
||
| 132 | 25 | return $this->quote <> ''; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $quote |
||
| 137 | * |
||
| 138 | * @return static |
||
| 139 | */ |
||
| 140 | 1 | public function setQuote($quote) |
|
| 141 | { |
||
| 142 | 1 | $this->quote = $quote; |
|
| 143 | 1 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 19 | public function getNullValue() |
|
| 150 | { |
||
| 151 | 19 | return $this->nullValue; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $nullValue |
||
| 156 | * |
||
| 157 | * @return static |
||
| 158 | */ |
||
| 159 | 1 | public function setNullValue($nullValue) |
|
| 160 | { |
||
| 161 | 1 | $this->nullValue = $nullValue; |
|
| 162 | 1 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 33 | public function hasHeaderRow() |
|
| 169 | { |
||
| 170 | 33 | return $this->headerRow > 0; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $headerRow |
||
| 175 | * |
||
| 176 | * @return static |
||
| 177 | */ |
||
| 178 | 2 | public function setHeaderRow($headerRow) |
|
| 179 | { |
||
| 180 | 2 | $this->headerRow = $headerRow; |
|
| 181 | 2 | return $this; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | 12 | public function getHeaderRow() |
|
| 188 | { |
||
| 189 | 12 | return $this->headerRow; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | 25 | public function getDataStart() |
|
| 196 | { |
||
| 197 | 25 | if ($this->hasHeaderRow() && $this->getHeaderRow() >= $this->dataStart) { |
|
| 198 | 7 | return max(1, $this->getHeaderRow() + 1); |
|
| 199 | } |
||
| 200 | 19 | return max(1, $this->dataStart); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param int $row |
||
| 205 | * |
||
| 206 | * @return static |
||
| 207 | */ |
||
| 208 | 2 | public function setDataStart($row) |
|
| 209 | { |
||
| 210 | 2 | $this->dataStart = $row; |
|
| 211 | 2 | return $this; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Type type of file format (defined in FileFormatType::) |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | 9 | public function getType() |
|
| 220 | { |
||
| 221 | 9 | return 'csv'; |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 37 | public function getEscape() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $escape |
||
| 234 | * |
||
| 235 | * @return static |
||
| 236 | */ |
||
| 237 | 1 | public function setEscape($escape) |
|
| 238 | { |
||
| 239 | 1 | $this->escape = $escape; |
|
| 240 | 1 | return $this; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | 3 | public function hasEscape() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get the limit that should be returned (-1 for no limit) |
||
| 253 | * |
||
| 254 | * @return int |
||
| 255 | */ |
||
| 256 | 15 | public function getLimit() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Set the limit of the number of items to be returned (-1 for not limit) |
||
| 263 | * |
||
| 264 | * @param int $limit |
||
| 265 | * |
||
| 266 | * @return static |
||
| 267 | */ |
||
| 268 | 1 | public function setLimit($limit) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 32 | public function useDoubleQuotes() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param bool $doubleQuote |
||
| 284 | * |
||
| 285 | * @return static |
||
| 286 | */ |
||
| 287 | 1 | public function setDoubleQuote($doubleQuote) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 33 | public function getEncoding() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $encoding |
||
| 307 | * |
||
| 308 | * @return static |
||
| 309 | */ |
||
| 310 | 1 | public function setEncoding($encoding) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @return string[] |
||
| 318 | */ |
||
| 319 | 15 | public function getNewLines() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param string|string[] $newLine |
||
| 326 | * |
||
| 327 | * @return static |
||
| 328 | */ |
||
| 329 | 21 | public function setNewLine($newLine) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get a new line for writing |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 13 | public function getNewLine() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param null|string[]|string $bom |
||
| 347 | * |
||
| 348 | * @return static |
||
| 349 | */ |
||
| 350 | 21 | public function setBom($bom) |
|
| 351 | { |
||
| 352 | 21 | $this->boms = $bom; |
|
| 353 | 21 | if (!is_null($bom)) { |
|
| 354 | 5 | $testBom = is_array($bom) ? reset($bom) : $bom; |
|
| 355 | 5 | Bom::getEncoding($testBom); |
|
| 356 | } |
||
| 357 | 21 | return $this; |
|
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string[] |
||
| 362 | */ |
||
| 363 | 15 | public function getBoms() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get a ByteOrderMark for writing if applicable |
||
| 376 | * |
||
| 377 | * @return string|null |
||
| 378 | */ |
||
| 379 | 25 | public function getBom() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @return string[] |
||
| 386 | */ |
||
| 387 | 13 | private function getDefaultBoms() |
|
| 391 | } |
||
| 392 |