Completed
Push — master ( b66e97...9d450b )
by Kevin
02:16
created

AbstractToken::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.0116
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
7
abstract class AbstractToken implements Token
8
{
9
    /** @var int */
10
    private $depth;
11
12
    /** @var int */
13
    protected $isValid;
14
15
    /** @var null|Token */
16
    private $parent;
17
18
    /** @var string */
19
    private $type;
20
21
    /**
22
     * Constructor
23
     */
24 11
    public function __construct($type, Token $parent = null)
25
    {
26 11
        if (!$this->isValidType($type)) {
27
            throw new \InvalidArgumentException('Invalid type: ' . $type);
28
        }
29
30 11
        $this->isValid = false;
0 ignored issues
show
Documentation Bug introduced by
The property $isValid was declared of type integer, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
31 11
        $this->setParent($parent);
32 11
        $this->type = $type;
33 11
    }
34
35
    public function getDepth()
36
    {
37
        return $this->depth;
38
    }
39
40
    /**
41
     * Getter for 'isValid'.
42
     */
43
    public function getIsValid()
44
    {
45
        return $this->isValid;
46
    }
47
48
    /**
49
     * Getter for 'parent'.
50
     */
51 2
    public function getParent()
52
    {
53 2
        return $this->parent;
54
    }
55
56
    /**
57
     * Chainable setter for 'parent'.
58
     */
59 11
    public function setParent(Token $parent = null)
60
    {
61 11
        $this->depth = 0;
62 11
        if ($parent instanceof Token) {
63
            $this->depth = $parent->getDepth() + 1;
64
        }
65
66 11
        $this->parent = $parent;
67
68 11
        return $this;
69
    }
70
71
    public function getType()
72
    {
73
        return $this->type;
74
    }
75
76 10
    public function validate(Configuration $configuration)
77
    {
78 10
        $this->isValid = $configuration->isAllowedType($this->type);
0 ignored issues
show
Documentation Bug introduced by
The property $isValid was declared of type integer, but $configuration->isAllowedType($this->type) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
79 10
    }
80
81 11
    protected function isValidType($type)
82
    {
83
        return $type === Token::CDATA
84 11
            || $type === Token::COMMENT
85 9
            || $type === Token::DOCTYPE
86 7
            || $type === Token::ELEMENT
87 11
            || $type === Token::TEXT;
88
    }
89
}
90