Skip to content

Commit 4b78eb0

Browse files
author
Nico
committed
Refactor
1 parent 0a2da61 commit 4b78eb0

File tree

5 files changed

+17
-80
lines changed

5 files changed

+17
-80
lines changed

src/nicoSWD/Rules/AST/BaseNode.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,22 @@ protected function getMethodName()
107107

108108
/**
109109
* @since 0.3.4
110+
* @param string $stopAt
110111
* @return mixed[]
111112
* @throws ParserException
112113
*/
113-
protected function getFunctionArgs()
114+
protected function getCommaSeparatedValues($stopAt = ')')
114115
{
115116
$commaExpected = \false;
116-
$arguments = [];
117+
$items = [];
117118

118119
do {
119120
$this->ast->next();
120121

121122
if (!$current = $this->ast->current()) {
122123
throw new ParserException(sprintf(
123-
'Unexpected end of string. Expected ")"'
124+
'Unexpected end of string. Expected "%s"',
125+
$stopAt
124126
));
125127
}
126128

@@ -136,7 +138,7 @@ protected function getFunctionArgs()
136138
}
137139

138140
$commaExpected = \true;
139-
$arguments[] = $value;
141+
$items[] = $value;
140142
} elseif ($current instanceof Tokens\TokenComma) {
141143
if (!$commaExpected) {
142144
throw new ParserException(sprintf(
@@ -147,7 +149,7 @@ protected function getFunctionArgs()
147149
}
148150

149151
$commaExpected = \false;
150-
} elseif ($value === ')') {
152+
} elseif ($value === $stopAt) {
151153
break;
152154
} elseif (!$this->isIgnoredToken($current)) {
153155
throw new ParserException(sprintf(
@@ -159,15 +161,15 @@ protected function getFunctionArgs()
159161
}
160162
} while ($this->ast->valid());
161163

162-
if (!$commaExpected && !empty($arguments)) {
164+
if (!$commaExpected && !empty($items)) {
163165
throw new ParserException(sprintf(
164166
'Unexpected token "," at position %d on line %d',
165167
$current->getPosition(),
166168
$current->getLine()
167169
));
168170
}
169171

170-
return $arguments;
172+
return $items;
171173
}
172174

173175
/**

src/nicoSWD/Rules/AST/NodeArray.php

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
*/
99
namespace nicoSWD\Rules\AST;
1010

11-
use nicoSWD\Rules\Constants;
12-
use nicoSWD\Rules\Exceptions\ParserException;
13-
use nicoSWD\Rules\Tokens\BaseToken;
1411
use nicoSWD\Rules\Tokens\TokenArray;
15-
use nicoSWD\Rules\Tokens\TokenComma;
1612

1713
/**
1814
* Class NodeArray
@@ -21,72 +17,17 @@
2117
final class NodeArray extends BaseNode
2218
{
2319
/**
24-
* @return BaseToken
25-
* @throws ParserException
20+
* @return \nicoSWD\Rules\Tokens\BaseToken
21+
* @throws \nicoSWD\Rules\Exceptions\ParserException
2622
*/
2723
public function getNode()
2824
{
2925
$stack = $this->ast->getStack();
3026
$offset = $stack->current()->getOffset();
31-
$commaExpected = \false;
32-
$items = [];
33-
34-
do {
35-
$stack->next();
36-
37-
if (!$current = $stack->current()) {
38-
throw new ParserException(sprintf(
39-
'Unexpected end of string. Expected "]"'
40-
));
41-
}
42-
43-
$value = $current->getValue();
44-
45-
if ($current->getGroup() === Constants::GROUP_VALUE) {
46-
if ($commaExpected) {
47-
throw new ParserException(sprintf(
48-
'Unexpected value at position %d on line %d',
49-
$current->getPosition(),
50-
$current->getLine()
51-
));
52-
}
53-
54-
$commaExpected = \true;
55-
$items[] = $value;
56-
} elseif ($current instanceof TokenComma) {
57-
if (!$commaExpected) {
58-
throw new ParserException(sprintf(
59-
'Unexpected token "," at position %d on line %d',
60-
$current->getPosition(),
61-
$current->getLine()
62-
));
63-
}
64-
65-
$commaExpected = \false;
66-
} elseif ($value === ']') {
67-
break;
68-
} elseif (!$this->isIgnoredToken($current)) {
69-
throw new ParserException(sprintf(
70-
'Unexpected token "%s" at position %d on line %d',
71-
$current->getOriginalValue(),
72-
$current->getPosition(),
73-
$current->getLine()
74-
));
75-
}
76-
} while ($stack->valid());
77-
78-
if (!$commaExpected && !empty($items)) {
79-
throw new ParserException(sprintf(
80-
'Unexpected token "," at position %d on line %d',
81-
$current->getPosition(),
82-
$current->getLine()
83-
));
84-
}
85-
86-
$token = new TokenArray($items, $offset, $stack);
27+
$token = new TokenArray($this->getCommaSeparatedValues(']'), $offset, $stack);
8728

8829
while ($this->hasMethodCall()) {
89-
$token = $this->getMethod($token)->call($this->getFunctionArgs());
30+
$token = $this->getMethod($token)->call($this->getCommaSeparatedValues());
9031
}
9132

9233
return $token;

src/nicoSWD/Rules/AST/NodeString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getNode()
2222
$current = $this->ast->getStack()->current();
2323

2424
while ($current->supportsMethodCalls() && $this->hasMethodCall()) {
25-
$current = $this->getMethod($current)->call($this->getFunctionArgs());
25+
$current = $this->getMethod($current)->call($this->getCommaSeparatedValues());
2626
}
2727

2828
return $current;

src/nicoSWD/Rules/AST/NodeVariable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getNode()
4949
$current->setStack($this->ast->getStack());
5050

5151
while ($this->hasMethodCall()) {
52-
$current = $this->getMethod($current)->call($this->getFunctionArgs());
52+
$current = $this->getMethod($current)->call($this->getCommaSeparatedValues());
5353
}
5454

5555
return $current;

tests/methods/JoinTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,8 @@ public function testCallOnStringLiteralArray()
3939
$this->assertTrue($this->evaluate('[1, 2, 3] . join("|") === "1|2|3"'));
4040
}
4141

42-
/**
43-
* Not currently supported.
44-
*
45-
* @expectedException \Exception
46-
* @expectedExceptionMessage Unexpected token "foo" at position 7 on line 1
47-
*/
48-
public function testIfVariableInsideArrayThrowsException()
42+
public function testVariableInArrayIsJoined()
4943
{
50-
$this->evaluate('[1, 2, foo].join("|") === "1|2|3"');
44+
$this->assertTrue($this->evaluate('[1, 2, foo].join("|") === "1|2|3"', ['foo' => 3]));
5145
}
5246
}

0 commit comments

Comments
 (0)