Skip to content

Commit

Permalink
Fix emulative lexer with default error handler
Browse files Browse the repository at this point in the history
If no error handler is provided, explicitly create one, so we don't
end up calling handleError() on null.
  • Loading branch information
nikic committed May 20, 2023
1 parent c23976a commit fb2c3ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/PhpParser/Lexer/Emulative.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function startLexing(string $code, ?ErrorHandler $errorHandler = null): v
return;
}

if ($errorHandler === null) {
$errorHandler = new ErrorHandler\Throwing();
}

$this->patches = [];
foreach ($emulators as $emulator) {
$code = $emulator->preprocessCode($code, $this->patches);
Expand Down
8 changes: 8 additions & 0 deletions test/PhpParser/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function provideTestError() {
];
}

public function testDefaultErrorHandler() {
$this->expectException(Error::class);
$this->expectExceptionMessage('Unterminated comment on line 1');
$lexer = $this->getLexer();
$lexer->startLexing("<?php readonly /*");
$lexer->getNextToken();
}

/**
* @dataProvider provideTestLex
*/
Expand Down

0 comments on commit fb2c3ac

Please sign in to comment.