Completed
Push — master ( 7aa624...fe99b2 )
by Kevin
02:25
created

Php   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 6
c 4
b 1
f 3
lcom 1
cbo 2
dl 0
loc 59
ccs 27
cts 27
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B parse() 0 26 3
A getValue() 0 4 1
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
    /** @var string */
10
    private $value;
11
12 9
    public function __construct(Token $parent = null, $throwOnError = false)
13
    {
14 9
        parent::__construct(Token::PHP, $parent, $throwOnError);
15
16 9
        $this->value = null;
17 9
    }
18
19 7
    public function parse($html)
20
    {
21 7
        $html = ltrim($html);
22
23
        // Get token position.
24 7
        $positionArray = HtmlTokenizer::getPosition($html);
25 7
        $this->setLine($positionArray['line']);
26 7
        $this->setPosition($positionArray['position']);
27
28
        // Parse token.
29 7
        $startPos = 3;
30 7
        if (mb_substr($html, 0, 5) == '<?php') {
31 7
            $startPos = 6;
32 7
        }
33
34 7
        $posOfEndOfPhp = mb_strpos($html, '?>');
35 7
        if ($posOfEndOfPhp === false) {
36 1
            $this->value = trim(mb_substr($html, $startPos));
37
38 1
            return '';
39
        }
40
41 6
        $this->value = trim(mb_substr($html, $startPos, $posOfEndOfPhp - $startPos - 1));
42
43 6
        return mb_substr($html, $posOfEndOfPhp + 2);
44
    }
45
46
    /**
47
     * Getter for 'value'.
48
     *
49
     * @return string
50
     */
51 4
    public function getValue()
52
    {
53 4
        return $this->value;
54
    }
55
56 2
    public function toArray()
57
    {
58
        return array(
59 2
            'type' => 'php',
60 2
            'value' => $this->value,
61 2
            'line' => $this->getLine(),
62 2
            'position' => $this->getPosition()
63 2
        );
64
    }
65
}
66