Completed
Push — master ( fd987e...4f1c16 )
by Kevin
02:24
created

Php::parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
class Php extends AbstractToken
6
{
7
    /** @var string */
8
    private $value;
9
10 8
    public function __construct(Token $parent = null, $throwOnError = false)
11
    {
12 8
        parent::__construct(Token::PHP, $parent, $throwOnError);
13
14 8
        $this->value = null;
15 8
    }
16
17 6
    public function parse($html)
18
    {
19 6
        $startPos = 3;
20 6
        if (mb_substr($html, 0, 5) == '<?php') {
21 6
            $startPos = 6;
22 6
        }
23
24 6
        $posOfEndOfPhp = mb_strpos($html, '?>');
25 6
        if ($posOfEndOfPhp === false) {
26 1
            $this->value = trim(mb_substr($html, $startPos));
27
28 1
            return '';
29
        }
30
31 5
        $this->value = trim(mb_substr($html, $startPos, $posOfEndOfPhp - $startPos - 1));
32
33 5
        return trim(mb_substr($html, $posOfEndOfPhp + 2));
34
    }
35
36
    /**
37
     * Getter for 'value'.
38
     *
39
     * @return string
40
     */
41 4
    public function getValue()
42
    {
43 4
        return $this->value;
44
    }
45
46 1
    public function toArray()
47
    {
48
        return array(
49 1
            'type' => 'php',
50 1
            'value' => $this->value
51 1
        );
52
    }
53
}
54