Passed
Push — master ( 523cb4...302393 )
by Sam
02:03
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
use LTDBeget\ascii\AsciiChar;
6
7
class ParseException extends \Exception
8
{
9
    /**
10
     * @var StringIterator
11
     */
12
    private $stringIterator;
13
14
    /**
15
     * ParseException constructor.
16
     *
17
     * @param string              $message
18
     * @param StringIterator|null $stringIterator
19
     */
20 3
    public function __construct(string $message = '', StringIterator $stringIterator = null)
21
    {
22 3
        if (null !== $stringIterator) {
23 1
            $this->stringIterator = $stringIterator;
24 1
            $message .= sprintf(' [Line no: %d]', $this->getLineNumber());
25
        }
26
27 3
        parent::__construct($message);
28 3
    }
29
30
    /**
31
     * Get line number of current entry on the StringIterator.
32
     *
33
     * @return int
34
     */
35 1
    private function getLineNumber(): int
36
    {
37 1
        $pos = $this->stringIterator->key();
38 1
        $this->stringIterator->rewind();
39 1
        $lineNo = 1;
40
41 1
        while ($this->stringIterator->key() < $pos) {
42 1
            if (AsciiChar::LINE_FEED === $this->stringIterator->ord()) {
43
                ++$lineNo;
44
            }
45 1
            $this->stringIterator->next();
46
        }
47
48 1
        return $lineNo;
49
    }
50
}
51