LineIterator::current()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Clover to Html package.
5
 *
6
 * (c) Stéphane Demonchaux <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace CloverToHtml;
12
13
class LineIterator implements \Iterator
14
{
15
    /**
16
     * @var array
17
     */
18
    private $lines = array();
19
    /**
20
     * @var \SplFileObject
21
     */
22
    private $file;
23
    /**
24
     * @var integer
25
     */
26
    private $position;
27
28
    /**
29
     * Construct.
30
     *
31
     * @param string $filePath
32
     * @param array  $lines
33
     */
34 1
    public function __construct($filePath, array $lines)
35
    {
36 1
        $this->file  = new \SplFileObject($filePath);
37 1
        $this->lines = $lines;
38 1
    }
39
40
    /* (non-PHPdoc)
41
     * @see Iterator::rewind()
42
     */
43
    public function rewind(): void
44
    {
45
        $this->file->rewind();
46
        $this->position = 0;
47
    }
48
49
    /* (non-PHPdoc)
50
     * @see Iterator::current()
51
     */
52
    public function current()
53
    {
54
        $type      = 'unkown';
55
        $isCovered = false;
56
57
        if (isset($this->lines[$this->position])) {
58
            $type      = $this->lines[$this->position]['type'];
59
            $isCovered = $this->lines[$this->position]['isCovered'];
60
        }
61
62
        return array('type' => $type, 'isCovered' => $isCovered, 'content' => $this->file->fgets());
63
    }
64
65
    /* (non-PHPdoc)
66
     * @see Iterator::key()
67
     */
68
    public function key()
69
    {
70
        return $this->position;
71
    }
72
73
    /* (non-PHPdoc)
74
     * @see Iterator::next()
75
     */
76
    public function next(): void
77
    {
78
        ++$this->position;
79
    }
80
81
    /* (non-PHPdoc)
82
     * @see Iterator::valid()
83
     */
84
    public function valid(): bool
85
    {
86
        return !$this->file->eof();
87
    }
88
89
    /**
90
     * @return number
91
     */
92 1
    public function count()
93
    {
94 1
        $file = new \SplFileObject($this->file->getPathname());
95
96 1
        $file->seek(PHP_INT_MAX);
97 1
        return $file->key() + 1;
98
    }
99
}
100