From 1d6c441d16c4abcf30397d69e24b66f840dbc5f8 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 10 Feb 2017 21:27:12 -0500 Subject: [PATCH] Use a more fault-tolerant version of php-parser --- src/Psalm/Checker/FileChecker.php | 10 ++++- tests/BadFormatTest.php | 62 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/BadFormatTest.php diff --git a/src/Psalm/Checker/FileChecker.php b/src/Psalm/Checker/FileChecker.php index 5d96f05e8..744572834 100644 --- a/src/Psalm/Checker/FileChecker.php +++ b/src/Psalm/Checker/FileChecker.php @@ -577,8 +577,16 @@ class FileChecker extends SourceChecker implements StatementsSource $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $lexer); + $error_handler = new \PhpParser\ErrorHandler\Collecting(); + /** @var array */ - $stmts = $parser->parse($file_contents); + $stmts = $parser->parse($file_contents, $error_handler); + + if (!$stmts && $error_handler->hasErrors()) { + foreach ($error_handler->getErrors() as $error) { + throw $error; + } + } } if ($parser_cache_directory && $cache_location) { diff --git a/tests/BadFormatTest.php b/tests/BadFormatTest.php new file mode 100644 index 000000000..fc30e79d7 --- /dev/null +++ b/tests/BadFormatTest.php @@ -0,0 +1,62 @@ +create(ParserFactory::PREFER_PHP7); + self::$config = new TestConfig(); + } + + /** + * @return void + */ + public function setUp() + { + FileChecker::clearCache(); + $this->project_checker = new \Psalm\Checker\ProjectChecker(); + $this->project_checker->setConfig(self::$config); + } + + /** + * @return void + */ + public function testMissingSemicolon() + { + $this->project_checker->registerFile( + getcwd() . '/somefile.php', + 'hello = 5 + } + }' + ); + + $file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker); + $file_checker->visitAndAnalyzeMethods(); + } +}