Complex classes like Reader 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 Reader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * header offset. |
||
| 57 | * |
||
| 58 | * @var int|null |
||
| 59 | */ |
||
| 60 | protected $header_offset; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * header record. |
||
| 64 | * |
||
| 65 | * @var string[] |
||
| 66 | */ |
||
| 67 | protected $header = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * records count. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected $nb_records = -1; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 3 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 85 | { |
||
| 86 | 3 | return new static(Stream::createFromPath($path, $open_mode, $context)); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the header offset. |
||
| 91 | * |
||
| 92 | * If no CSV header offset is set this method MUST return null |
||
| 93 | * |
||
| 94 | * @return int|null |
||
| 95 | */ |
||
| 96 | 15 | public function getHeaderOffset() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the CSV record used as header. |
||
| 103 | * |
||
| 104 | * The returned header is represented as an array of string values |
||
| 105 | * |
||
| 106 | * @return string[] |
||
| 107 | */ |
||
| 108 | 15 | public function getHeader(): array |
|
| 109 | { |
||
| 110 | 15 | if (null === $this->header_offset) { |
|
| 111 | 12 | return $this->header; |
|
| 112 | } |
||
| 113 | |||
| 114 | 6 | if ([] !== $this->header) { |
|
| 115 | 3 | return $this->header; |
|
| 116 | } |
||
| 117 | |||
| 118 | 6 | $this->header = $this->setHeader($this->header_offset); |
|
| 119 | |||
| 120 | 6 | return $this->header; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Determine the CSV record header. |
||
| 125 | * |
||
| 126 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
| 127 | * |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | 12 | protected function setHeader(int $offset): array |
|
| 131 | { |
||
| 132 | 12 | $header = $this->seekRow($offset); |
|
| 133 | 12 | if (false === $header || [] === $header) { |
|
| 134 | 6 | throw new Exception(sprintf('The header record does not exist or is empty at offset: `%s`', $offset)); |
|
| 135 | } |
||
| 136 | |||
| 137 | 6 | if (0 === $offset) { |
|
| 138 | 3 | return $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure); |
|
| 139 | } |
||
| 140 | |||
| 141 | 3 | return $header; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the row at a given offset. |
||
| 146 | */ |
||
| 147 | 6 | protected function seekRow(int $offset) |
|
| 148 | { |
||
| 149 | 6 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
| 150 | 6 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
| 151 | 6 | $this->document->rewind(); |
|
| 152 | |||
| 153 | //Workaround for SplFileObject::seek bug in PHP7.2+ see https://bugs.php.net/bug.php?id=75917 |
||
| 154 | 6 | if (PHP_VERSION_ID >= 70200 && !$this->document instanceof Stream) { |
|
| 155 | while ($offset !== $this->document->key() && $this->document->valid()) { |
||
| 156 | $this->document->next(); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this->document->current(); |
||
| 160 | } |
||
| 161 | |||
| 162 | 6 | $this->document->seek($offset); |
|
| 163 | |||
| 164 | 6 | return $this->document->current(); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Strip the BOM sequence from a record. |
||
| 169 | * |
||
| 170 | * @param string[] $record |
||
| 171 | * |
||
| 172 | * @return string[] |
||
| 173 | */ |
||
| 174 | 12 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 175 | { |
||
| 176 | 12 | if (0 == $bom_length) { |
|
| 177 | 3 | return $record; |
|
| 178 | } |
||
| 179 | |||
| 180 | 9 | $record[0] = mb_substr($record[0], $bom_length); |
|
| 181 | 9 | if ($enclosure.$enclosure != substr($record[0].$record[0], strlen($record[0]) - 1, 2)) { |
|
| 182 | 6 | return $record; |
|
| 183 | } |
||
| 184 | |||
| 185 | 3 | $record[0] = substr($record[0], 1, -1); |
|
| 186 | |||
| 187 | 3 | return $record; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 9 | public function __call($method, array $arguments) |
|
| 194 | { |
||
| 195 | 9 | static $whitelisted = ['fetchColumn' => 1, 'fetchOne' => 1, 'fetchPairs' => 1]; |
|
| 196 | 9 | if (isset($whitelisted[$method])) { |
|
| 197 | 3 | return (new ResultSet($this->getRecords(), $this->getHeader()))->$method(...$arguments); |
|
| 198 | } |
||
| 199 | |||
| 200 | 6 | throw new BadMethodCallException(sprintf('%s::%s() method does not exist', __CLASS__, $method)); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 3 | public function count(): int |
|
| 207 | { |
||
| 208 | 3 | if (-1 === $this->nb_records) { |
|
| 209 | 3 | $this->nb_records = iterator_count($this->getRecords()); |
|
| 210 | } |
||
| 211 | |||
| 212 | 3 | return $this->nb_records; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | 3 | public function getIterator(): Iterator |
|
| 219 | { |
||
| 220 | 3 | return $this->getRecords(); |
|
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 3 | public function jsonSerialize(): array |
|
| 227 | { |
||
| 228 | 3 | return iterator_to_array($this->getRecords(), false); |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the CSV records as an iterator object. |
||
| 233 | * |
||
| 234 | * Each CSV record is represented as a simple array containig strings or null values. |
||
| 235 | * |
||
| 236 | * If the CSV document has a header record then each record is combined |
||
| 237 | * to the header record and the header record is removed from the iterator. |
||
| 238 | * |
||
| 239 | * If the CSV document is inconsistent. Missing record fields are |
||
| 240 | * filled with null values while extra record fields are strip from |
||
| 241 | * the returned object. |
||
| 242 | * |
||
| 243 | * @param string[] $header an optional header to use instead of the CSV document header |
||
| 244 | */ |
||
| 245 | 18 | public function getRecords(array $header = []): Iterator |
|
| 246 | { |
||
| 247 | 18 | $header = $this->computeHeader($header); |
|
| 248 | 15 | $normalized = function ($record): bool { |
|
| 249 | 15 | return is_array($record) && $record != [null]; |
|
| 250 | 15 | }; |
|
| 251 | 15 | $bom = $this->getInputBOM(); |
|
| 252 | 15 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
| 253 | 15 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
| 254 | |||
| 255 | 15 | $records = $this->stripBOM(new CallbackFilterIterator($this->document, $normalized), $bom); |
|
| 256 | 15 | if (null !== $this->header_offset) { |
|
| 257 | 6 | $records = new CallbackFilterIterator($records, function (array $record, int $offset): bool { |
|
| 258 | 6 | return $offset !== $this->header_offset; |
|
| 259 | 6 | }); |
|
| 260 | } |
||
| 261 | |||
| 262 | 15 | return $this->combineHeader($records, $header); |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns the header to be used for iteration. |
||
| 267 | * |
||
| 268 | * @param string[] $header |
||
| 269 | * |
||
| 270 | * @throws Exception If the header contains non unique column name |
||
| 271 | * |
||
| 272 | * @return string[] |
||
| 273 | */ |
||
| 274 | 24 | protected function computeHeader(array $header) |
|
| 275 | { |
||
| 276 | 24 | if ([] === $header) { |
|
| 277 | 21 | $header = $this->getHeader(); |
|
| 278 | } |
||
| 279 | |||
| 280 | 24 | if ($header === array_unique(array_filter($header, 'is_string'))) { |
|
| 281 | 21 | return $header; |
|
| 282 | } |
||
| 283 | |||
| 284 | 3 | throw new Exception('The header record must be empty or a flat array with unique string values'); |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Combine the CSV header to each record if present. |
||
| 289 | * |
||
| 290 | * @param string[] $header |
||
| 291 | */ |
||
| 292 | 30 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
| 293 | { |
||
| 294 | 30 | if ([] === $header) { |
|
| 295 | 21 | return $iterator; |
|
| 296 | } |
||
| 297 | |||
| 298 | 12 | $field_count = count($header); |
|
| 299 | 12 | $mapper = function (array $record) use ($header, $field_count): array { |
|
| 300 | 12 | if (count($record) != $field_count) { |
|
| 301 | 6 | $record = array_slice(array_pad($record, $field_count, null), 0, $field_count); |
|
| 302 | } |
||
| 303 | |||
| 304 | 12 | return array_combine($header, $record); |
|
| 305 | 12 | }; |
|
| 306 | |||
| 307 | 12 | return new MapIterator($iterator, $mapper); |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Strip the BOM sequence from the returned records if necessary. |
||
| 312 | */ |
||
| 313 | 24 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Selects the record to be used as the CSV header. |
||
| 333 | * |
||
| 334 | * Because the header is represented as an array, to be valid |
||
| 335 | * a header MUST contain only unique string value. |
||
| 336 | * |
||
| 337 | * @param int|null $offset the header record offset |
||
| 338 | * |
||
| 339 | * @throws Exception if the offset is a negative integer |
||
| 340 | * |
||
| 341 | * @return static |
||
| 342 | */ |
||
| 343 | 21 | public function setHeaderOffset($offset): self |
|
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | 24 | protected function resetProperties() |
|
| 367 | { |
||
| 368 | 24 | parent::resetProperties(); |
|
| 369 | 24 | $this->nb_records = -1; |
|
| 370 | 24 | $this->header = []; |
|
| 371 | 24 | } |
|
| 372 | } |
||
| 373 |