Skip to content

Commit 181b45b

Browse files
committed
fixed context resolver issues
1 parent 091ce14 commit 181b45b

3 files changed

Lines changed: 98 additions & 31 deletions

File tree

specs/completer/resolver/ContextResolver.spec.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
$context = $this->resolver->getContext('$var->');
7272
expect($context->isObject())->to->be->true;
7373
});
74+
it('has type object after object operator and space', function() {
75+
$context = $this->resolver->getContext('$var-> ');
76+
expect($context->isObject())->to->be->true;
77+
});
7478
it('has type object after object operator and $this', function() {
7579
$context = $this->resolver->getContext('$this->');
7680
expect($context->isObject())->to->be->true;
@@ -103,5 +107,23 @@
103107
expect($context->isMethodCall())->to->be->true;
104108
});
105109
});
110+
describe("Variable", function() {
111+
it('has type variable after $', function() {
112+
$context = $this->resolver->getContext('$');
113+
expect($context->isVar())->to->be->true;
114+
});
115+
it('has type variable after $ with TString', function() {
116+
$context = $this->resolver->getContext('$var');
117+
expect($context->isVar())->to->be->true;
118+
});
119+
it('hasn\'t type variable after space symbol', function() {
120+
$context = $this->resolver->getContext('$var ');
121+
expect($context->isVar())->to->be->false;
122+
});
123+
it('hasn\'t type variable after - symbol', function() {
124+
$context = $this->resolver->getContext('$var-');
125+
expect($context->isVar())->to->be->false;
126+
});
127+
});
106128
});
107129
});

src/Entity/Completion/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Entity\Completion;
44

