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

Php   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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