Skip to content

Commit 4cb58ba

Browse files
committed
Refactor duplicated code
split: b9b3ee8e49f0fa088c11b2066722592e2c397dff
1 parent b91ede3 commit 4cb58ba

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

Parser/State.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,6 @@ public function store()
632632
*/
633633
public function throwException($message, $code = 0, TokenInterface $relatedToken = null, $previous = null)
634634
{
635-
$pattern = "Failed to parse: %s \nNear: %s \nLine: %s \nOffset: %s";
636-
637635
$lexer = $this->parser->getLexer();
638636
//This will basically check for a source location for this Node. The process is like:
639637
//- If there's a related token, we use the cloned token's source location
@@ -648,24 +646,15 @@ public function throwException($message, $code = 0, TokenInterface $relatedToken
648646
: new SourceLocation(null, 0, 0)
649647
);
650648

651-
$near = $lexer->hasState()
652-
? $lexer->getState()->getReader()->peek(20)
653-
: '[No clue]';
654-
655-
//We know where it occured!
656-
$path = $location->getPath();
657-
658-
if ($path) {
659-
$pattern .= "\nPath: $path";
660-
}
661-
662649
throw new ParserException(
663650
$location,
664-
vsprintf($pattern, [
665-
$message,
666-
$near,
667-
$location->getLine(),
668-
$location->getOffset(),
651+
ParserException::message($message, [
652+
'path' => $location->getPath(),
653+
'near' => $lexer->hasState()
654+
? $lexer->getState()->getReader()->peek(20)
655+
: '[No clue]',
656+
'line' => $location->getLine(),
657+
'offset' => $location->getOffset(),
669658
]),
670659
$code,
671660
$relatedToken,

ParserException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public function getRelatedToken()
3232
{
3333
return $this->relatedToken;
3434
}
35+
36+
public static function message($message, array $details = [])
37+
{
38+
return static::getFailureMessage('parse', $message, $details);
39+
}
3540
}

0 commit comments

Comments
 (0)