Completed
Push — master ( 68f73c...f03bb1 )
by Kevin
03:04
created

Comment   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 41
ccs 0
cts 29
cp 0
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
    public function __construct(Token $parent = null, bool $throwOnError = true)
11
    {
12
        parent::__construct(Token::COMMENT, $parent, $throwOnError);
13
    }
14
15
    public function parse(string $html) : string
16
    {
17
        $html = ltrim($html);
18
19
        // Get token position.
20
        $positionArray = HtmlTokenizer::getPosition($html);
21
        $this->line = $positionArray['line'];
22
        $this->position = $positionArray['position'];
23
24
        // Parse token.
25
        $posOfEndOfComment = mb_strpos($html, '-->');
26
        if ($posOfEndOfComment === false) {
27
            if ($this->getThrowOnError()) {
28
                throw new ParseException('Invalid comment.');
29
            }
30
31
            return '';
32
        }
33
34
        $this->value = trim(mb_substr($html, 4, $posOfEndOfComment - 4));
35
36
        return mb_substr($html, $posOfEndOfComment + 3);
37
    }
38
39
    public function toArray() : array
40
    {
41
        return array(
42
            'type' => 'comment',
43
            'value' => $this->value,
44
            'line' => $this->getLine(),
45
            'position' => $this->getPosition()
46
        );
47
    }
48
}
49