Skip to content

Commit 6a54bb0

Browse files
committed
Handle class instances creations
1 parent 04f1f09 commit 6a54bb0

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

examples/new.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new Date();

examples/new.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new Date();

src/JsPhpize/Compiler/Compiler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
189189
{
190190
$function = $functionCall->function;
191191
$arguments = $functionCall->arguments;
192+
$applicant = $functionCall->applicant;
192193
$arguments = $this->visitNodesArray($arguments, $indent, ', ');
193194
$dynamicCall = 'call_user_func(' .
194195
$this->visitNode($function, $indent) .
@@ -199,6 +200,10 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
199200
$name = $function->name;
200201
$staticCall = $name . '(' . $arguments . ')';
201202

203+
if ($applicant === 'new') {
204+
return $staticCall;
205+
}
206+
202207
if (in_array($name, array(
203208
'array',
204209
'echo',

src/JsPhpize/Nodes/FunctionCall.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ class FunctionCall extends Value
1414
*/
1515
protected $arguments;
1616

17-
public function __construct(Value $function, array $arguments)
17+
/**
18+
* @var null|string
19+
*/
20+
protected $applicant;
21+
22+
public function __construct(Value $function, array $arguments, $applicant = null)
1823
{
1924
$this->function = $function;
2025
$this->arguments = $arguments;
26+
$this->applicant = $applicant;
2127
}
2228
}

src/JsPhpize/Parser/Parser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,12 @@ protected function parseKeywordStatement($token)
247247
$name = $token->value;
248248
$keyword = new Block($name);
249249
switch ($name) {
250+
case 'new':
251+
case 'clone':
250252
case 'return':
251253
case 'continue':
252254
case 'break':
253-
$this->handleOptionalValue($keyword, $this->get(0));
255+
$this->handleOptionalValue($keyword, $this->get(0), $name);
254256
break;
255257
case 'case':
256258
$value = $this->expectValue($this->next());

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,20 @@ protected function getInstructionFromToken($token)
7878
}
7979
}
8080

81-
protected function getValueFromToken($token)
81+
protected function getValueFromToken($token, $previousToken = null, $applicant = null)
8282
{
8383
$value = $this->getInitialValue($token);
8484
if ($value) {
85-
$this->appendFunctionsCalls($value);
85+
$this->appendFunctionsCalls($value, $previousToken, $applicant);
8686
}
8787

8888
return $value;
8989
}
9090

91-
protected function handleOptionalValue($keyword, $afterKeyword)
91+
protected function handleOptionalValue($keyword, $afterKeyword, $applicant)
9292
{
9393
if (!$afterKeyword->is(';')) {
94-
$value = $this->expectValue($this->next());
94+
$value = $this->expectValue($this->next(), $keyword, $applicant);
9595
$keyword->setValue($value);
9696
}
9797
}
@@ -131,7 +131,7 @@ protected function getInitialValue($token)
131131
}
132132
}
133133

134-
protected function appendFunctionsCalls(&$value)
134+
protected function appendFunctionsCalls(&$value, $previousToken = null, $applicant = null)
135135
{
136136
while ($token = $this->get(0)) {
137137
if ($token->is('{') || $token->expectNoLeftMember()) {
@@ -145,8 +145,7 @@ protected function appendFunctionsCalls(&$value)
145145
}
146146
if ($token->is('(')) {
147147
$this->skip();
148-
$arguments = array();
149-
$value = new FunctionCall($value, $this->parseParentheses()->nodes);
148+
$value = new FunctionCall($value, $this->parseParentheses()->nodes, $applicant);
150149

151150
continue;
152151
}
@@ -158,17 +157,16 @@ protected function appendFunctionsCalls(&$value)
158157
}
159158
if ($token->isAssignation()) {
160159
$this->skip();
161-
$arguments = array();
162-
$valueToAssign = $this->expectValue($this->next());
160+
$valueToAssign = $this->expectValue($this->next(), $previousToken);
163161
$value = new Assignation($token->type, $value, $valueToAssign);
164162

165163
continue;
166164
}
167165

168166
$this->skip();
169-
$nextValue = $this->expectValue($this->next());
167+
$nextValue = $this->expectValue($this->next(), $previousToken);
170168
$value = new Dyiade($token->type, $value, $nextValue);
171-
$token = $this->get(0);
169+
$this->get(0);
172170

173171
continue;
174172
}
@@ -177,15 +175,15 @@ protected function appendFunctionsCalls(&$value)
177175
}
178176
}
179177

180-
protected function expectValue($next, $token = null)
178+
protected function expectValue($next, $token = null, $applicant = null)
181179
{
182180
if (!$next) {
183181
if ($token) {
184182
throw $this->unexpected($token);
185183
}
186184
throw new Exception('Value expected after ' . $this->exceptionInfos(), 20);
187185
}
188-
$value = $this->getValueFromToken($next);
186+
$value = $this->getValueFromToken($next, $token, $applicant);
189187
if (!$value) {
190188
throw $this->unexpected($next);
191189
}

0 commit comments

Comments
 (0)