Chris@13: traverser = null; Chris@16: $this->parser = null; Chris@16: $this->printer = null; Chris@16: } Chris@16: Chris@13: protected function parse($code, $prefix = 'getParser()->parse($code); Chris@13: } catch (\PhpParser\Error $e) { Chris@13: if (!$this->parseErrorIsEOF($e)) { Chris@13: throw ParseErrorException::fromParseError($e); Chris@13: } Chris@13: Chris@13: try { Chris@13: // Unexpected EOF, try again with an implicit semicolon Chris@13: return $this->getParser()->parse($code . ';'); Chris@13: } catch (\PhpParser\Error $e) { Chris@13: return false; Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@13: protected function traverse(array $stmts) Chris@13: { Chris@13: if (!isset($this->traverser)) { Chris@13: throw new \RuntimeException('Test cases must provide a traverser'); Chris@13: } Chris@13: Chris@13: return $this->traverser->traverse($stmts); Chris@13: } Chris@13: Chris@13: protected function prettyPrint(array $stmts) Chris@13: { Chris@13: return $this->getPrinter()->prettyPrint($stmts); Chris@13: } Chris@13: Chris@13: protected function assertProcessesAs($from, $to) Chris@13: { Chris@13: $stmts = $this->parse($from); Chris@13: $stmts = $this->traverse($stmts); Chris@16: $toStmts = $this->parse($to); Chris@16: $this->assertSame($this->prettyPrint($toStmts), $this->prettyPrint($stmts)); Chris@13: } Chris@13: Chris@13: private function getParser() Chris@13: { Chris@13: if (!isset($this->parser)) { Chris@13: $parserFactory = new ParserFactory(); Chris@13: $this->parser = $parserFactory->createParser(); Chris@13: } Chris@13: Chris@13: return $this->parser; Chris@13: } Chris@13: Chris@13: private function getPrinter() Chris@13: { Chris@13: if (!isset($this->printer)) { Chris@13: $this->printer = new Printer(); Chris@13: } Chris@13: Chris@13: return $this->printer; Chris@13: } Chris@13: Chris@13: private function parseErrorIsEOF(\PhpParser\Error $e) Chris@13: { Chris@13: $msg = $e->getRawMessage(); Chris@13: Chris@17: return ($msg === 'Unexpected token EOF') || (\strpos($msg, 'Syntax error, unexpected EOF') !== false); Chris@13: } Chris@13: }