Passed
Push — master ( 750782...7d3f93 )
by Sam
01:54
created

ParseException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Badcow\DNS\Parser;
4
5
class ParseException extends \Exception
6
{
7
    /**
8
     * @var StringIterator
9
     */
10
    private $stringIterator;
11
12
    /**
13
     * ParseException constructor.
14
     *
15
     * @param string              $message
16
     * @param StringIterator|null $stringIterator
17
     */
18 5
    public function __construct(string $message = '', StringIterator $stringIterator = null)
19
    {
20 5
        if (null !== $stringIterator) {
21 1
            $this->stringIterator = $stringIterator;
22 1
            $message .= sprintf(' [Line no: %d]', $this->getLineNumber());
23
        }
24
25 5
        parent::__construct($message);
26 5
    }
27
28
    /**
29
     * Get line number of current entry on the StringIterator.
30
     *
31
     * @return int
32
     */
33 1
    private function getLineNumber(): int
34
    {
35 1
        $pos = $this->stringIterator->key();
36 1
        $this->stringIterator->rewind();
37 1
        $lineNo = 1;
38
39 1
        while ($this->stringIterator->key() < $pos) {
40 1
            if ($this->stringIterator->is(Tokens::LINE_FEED)) {
41 1
                ++$lineNo;
42
            }
43 1
            $this->stringIterator->next();
44
        }
45
46 1
        return $lineNo;
47
    }
48
}
49