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

AbstractToken   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 66
ccs 0
cts 37
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getDepth() 0 4 1
A getParent() 0 4 1
A setParent() 0 11 2
A getType() 0 4 1
B isValidType() 0 8 5
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