Skip to content

Commit 0c039a6

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/JsPhpize/Parser/TokenExtractor.php
2 parents 2e28f04 + 5ea96e4 commit 0c039a6

File tree

15 files changed

+164
-27
lines changed

15 files changed

+164
-27
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ php:
77
- '5.6'
88
- '7.0'
99
- '7.1'
10-
- hhvm
10+
11+
matrix:
12+
include:
13+
- php: hhvm
14+
dist: trusty
15+
sudo: required
1116

1217
before_script:
1318
- travis_retry composer self-update

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();

examples/prototype.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
item = "uno";
2+
string = item.charAt(0)
3+
4+
.toUpperCase() +
5+
item.slice(1);
6+
7+
return string;

examples/prototype.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Uno

examples/special-members.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
attributes = {
2+
class: 'foo'
3+
};
4+
5+
return attributes.class;

examples/special-members.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

src/JsPhpize/Compiler/Compiler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,21 @@ 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) .
195196
($arguments === '' ? '' : ', ' . $arguments) .
196197
')';
197198

198-
if ($function instanceof Variable) {
199+
if ($function instanceof Variable && count($function->children) === 0) {
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',
@@ -284,6 +289,12 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
284289
$dynamicCall;
285290
}
286291

292+
if (count($functionCall->children)) {
293+
$arguments = $this->mapNodesArray($functionCall->children, $indent);
294+
array_unshift($arguments, $dynamicCall);
295+
$dynamicCall = $this->helperWrap('dot', $arguments);
296+
}
297+
287298
return $dynamicCall;
288299
}
289300

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ function ($base) {
1212
return $getFromArray($base, $key);
1313
}
1414
};
15+
$fallbackDot = function ($base, $key) use ($getCallable) {
16+
if (is_string($base)) {
17+
if ($key === 'substr' || $key === 'slice') {
18+
return function ($start, $length = null) use ($base) {
19+
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);
20+
};
21+
}
22+
if ($key === 'charAt') {
23+
return function ($pos) use ($base) {
24+
return substr($base, $pos, 1);
25+
};
26+
}
27+
if ($key === 'indexOf') {
28+
return function ($needle) use ($base) {
29+
$pos = strpos($base, $needle);
30+
31+
return $pos === false ? -1 : $pos;
32+
};
33+
}
34+
if ($key === 'toUpperCase') {
35+
return function () use ($base) {
36+
return strtoupper($base);
37+
};
38+
}
39+
if ($key === 'toLowerCase') {
40+
return function () use ($base) {
41+
return strtolower($base);
42+
};
43+
}
44+
}
45+
46+
return $getCallable($base, $key);
47+
};
1548
foreach (array_slice(func_get_args(), 1) as $key) {
1649
$base = is_array($base)
1750
? $getFromArray($base, $key)
@@ -26,7 +59,7 @@ function ($base) {
2659
)
2760
)
2861
)
29-
: $getCallable($base, $key)
62+
: $fallbackDot($base, $key)
3063
);
3164
}
3265

src/JsPhpize/Lexer/Token.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function isValue()
4343
return $this->typeIn(array('variable', 'constant', 'string', 'number'));
4444
}
4545

46+
public function isValidMember()
47+
{
48+
return $this->typeIn(array('variable', 'keyword'));
49+
}
50+
4651
protected function isComparison()
4752
{
4853
return $this->typeIn(array('===', '!==', '>=', '<=', '<>', '!=', '==', '>', '<'));

0 commit comments

Comments
 (0)