| Conditions | 20 |
| Paths | 46 |
| Total Lines | 106 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 179 | public function read() |
||
| 180 | { |
||
| 181 | $rows = array(); |
||
| 182 | |||
| 183 | $start = $this->offset ? $this->offset : $this->last_position; |
||
| 184 | $this->last_position = 0; |
||
| 185 | |||
| 186 | $parsed = 0; |
||
| 187 | for ($this->rewind($start); $this->valid(); $this->next()) { |
||
| 188 | |||
| 189 | $line = trim($this->current(), "\r\n"); |
||
| 190 | |||
| 191 | if (empty($line)) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($this->skip_header) { |
||
| 196 | $this->skip_header = false; |
||
| 197 | continue; |
||
| 198 | } |
||
| 199 | |||
| 200 | $quoted = false; |
||
| 201 | $current_index = 0; |
||
| 202 | $current_field = ''; |
||
| 203 | $fields = array(); |
||
| 204 | |||
| 205 | while ($current_index <= strlen($line)) { |
||
| 206 | if ($quoted) { |
||
| 207 | $next_quote_index = strpos($line, '"', $current_index); |
||
| 208 | |||
| 209 | if ($next_quote_index === false) { |
||
| 210 | $current_field .= substr($line, $current_index); |
||
| 211 | $this->next(); |
||
| 212 | |||
| 213 | if (!$this->valid()) { |
||
| 214 | $fields[] = $current_field; |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | |||
| 218 | $current_field .= "\n"; |
||
| 219 | $line = trim($this->current(), "\r\n"); |
||
| 220 | $current_index = 0; |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | $current_field .= substr($line, $current_index, $next_quote_index - $current_index); |
||
| 225 | |||
| 226 | if (isset($line[$next_quote_index + 1]) && $line[$next_quote_index + 1] === '"') { |
||
| 227 | $current_field .= '"'; |
||
| 228 | $current_index = $next_quote_index + 2; |
||
| 229 | } else { |
||
| 230 | $quoted = false; |
||
| 231 | $current_index = $next_quote_index + 1; |
||
| 232 | } |
||
| 233 | } else { |
||
| 234 | $next_quote_index = strpos($line, '"', $current_index); |
||
| 235 | $next_delimiter_index = strpos($line, $this->delimiter, $current_index); |
||
| 236 | |||
| 237 | if ($next_quote_index === false) { |
||
| 238 | $next_index = $next_delimiter_index; |
||
| 239 | } elseif ($next_delimiter_index === false) { |
||
| 240 | $next_index = $next_quote_index; |
||
| 241 | } else { |
||
| 242 | $next_index = min($next_quote_index, $next_delimiter_index); |
||
| 243 | } |
||
| 244 | |||
| 245 | if ($next_index === false) { |
||
| 246 | $current_field .= substr($line, $current_index); |
||
| 247 | $fields[] = $current_field; |
||
| 248 | break; |
||
| 249 | } elseif ($line[$next_index] === $this->delimiter) { |
||
| 250 | $length = ($next_index + strlen($this->delimiter) - 1) - $current_index; |
||
| 251 | $current_field .= substr($line, $current_index, $length); |
||
| 252 | $fields[] = $current_field; |
||
| 253 | $current_field = ''; |
||
| 254 | $current_index += $length + 1; |
||
| 255 | } else { |
||
| 256 | $quoted = true; |
||
| 257 | $current_field .= substr($line, $current_index, $next_index - $current_index); |
||
| 258 | $current_index = $next_index + 1; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if (!empty($this->header)) { |
||
| 264 | $row = array(); |
||
| 265 | foreach ($this->header as $key => $name) { |
||
| 266 | $field = array_shift($fields); |
||
| 267 | $row[$key] = isset($field) ? $field : ''; |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | $row = $fields; |
||
| 271 | } |
||
| 272 | |||
| 273 | $rows[] = $row; |
||
| 274 | |||
| 275 | $parsed++; |
||
| 276 | |||
| 277 | if (!empty($this->limit) && $parsed >= $this->limit) { |
||
| 278 | $this->last_position = $this->currentPosition(); |
||
| 279 | break; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return $rows; |
||
| 284 | } |
||
| 285 | |||
| 384 |