Skip to content

Commit 150d481

Browse files
authored
Merge pull request #22 from pug-php/refactor
Refactor
2 parents 6e6ff7d + 85bbe50 commit 150d481

File tree

4 files changed

+51
-110
lines changed

4 files changed

+51
-110
lines changed

.codeclimate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ engines:
1212
phpmd:
1313
enabled: true
1414
checks:
15+
Naming/ShortMethodName:
16+
enabled: false
1517
Design/CouplingBetweenObjects:
1618
enabled: false
1719
Design/EvalExpression:

src/JsPhpize/Compiler/Compiler.php

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919

2020
class Compiler
2121
{
22+
/**
23+
* @const string
24+
*/
25+
const STATIC_CALL_FUNCTIONS = 'array,echo,print,printf,exit,__halt_compiler,abstract,and,array,as,break,
26+
callable,case,catch,class,clone,const,continue,declare,default,die,do,echo,else,elseif,empty,enddeclare,
27+
endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,final,for,foreach,function,global,goto,if,
28+
implements,include,include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,print,private,
29+
protected,public,require,require_once,return,static,switch,throw,trait,try,unset,use,var,while,xor';
30+
2231
/**
2332
* @var JsPhpize
2433
*/
@@ -236,87 +245,8 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
236245
$name = $function->name;
237246
$staticCall = $name . '(' . $arguments . ')';
238247

239-
if ($applicant === 'new') {
240-
return $staticCall;
241-
}
242-
243-
if (in_array($name, array(
244-
'array',
245-
'echo',
246-
'print',
247-
'printf',
248-
'exit',
249-
))) {
250-
return $staticCall;
251-
}
252-
253-
if (in_array($name, array(
254-
'__halt_compiler',
255-
'abstract',
256-
'and',
257-
'array',
258-
'as',
259-
'break',
260-
'callable',
261-
'case',
262-
'catch',
263-
'class',
264-
'clone',
265-
'const',
266-
'continue',
267-
'declare',
268-
'default',
269-
'die',
270-
'do',
271-
'echo',
272-
'else',
273-
'elseif',
274-
'empty',
275-
'enddeclare',
276-
'endfor',
277-
'endforeach',
278-
'endif',
279-
'endswitch',
280-
'endwhile',
281-
'eval',
282-
'exit',
283-
'extends',
284-
'final',
285-
'for',
286-
'foreach',
287-
'function',
288-
'global',
289-
'goto',
290-
'if',
291-
'implements',
292-
'include',
293-
'include_once',
294-
'instanceof',
295-
'insteadof',
296-
'interface',
297-
'isset',
298-
'list',
299-
'namespace',
300-
'new',
301-
'or',
302-
'print',
303-
'private',
304-
'protected',
305-
'public',
306-
'require',
307-
'require_once',
308-
'return',
309-
'static',
310-
'switch',
311-
'throw',
312-
'trait',
313-
'try',
314-
'unset',
315-
'use',
316-
'var',
317-
'while',
318-
'xor',
319-
))) {
248+
$functions = str_replace(array("\n", "\t", "\r", ' '), '', static::STATIC_CALL_FUNCTIONS);
249+
if ($applicant === 'new' || in_array($name, explode(',', $functions))) {
320250
return $staticCall;
321251
}
322252

src/JsPhpize/JsPhpizeOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function hasFlag($flag)
150150
* @param int $flag flag to set
151151
* @param bool $enabled flag state
152152
*/
153-
public function setFlag($flag, $enabled = true)
153+
public function setFlag($flag, $enabled)
154154
{
155155
if ($enabled) {
156156
$this->flags |= $flag;

src/JsPhpize/Parser/Parser.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function parseParentheses()
102102
}
103103

104104
if ($this->engine->getOption('allowTruncatedParentheses')) {
105-
$this->engine->setFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES);
105+
$this->engine->setFlag(JsPhpize::FLAG_TRUNCATED_PARENTHESES, true);
106106

107107
return $parentheses;
108108
}
@@ -357,44 +357,53 @@ protected function parseLet()
357357
return $letVariable->value;
358358
}
359359

360-
protected function parseInstructions($block)
360+
protected function parseInstruction($block, $token, &$initNext)
361361
{
362-
$initNext = false;
362+
if ($token->type === 'keyword') {
363+
if ($token->isIn('var', 'const')) {
364+
$initNext = true;
363365

364-
while ($token = $this->next()) {
365-
if ($token->is($block->multipleInstructions ? '}' : ';')) {
366-
break;
366+
return true;
367367
}
368368

369-
if ($token->type === 'keyword') {
370-
if ($token->isIn('var', 'const')) {
371-
$initNext = true;
372-
373-
continue;
374-
}
369+
if ($token->value === 'let') {
370+
$initNext = true;
371+
$block->let($this->parseLet($token));
375372

376-
if ($token->value === 'let') {
377-
$initNext = true;
378-
$block->let($this->parseLet($token));
373+
return true;
374+
}
375+
}
379376

380-
continue;
381-
}
377+
if ($instruction = $this->getInstructionFromToken($token)) {
378+
if ($initNext && $instruction instanceof Variable) {
379+
$instruction = new Assignation('=', $instruction, new Constant('constant', 'null'));
382380
}
381+
$initNext = false;
382+
$block->addInstruction($instruction);
383383

384-
if ($instruction = $this->getInstructionFromToken($token)) {
385-
if ($initNext && $instruction instanceof Variable) {
386-
$instruction = new Assignation('=', $instruction, new Constant('constant', 'null'));
387-
}
388-
$initNext = false;
389-
$block->addInstruction($instruction);
384+
return true;
385+
}
390386

391-
continue;
392-
}
387+
if ($token->is(';') || !$this->engine->getOption('strict')) {
388+
$initNext = false;
389+
$block->endInstruction();
390+
391+
return true;
392+
}
393+
394+
return false;
395+
}
396+
397+
protected function parseInstructions($block)
398+
{
399+
$initNext = false;
393400

394-
if ($token->is(';') || !$this->engine->getOption('strict')) {
395-
$initNext = false;
396-
$block->endInstruction();
401+
while ($token = $this->next()) {
402+
if ($token->is($block->multipleInstructions ? '}' : ';')) {
403+
break;
404+
}
397405

406+
if ($this->parseInstruction($block, $token, $initNext)) {
398407
continue;
399408
}
400409

0 commit comments

Comments
 (0)