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

ParseException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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
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