Skip to content

Commit 9ab8ee6

Browse files
committed
Split keyword statement different types
1 parent 16c7c91 commit 9ab8ee6

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/JsPhpize/Parser/Parser.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,32 @@ protected function getValueFromToken($token)
451451
return $value;
452452
}
453453

454+
protected function expectColon($errorMessage, $errorCode)
455+
{
456+
$colon = $this->next();
457+
if (!$colon || !$colon->is(':')) {
458+
throw new Exception($errorMessage, $errorCode);
459+
}
460+
}
461+
462+
protected function handleOptionalValue($keyword, $afterKeyword)
463+
{
464+
if (!$afterKeyword->is(';')) {
465+
$value = $this->expectValue($this->next());
466+
$keyword->setValue($value);
467+
}
468+
}
469+
470+
protected function handleParentheses($keyword, $afterKeyword)
471+
{
472+
if ($afterKeyword && $afterKeyword->is('(')) {
473+
$this->skip();
474+
$keyword->setValue($this->parseParentheses());
475+
} elseif ($keyword->needParenthesis()) {
476+
throw new Exception("'" . $keyword->type . "' block need parentheses.", 17);
477+
}
478+
}
479+
454480
protected function parseKeywordStatement($token)
455481
{
456482
$name = $token->value;
@@ -459,34 +485,18 @@ protected function parseKeywordStatement($token)
459485
case 'return':
460486
case 'continue':
461487
case 'break':
462-
$afterKeyword = $this->get(0);
463-
if (!$afterKeyword->is(';')) {
464-
$value = $this->expectValue($this->next());
465-
$keyword->setValue($value);
466-
}
488+
$this->handleOptionalValue($keyword, $this->get(0));
467489
break;
468490
case 'case':
469491
$value = $this->expectValue($this->next());
470492
$keyword->setValue($value);
471-
$colon = $this->next();
472-
if (!$colon || !$colon->is(':')) {
473-
throw new Exception("'case' must be followed by a value and a colon.", 21);
474-
}
493+
$this->expectColon("'case' must be followed by a value and a colon.", 21);
475494
break;
476495
case 'default':
477-
$colon = $this->next();
478-
if (!$colon || !$colon->is(':')) {
479-
throw new Exception("'default' must be followed by a colon.", 22);
480-
}
496+
$this->expectColon("'default' must be followed by a colon.", 22);
481497
break;
482498
default:
483-
$next = $this->get(0);
484-
if ($next && $next->is('(')) {
485-
$this->skip();
486-
$keyword->setValue($this->parseParentheses());
487-
} elseif ($keyword->needParenthesis()) {
488-
throw new Exception("'" . $keyword->type . "' block need parentheses.", 17);
489-
}
499+
$this->handleParentheses($keyword, $this->get(0));
490500
}
491501

492502
return $keyword;

0 commit comments

Comments
 (0)