Completed
Push — master ( 62c01b...6e9edd )
by Kevin
02:26
created

AbstractToken::toHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
7
abstract class AbstractToken implements Token
8
{
9
    /** @var Configuration */
10
    protected $configuration;
11
12
    /** @var int */
13
    private $depth;
14
15
    /** @var null|Token */
16
    private $parent;
17
18
    /** @var string */
19
    private $type;
20
21
    /**
22
     * Constructor
23
     */
24 49
    public function __construct($type, Configuration $configuration, Token $parent = null)
25
    {
26 49
        if (!$this->isValidType($type)) {
27 1
            throw new \InvalidArgumentException('Invalid type: ' . $type);
28
        }
29
30 48
        $this->configuration = $configuration;
31 48
        $this->setParent($parent);
32 48
        $this->type = $type;
33 48
    }
34
35
    /**
36
     * Required by the Token interface.
37
     */
38 22
    public function getDepth()
39
    {
40 22
        return $this->depth;
41
    }
42
43
    /**
44
     * Required by the Token interface.
45
     */
46 3
    public function getParent()
47
    {
48 3
        return $this->parent;
49
    }
50
51
    /**
52
     * Chainable setter for 'parent'.
53
     */
54 48
    public function setParent(Token $parent = null)
55
    {
56 48
        $this->depth = 0;
57 48
        if ($parent instanceof Token) {
58 21
            $this->depth = $parent->getDepth() + 1;
59 21
        }
60
61 48
        $this->parent = $parent;
62
63 48
        return $this;
64
    }
65
66
    /**
67
     * Required by the Token interface.
68
     */
69 15
    public function getType()
70
    {
71 15
        return $this->type;
72
    }
73
74 49
    protected function isValidType($type)
75
    {
76
        return $type === Token::CDATA
77 49
            || $type === Token::COMMENT
78 45
            || $type === Token::DOCTYPE
79 40
            || $type === Token::ELEMENT
80 49
            || $type === Token::TEXT;
81
    }
82
}
83