Completed
Push — master ( 15a7c5...a9adfd )
by Kevin
02:15
created

AbstractToken::isValidType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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