ParseException   A
last analyzed

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
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[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
12
namespace Badcow\DNS\Parser;
13
14
class ParseException extends \Exception
15
{
16
    /**
17
     * @var StringIterator
18
     */
19
    private $stringIterator;
20
21
    /**
22
     * ParseException constructor.
23
     *
24
     * @param string              $message
25
     * @param StringIterator|null $stringIterator
26
     */
27 5
    public function __construct(string $message = '', StringIterator $stringIterator = null)
28
    {
29 5
        if (null !== $stringIterator) {
30 1
            $this->stringIterator = $stringIterator;
31 1
            $message .= sprintf(' [Line no: %d]', $this->getLineNumber());
32
        }
33
34 5
        parent::__construct($message);
35 5
    }
36
37
    /**
38
     * Get line number of current entry on the StringIterator.
39
     *
40
     * @return int
41
     */
42 1
    private function getLineNumber(): int
43
    {
44 1
        $pos = $this->stringIterator->key();
45 1
        $this->stringIterator->rewind();
46 1
        $lineNo = 1;
47
48 1
        while ($this->stringIterator->key() < $pos) {
49 1
            if ($this->stringIterator->is(Tokens::LINE_FEED)) {
50 1
                ++$lineNo;
51
            }
52 1
            $this->stringIterator->next();
53
        }
54
55 1
        return $lineNo;
56
    }
57
}
58