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
|
|
|
const TYPE_EQUAL = '='; |
37
|
|
|
const TYPE_AT = '@'; |
38
|
|
|
|
39
|
|
|
const TYPE_NULL = 'null'; |
40
|
|
|
const TYPE_TRUE = 'true'; |
41
|
|
|
const TYPE_FALSE = 'false'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** @var mixed */ |
45
|
|
|
private $data; |
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
private $type; |
49
|
|
|
|
50
|
|
|
/** @var integer */ |
51
|
|
|
private $line; |
52
|
|
|
|
53
|
|
|
/** @var integer */ |
54
|
|
|
private $column; |
55
|
|
|
|
56
|
108 |
|
public function __construct($type, $line, $column, $data = null) |
57
|
|
|
{ |
58
|
108 |
|
$this->type = $type; |
59
|
108 |
|
$this->data = $data; |
60
|
|
|
|
61
|
108 |
|
$this->line = $line; |
62
|
108 |
|
$this->column = $column; |
63
|
|
|
|
64
|
108 |
|
if ($data) { |
65
|
106 |
|
$tokenLength = mb_strlen($data); |
66
|
106 |
|
$tokenLength = $tokenLength > 1 ? $tokenLength - 1 : 0; |
67
|
|
|
|
68
|
106 |
|
$this->column = $column - $tokenLength; |
|
|
|
|
69
|
106 |
|
} |
70
|
|
|
|
71
|
108 |
|
if ($this->getType() == self::TYPE_TRUE) { |
72
|
5 |
|
$this->data = true; |
73
|
5 |
|
} |
74
|
|
|
|
75
|
108 |
|
if ($this->getType() == self::TYPE_FALSE) { |
76
|
1 |
|
$this->data = false; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
108 |
|
if ($this->getType() == self::TYPE_NULL) { |
80
|
9 |
|
$this->data = null; |
81
|
9 |
|
} |
82
|
108 |
|
} |
83
|
|
|
|
84
|
9 |
|
public static function tokenName($tokenType) |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
9 |
|
self::TYPE_END => 'END', |
88
|
9 |
|
self::TYPE_IDENTIFIER => 'IDENTIFIER', |
89
|
9 |
|
self::TYPE_NUMBER => 'NUMBER', |
90
|
9 |
|
self::TYPE_STRING => 'STRING', |
91
|
9 |
|
self::TYPE_ON => 'ON', |
92
|
9 |
|
self::TYPE_QUERY => 'QUERY', |
93
|
9 |
|
self::TYPE_MUTATION => 'MUTATION', |
94
|
9 |
|
self::TYPE_FRAGMENT => 'FRAGMENT', |
95
|
9 |
|
self::TYPE_FRAGMENT_REFERENCE => 'FRAGMENT_REFERENCE', |
96
|
9 |
|
self::TYPE_TYPED_FRAGMENT => 'TYPED_FRAGMENT', |
97
|
9 |
|
self::TYPE_LBRACE => 'LBRACE', |
98
|
9 |
|
self::TYPE_RBRACE => 'RBRACE', |
99
|
9 |
|
self::TYPE_LPAREN => 'LPAREN', |
100
|
9 |
|
self::TYPE_RPAREN => 'RPAREN', |
101
|
9 |
|
self::TYPE_LSQUARE_BRACE => 'LSQUARE_BRACE', |
102
|
9 |
|
self::TYPE_RSQUARE_BRACE => 'RSQUARE_BRACE', |
103
|
9 |
|
self::TYPE_COLON => 'COLON', |
104
|
9 |
|
self::TYPE_COMMA => 'COMMA', |
105
|
9 |
|
self::TYPE_VARIABLE => 'VARIABLE', |
106
|
9 |
|
self::TYPE_POINT => 'POINT', |
107
|
9 |
|
self::TYPE_NULL => 'NULL', |
108
|
9 |
|
self::TYPE_TRUE => 'TRUE', |
109
|
9 |
|
self::TYPE_FALSE => 'FALSE', |
110
|
9 |
|
self::TYPE_REQUIRED => 'REQUIRED', |
111
|
9 |
|
self::TYPE_AT => 'AT', |
112
|
9 |
|
][$tokenType]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
98 |
|
public function getData() |
119
|
|
|
{ |
120
|
98 |
|
return $this->data; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
108 |
|
public function getType() |
127
|
|
|
{ |
128
|
108 |
|
return $this->type; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
102 |
|
public function getLine() |
135
|
|
|
{ |
136
|
102 |
|
return $this->line; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
102 |
|
public function getColumn() |
143
|
|
|
{ |
144
|
102 |
|
return $this->column; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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 theid
property of an instance of theAccount
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.