| Conditions | 20 | 
| Paths | 46 | 
| Total Lines | 110 | 
| Code Lines | 74 | 
| 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 | ||
| 178 | public function read() | ||
| 179 |     { | ||
| 180 | $rows = array(); | ||
| 181 | |||
| 182 | $start = $this->offset ? $this->offset : $this->last; | ||
| 183 | |||
| 184 | $this->key = 0; | ||
| 185 | $this->last = 0; | ||
| 186 | |||
| 187 | $parsed = 0; | ||
| 188 |         for ($this->rewind($start); $this->valid(); $this->next()) { | ||
| 189 | |||
| 190 | $line = trim($this->current(), "\r\n"); | ||
| 191 | |||
| 192 |             if (empty($line)) { | ||
| 193 | continue; | ||
| 194 | } | ||
| 195 | |||
| 196 |             if ($this->skip_header) { | ||
| 197 | $this->skip_header = false; | ||
| 198 | continue; | ||
| 199 | } | ||
| 200 | |||
| 201 | $quoted = false; | ||
| 202 | $current_index = 0; | ||
| 203 | $current_field = ''; | ||
| 204 | $fields = array(); | ||
| 205 | |||
| 206 |             while ($current_index <= strlen($line)) { | ||
| 207 |                 if ($quoted) { | ||
| 208 | $next_quote_index = strpos($line, '"', $current_index); | ||
| 209 | |||
| 210 |                     if ($next_quote_index === false) { | ||
| 211 | $current_field .= substr($line, $current_index); | ||
| 212 | $this->next(); | ||
| 213 | |||
| 214 |                         if (!$this->valid()) { | ||
| 215 | $fields[] = $current_field; | ||
| 216 | break; | ||
| 217 | } | ||
| 218 | |||
| 219 | $current_field .= "\n"; | ||
| 220 | $line = trim($this->current(), "\r\n"); | ||
| 221 | $current_index = 0; | ||
| 222 | continue; | ||
| 223 | } | ||
| 224 | |||
| 225 | $current_field .= substr($line, $current_index, $next_quote_index - $current_index); | ||
| 226 | |||
| 227 |                     if (isset($line[$next_quote_index + 1]) && $line[$next_quote_index + 1] === '"') { | ||
| 228 | $current_field .= '"'; | ||
| 229 | $current_index = $next_quote_index + 2; | ||
| 230 |                     } else { | ||
| 231 | $quoted = false; | ||
| 232 | $current_index = $next_quote_index + 1; | ||
| 233 | } | ||
| 234 |                 } else { | ||
| 235 | $next_quote_index = strpos($line, '"', $current_index); | ||
| 236 | $next_delimiter_index = strpos($line, $this->delimiter, $current_index); | ||
| 237 | |||
| 238 |                     if ($next_quote_index === false) { | ||
| 239 | $next_index = $next_delimiter_index; | ||
| 240 |                     } elseif ($next_delimiter_index === false) { | ||
| 241 | $next_index = $next_quote_index; | ||
| 242 |                     } else { | ||
| 243 | $next_index = min($next_quote_index, $next_delimiter_index); | ||
| 244 | } | ||
| 245 | |||
| 246 |                     if ($next_index === false) { | ||
| 247 | $current_field .= substr($line, $current_index); | ||
| 248 | $fields[] = $current_field; | ||
| 249 | break; | ||
| 250 |                     } elseif ($line[$next_index] === $this->delimiter) { | ||
| 251 | $length = ($next_index + strlen($this->delimiter) - 1) - $current_index; | ||
| 252 | $current_field .= substr($line, $current_index, $length); | ||
| 253 | $fields[] = $current_field; | ||
| 254 | $current_field = ''; | ||
| 255 | $current_index += $length + 1; | ||
| 256 |                     } else { | ||
| 257 | $quoted = true; | ||
| 258 | $current_field .= substr($line, $current_index, $next_index - $current_index); | ||
| 259 | $current_index = $next_index + 1; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 |             if (!empty($this->header)) { | ||
| 265 | $row = array(); | ||
| 266 |                 foreach ($this->header as $key => $name) { | ||
| 267 | $field = array_shift($fields); | ||
| 268 | $row[$key] = isset($field) ? $field : ''; | ||
| 269 | } | ||
| 270 |             } else { | ||
| 271 | $row = $fields; | ||
| 272 | } | ||
| 273 | |||
| 274 | $rows[$this->key] = $row; | ||
| 275 | |||
| 276 | $parsed++; | ||
| 277 | |||
| 278 |             if (!empty($this->limit) && $parsed >= $this->limit) { | ||
| 279 | $this->last = $this->getPosition(); | ||
| 280 | break; | ||
| 281 | } | ||
| 282 | |||
| 283 | $this->key++; | ||
| 284 | } | ||
| 285 | |||
| 286 | return $rows; | ||
| 287 | } | ||
| 288 | |||
| 396 |