Iterator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 23
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 13 3
1
<?php
2
3
/**
4
 * Example:
5
 *
6
 * $csv = new \Csv\Iterator('/path/to/huge/file.csv');
7
 * foreach ($csv->parse() as $row) {
8
 *     // Do something with the row.
9
 * }
10
 */
11
12
namespace AtBase\Csv;
13
14
class Iterator
15
{
16
    protected $file;
17
18
    public function __construct($file)
19
    {
20
        $this->file = fopen($file, 'r');
21
    }
22
23
    public function parse()
24
    {
25
        $headers = array_map('trim', fgetcsv($this->file, 4096));
26
        while (!feof($this->file)) {
27
            $row = array_map('trim', (array)fgetcsv($this->file, 4096));
28
            if (count($headers) !== count($row)) {
29
                continue;
30
            }
31
            $row = array_combine($headers, $row);
32
            yield $row;
33
        }
34
        return;
35
    }
36
}