Completed
Pull Request — master (#296)
by
unknown
03:13
created

CountableIteratorReader   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 2
c 2
b 0
f 2
lcom 0
cbo 1
dl 0
loc 16
rs 10
ccs 5
cts 5
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A count() 0 10 2
1
<?php
2
3
namespace Ddeboer\DataImport\Reader;
4
5
/**
6
 * Use a class implementing both \Iterator and \Countable as a reader
7
 *
8
 * This class uses count() on iterators implementing \Countable interface
9
 * and iterator_count in any further cases
10
 *
11
 * Be careful! iterator_count iterates through the whole iterator loading every data into the memory (for example from streams)
12
 * It is not recommended for very big datasets.
13
 *
14
 * @author Márk Sági-Kazár <[email protected]>
15
 */
16
class CountableIteratorReader extends IteratorReader implements CountableReader
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function count()
22
    {
23 2
        $iterator = $this->getInnerIterator();
24
25 2
        if ($iterator instanceof \Countable) {
26 1
            return count($iterator);
27
        }
28
29 1
        return iterator_count($iterator);
30
    }
31
}
32