Completed
Push — master ( 15134a...d6e1fc )
by Kevin
02:45
created

Comment   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 41
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 23 3
A toArray() 0 9 1
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
use Kevintweber\HtmlTokenizer\HtmlTokenizer;
6
use Kevintweber\HtmlTokenizer\Exceptions\ParseException;
7
8
class Comment extends AbstractToken
9
{
10 16
    public function __construct(Token $parent = null, bool $throwOnError = true)
11
    {
12 16
        parent::__construct(Token::COMMENT, $parent, $throwOnError);
13 16
    }
14
15 13
    public function parse(string $html) : string
16
    {
17 13
        $html = ltrim($html);
18
19
        // Get token position.
20 13
        $positionArray = HtmlTokenizer::getPosition($html);
21 13
        $this->line = $positionArray['line'];
22 13
        $this->position = $positionArray['position'];
23
24
        // Parse token.
25 13
        $posOfEndOfComment = mb_strpos($html, '-->');
26 13
        if ($posOfEndOfComment === false) {
27 2
            if ($this->getThrowOnError()) {
28 1
                throw new ParseException('Invalid comment.');
29
            }
30
31 1
            return '';
32
        }
33
34 11
        $this->value = trim(mb_substr($html, 4, $posOfEndOfComment - 4));
35
36 11
        return mb_substr($html, $posOfEndOfComment + 3);
37
    }
38
39 6
    public function toArray() : array
40
    {
41
        return array(
42 6
            'type' => 'comment',
43 6
            'value' => $this->value,
44 6
            'line' => $this->getLine(),
45 6
            'position' => $this->getPosition()
46
        );
47
    }
48
}
49