Passed
Push — master ( 523cb4...302393 )
by Sam
02:03
created

ParseException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 42
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getLineNumber() 0 14 3
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