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

Php   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 44
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

3 Methods

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