55
class Context {
6-
const T_VAR = 1;
76
const T_USE = 2;
87
const T_NAMESPACE = 4;
98
const T_OBJECT = 8;
@@ -13,6 +12,7 @@ class Context {
1312
const T_CLASS_STATIC = 128;
1413
const T_CLASS_METHODS = 256;
1514
const T_METHOD_CALL = 512;
15+
const T_VAR = 1024;
1616

1717
private $type = 0;
1818
private $token;

src/Entity/Completion/Token.php

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,53 @@
22

33
namespace Entity\Completion;
44

5-
class Token {
6-
private $symbol = "";
7-
private $type = 0;
8-
9-
public function __construct($code, $symbol) {
5+
class Token
6+
{
7+
public function __construct($code, $symbol)
8+
{
109
$this->add($code, $symbol);
1110
}
1211

13-
public function add($code, $symbol) {
12+
public function add($code, $symbol)
13+
{
1414
switch ($code) {
1515
case T_WHITESPACE:
1616
$this->addType(self::T_WHITESPACE);
1717
case T_NS_SEPARATOR:
18+
$this->addType(self::T_CONTINUE_PROCESS);
19+
break;
1820
case T_STRING:
21+
$this->addType(self::T_STRING);
1922
$this->addType(self::T_CONTINUE_PROCESS);
2023
break;
2124
case T_VARIABLE:
2225
case T_DOUBLE_COLON:
23-
case T_OBJECT_OPERATOR:
2426
if ($this->isWhitespace()) {
25-
$this->addType(self::T_UNKNOWN);
27+
$this->resetType(self::T_UNKNOWN);
28+
break;
29+
}
30+
case T_OBJECT_OPERATOR:
31+
if ($this->hasString() && $this->hasWhitespace()) {
32+
$this->resetType(self::T_UNKNOWN);
33+
break;
2634
}
2735
case T_NAMESPACE:
2836
case T_USE:
2937
case T_NEW:
3038
case T_EXTENDS:
3139
case T_IMPLEMENTS:
32-
$this->removeType(self::T_CONTINUE_PROCESS);
33-
$this->addType(self::$MAP[$code]);
34-
break;
40+
case '$':
3541
case '(':
36-
$this->addType(self::T_METHOD_CALL);
42+
$this->resetType(self::$MAP[$code]);
3743
break;
3844
case ';':
3945
case ',':
46+
case '-':
47+
case ':':
4048
case '=':
4149
case ')':
4250
case ']':
43-
$this->addType(self::T_TERMINATE);
51+
$this->resetType(self::T_TERMINATE);
4452
break;
4553
default:
4654
$this->addType(self::T_UNKNOWN);
@@ -50,81 +58,111 @@ public function add($code, $symbol) {
5058
}
5159
}
5260

53-
public function getSymbol() {
61+
public function getSymbol()
62+
{
5463
return $this->symbol;
5564
}
5665

57-
public function getType() {
66+
public function getType()
67+
{
5868
return $this->type;
5969
}
6070

61-
public function isUnknown() {
71+
public function isUnknown()
72+
{
6273
return (bool) ($this->type & self::T_UNKNOWN);
6374
}
6475

65-
public function isReady() {
76+
public function isReady()
77+
{
6678
return !((bool) ($this->type & self::T_CONTINUE_PROCESS));
6779
}
6880

69-
public function isTerminate() {
81+
public function isTerminate()
82+
{
7083
return (bool) ($this->type & self::T_TERMINATE);
7184
}
7285

73-
public function isObjectOperator() {
86+
public function isObjectOperator()
87+
{
7488
return (bool) ($this->type & self::T_OBJECT_OPERATOR);
7589
}
7690

77-
public function isStaticOperator() {
91+
public function isStaticOperator()
92+
{
7893
return (bool) ($this->type & self::T_STATIC_OPERATOR);
7994
}
8095

81-
public function isUseOperator() {
96+
public function isUseOperator()
97+
{
8298
return (bool) ($this->type & self::T_USE_OPERATOR);
8399
}
84100

85-
public function isNamespaceOperator() {
101+
public function isNamespaceOperator()
102+
{
86103
return (bool) ($this->type & self::T_NAMESPACE_OPERATOR);
87104
}
88105

89-
public function isExtendsOperator() {
106+
public function isExtendsOperator()
107+
{
90108
return (bool) ($this->type & self::T_EXTENDS_OPERATOR);
91109
}
92110

93-
public function isImplementsOperator() {
111+
public function isImplementsOperator()
112+
{
94113
return (bool) ($this->type & self::T_IMPLEMENTS_OPERATOR);
95114
}
96115

97-
public function isNewOperator() {
116+
public function isNewOperator()
117+
{
98118
return (bool) ($this->type & self::T_NEW_OPERATOR);
99119
}
100120

101-
public function isVar() {
121+
public function isVar()
122+
{
102123
return (bool) ($this->type & self::T_VAR);
103124
}
104125

105-
public function isWhitespace() {
126+
public function isWhitespace()
127+
{
106128
return (bool) ($this->type & self::T_WHITESPACE);
107129
}
108130

131+
public function hasWhitespace()
132+
{
133+
return $this->isWhitespace();
134+
}
135+
136+
public function hasString()
137+
{
138+
return (bool) ($this->type & self::T_STRING);
139+
}
140+
109141
public function isMethodCall()
110142
{
111143
return (bool) ($this->type & self::T_METHOD_CALL);
112144
}
113145

114-
protected function addType($type){
146+
protected function resetType($type = 0)
147+
{
148+
$this->type = $type;
149+
}
150+
protected function addType($type)
151+
{
115152
$this->type |= $type;
116153
}
117154

118155
/**
119156
* @param integer $type
120157
*/
121-
protected function removeType($type){
158+
protected function removeType($type)
159+
{
122160
if((bool) ($this->type & $type)){
123161
$this->type ^= $type;
124162
}
125163
}
126164

127-
const T_UNKNOWN = -1;
165+
const T_UNKNOWN = 0;
128166
const T_CONTINUE_PROCESS = 1;
129167
const T_TERMINATE = 2;
130168
const T_OBJECT_OPERATOR = 4;
@@ -137,6 +175,7 @@ protected function removeType($type){
137175
const T_VAR = 512;
138176
const T_WHITESPACE = 1024;
139177
const T_METHOD_CALL = 2048;
178+
const T_STRING = 4096;
140179

141180
protected static $MAP = [
142181
T_VARIABLE => Token::T_VAR,
@@ -147,5 +186,11 @@ protected function removeType($type){
147186
T_NEW => Token::T_NEW_OPERATOR,
148187
T_EXTENDS => Token::T_EXTENDS_OPERATOR,
149188
T_IMPLEMENTS => Token::T_IMPLEMENTS_OPERATOR,
189+
'$' => Token::T_VAR,
190+
'(' => Token::T_METHOD_CALL
150191
];
192+
193+
private $symbol = "";
194+
private $type = 0;
195+
151196
}

0 commit comments

Comments
 (0)