TokenTrait::token()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Output;
4
5
use Vanderlee\Comprehend\Core\Token;
6
7
/**
8
 * @author Martijn
9
 */
10
trait TokenTrait
11
{
12
    /**
13
     * Name of the token.
14
     *
15
     * @var null
16
     */
17
    private $tokenName = null;
18
19
    /**
20
     * Group to which this token belongs (mostly for standard Library tokens).
21
     *
22
     * @var string|null
23
     */
24
    private $tokenGroup = null;
25
26
    /**
27
     * Is this token the deepest node in this tree to report a token on?
28
     *
29
     * @var bool
30
     */
31
    private $tokenIsTerminal = false;
32
33
    /**
34
     * @param string $token
35
     * @param string|null $group
36
     * @param bool $isTerminal
37
     *
38
     * @return $this
39
     */
40 43
    public function token($token, $group = null, $isTerminal = false)
41
    {
42 43
        $this->tokenName = $token;
0 ignored issues
show
Documentation Bug introduced by
It seems like $token of type string is incompatible with the declared type null of property $tokenName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43 43
        $this->tokenGroup = $group;
44 43
        $this->tokenIsTerminal = $isTerminal;
45
46 43
        return $this;
47
    }
48
49
    /**
50
     * Has a token been set for this Parser?
51
     *
52
     * @return bool
53
     */
54 36
    public function hasToken()
55
    {
56 36
        return $this->tokenName !== null;
57
    }
58
59 8
    private function resolveToken($input, $offset, $length, &$children, $class)
60
    {
61 8
        if ($this->tokenIsTerminal) {
62 1
            $children = [];
63
        }
64
65 8
        return new Token($this->tokenGroup, $this->tokenName, $input, $offset, $length, $children, $class);
66
    }
67
}
68