ParseException::getLineNumber()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Parser;
15
16
class ParseException extends \Exception
17
{
18
    /**
19
     * @var StringIterator
20
     */
21
    private $stringIterator;
22
23
    /**
24
     * ParseException constructor.
25
     */
26 7
    public function __construct(string $message = '', StringIterator $stringIterator = null, \Throwable $previous = null)
27
    {
28 7
        if (null !== $stringIterator) {
29 1
            $this->stringIterator = $stringIterator;
30 1
            $message .= sprintf(' [Line no: %d]', $this->getLineNumber());
31
        }
32
33 7
        parent::__construct($message, 0, $previous);
34 7
    }
35
36
    /**
37
     * Get line number of current entry on the StringIterator.
38
     */
39 1
    private function getLineNumber(): int
40
    {
41 1
        $pos = $this->stringIterator->key();
42 1
        $this->stringIterator->rewind();
43 1
        $lineNo = 1;
44
45 1
        while ($this->stringIterator->key() < $pos) {
46 1
            if ($this->stringIterator->is(Tokens::LINE_FEED)) {
47 1
                ++$lineNo;
48
            }
49 1
            $this->stringIterator->next();
50
        }
51
52 1
        return $lineNo;
53
    }
54
}
55