Completed
Push — master ( 9e4cdb...1641ea )
by Alexandr
02:44
created

Token   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 134
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 6
B tokenName() 0 29 1
A getData() 0 4 1
A getType() 0 4 1
A getLine() 0 4 1
A getColumn() 0 4 1
1
<?php
2
/**
3
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Parser;
9
10
class Token
11
{
12
13
    const TYPE_END        = 'end';
14
    const TYPE_IDENTIFIER = 'identifier';
15
    const TYPE_NUMBER     = 'number';
16
    const TYPE_STRING     = 'string';
17
    const TYPE_ON         = 'on';
18
19
    const TYPE_QUERY              = 'query';
20
    const TYPE_MUTATION           = 'mutation';
21
    const TYPE_FRAGMENT           = 'fragment';
22
    const TYPE_FRAGMENT_REFERENCE = '...';
23
    const TYPE_TYPED_FRAGMENT     = 'typed fragment';
24
25
    const TYPE_LBRACE        = '{';
26
    const TYPE_RBRACE        = '}';
27
    const TYPE_LPAREN        = '(';
28
    const TYPE_RPAREN        = ')';
29
    const TYPE_LSQUARE_BRACE = '[';
30
    const TYPE_RSQUARE_BRACE = ']';
31
    const TYPE_COLON         = ':';
32
    const TYPE_COMMA         = ',';
33
    const TYPE_VARIABLE      = '$';
34
    const TYPE_POINT         = '.';
35
    const TYPE_REQUIRED      = '!';
36
37
    const TYPE_NULL  = 'null';
38
    const TYPE_TRUE  = 'true';
39
    const TYPE_FALSE = 'false';
40
41
42
    /** @var mixed */
43
    private $data;
44
45
    /** @var  string */
46
    private $type;
47
48
    /** @var integer */
49
    private $line;
50
51
    /** @var integer */
52
    private $column;
53
54 78
    public function __construct($type, $line, $column, $data = null)
55
    {
56 78
        $this->type = $type;
57 78
        $this->data = $data;
58
59 78
        $this->line   = $line;
60 78
        $this->column = $column;
61
62 78
        if ($data) {
63 76
            $tokenLength = mb_strlen($data);
64 76
            $tokenLength = $tokenLength > 1 ? $tokenLength - 1 : 0;
65
66 76
            $this->column = $column - $tokenLength;
0 ignored issues
show
Documentation Bug introduced by
It seems like $column - $tokenLength can also be of type double. However, the property $column is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
67 76
        }
68
69 78
        if ($this->getType() == self::TYPE_TRUE) {
70 5
            $this->data = true;
71 5
        }
72
73 78
        if ($this->getType() == self::TYPE_FALSE) {
74 1
            $this->data = false;
75 1
        }
76
77 78
        if ($this->getType() == self::TYPE_NULL) {
78 4
            $this->data = null;
79 4
        }
80 78
    }
81
82 12
    public static function tokenName($tokenType)
83
    {
84
        return [
85 12
            self::TYPE_END                => 'END',
86 12
            self::TYPE_IDENTIFIER         => 'IDENTIFIER',
87 12
            self::TYPE_NUMBER             => 'NUMBER',
88 12
            self::TYPE_STRING             => 'STRING',
89 12
            self::TYPE_ON                 => 'ON',
90 12
            self::TYPE_QUERY              => 'QUERY',
91 12
            self::TYPE_MUTATION           => 'MUTATION',
92 12
            self::TYPE_FRAGMENT           => 'FRAGMENT',
93 12
            self::TYPE_FRAGMENT_REFERENCE => 'FRAGMENT_REFERENCE',
94 12
            self::TYPE_TYPED_FRAGMENT     => 'TYPED_FRAGMENT',
95 12
            self::TYPE_LBRACE             => 'LBRACE',
96 12
            self::TYPE_RBRACE             => 'RBRACE',
97 12
            self::TYPE_LPAREN             => 'LPAREN',
98 12
            self::TYPE_RPAREN             => 'RPAREN',
99 12
            self::TYPE_LSQUARE_BRACE      => 'LSQUARE_BRACE',
100 12
            self::TYPE_RSQUARE_BRACE      => 'RSQUARE_BRACE',
101 12
            self::TYPE_COLON              => 'COLON',
102 12
            self::TYPE_COMMA              => 'COMMA',
103 12
            self::TYPE_VARIABLE           => 'VARIABLE',
104 12
            self::TYPE_POINT              => 'POINT',
105 12
            self::TYPE_NULL               => 'NULL',
106 12
            self::TYPE_TRUE               => 'TRUE',
107 12
            self::TYPE_FALSE              => 'FALSE',
108 12
            self::TYPE_REQUIRED           => 'REQUIRED',
109 12
        ][$tokenType];
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115 68
    public function getData()
116
    {
117 68
        return $this->data;
118
    }
119
120
    /**
121
     * @return string
122
     */
123 78
    public function getType()
124
    {
125 78
        return $this->type;
126
    }
127
128
    /**
129
     * @return int
130
     */
131 73
    public function getLine()
132
    {
133 73
        return $this->line;
134
    }
135
136
    /**
137
     * @return int
138
     */
139 73
    public function getColumn()
140
    {
141 73
        return $this->column;
142
    }
143
}
144