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

Php::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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