Skip to content

Commit

Permalink
Fix SyntaxErrorException throwing ValueError in PHP 8 (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Graham Campbell <[email protected]>
  • Loading branch information
flavioheleno and GrahamCampbell authored Jun 14, 2021
1 parent 14fe6ef commit 9b87907
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SyntaxErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
$expression
) {
$message = "Syntax error at character {$token['pos']}\n"
. $expression . "\n" . str_repeat(' ', $token['pos']) . "^\n";
. $expression . "\n" . str_repeat(' ', max($token['pos'], 0)) . "^\n";
$message .= !is_array($expectedTypesOrMessage)
? $expectedTypesOrMessage
: $this->createTokenMessage($token, $expectedTypesOrMessage);
Expand Down
20 changes: 20 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ public function testHandlesEmptyExpressions()
{
(new Parser(new Lexer()))->parse('');
}

/**
* @dataProvider invalidExpressionProvider
* @expectedException \JmesPath\SyntaxErrorException
* @expectedExceptionMessag Syntax error at character 0
*/
public function testHandlesInvalidExpressions($expr)
{
(new Parser(new Lexer()))->parse($expr);
}

public function invalidExpressionProvider()
{
return [
['='],
['<'],
['>'],
['|']
];
}
}

0 comments on commit 9b87907

Please sign in to comment.