annotate vendor/psy/psysh/test/Psy/Test/CodeCleaner/CodeCleanerTestCase.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Test\CodeCleaner;
Chris@0 13
Chris@0 14 use PhpParser\NodeTraverser;
Chris@0 15 use PhpParser\PrettyPrinter\Standard as Printer;
Chris@0 16 use Psy\CodeCleaner\CodeCleanerPass;
Chris@0 17 use Psy\Exception\ParseErrorException;
Chris@0 18 use Psy\ParserFactory;
Chris@0 19
Chris@0 20 class CodeCleanerTestCase extends \PHPUnit\Framework\TestCase
Chris@0 21 {
Chris@0 22 protected $pass;
Chris@0 23 protected $traverser;
Chris@0 24 private $parser;
Chris@0 25 private $printer;
Chris@0 26
Chris@0 27 protected function setPass(CodeCleanerPass $pass)
Chris@0 28 {
Chris@0 29 $this->pass = $pass;
Chris@0 30 if (!isset($this->traverser)) {
Chris@0 31 $this->traverser = new NodeTraverser();
Chris@0 32 }
Chris@0 33 $this->traverser->addVisitor($this->pass);
Chris@0 34 }
Chris@0 35
Chris@0 36 protected function parse($code, $prefix = '<?php ')
Chris@0 37 {
Chris@0 38 $code = $prefix . $code;
Chris@0 39 try {
Chris@0 40 return $this->getParser()->parse($code);
Chris@0 41 } catch (\PhpParser\Error $e) {
Chris@0 42 if (!$this->parseErrorIsEOF($e)) {
Chris@0 43 throw ParseErrorException::fromParseError($e);
Chris@0 44 }
Chris@0 45
Chris@0 46 try {
Chris@0 47 // Unexpected EOF, try again with an implicit semicolon
Chris@0 48 return $this->getParser()->parse($code . ';');
Chris@0 49 } catch (\PhpParser\Error $e) {
Chris@0 50 return false;
Chris@0 51 }
Chris@0 52 }
Chris@0 53 }
Chris@0 54
Chris@0 55 protected function traverse(array $stmts)
Chris@0 56 {
Chris@0 57 return $this->traverser->traverse($stmts);
Chris@0 58 }
Chris@0 59
Chris@0 60 protected function prettyPrint(array $stmts)
Chris@0 61 {
Chris@0 62 return $this->getPrinter()->prettyPrint($stmts);
Chris@0 63 }
Chris@0 64
Chris@0 65 protected function assertProcessesAs($from, $to)
Chris@0 66 {
Chris@0 67 $stmts = $this->parse($from);
Chris@0 68 $stmts = $this->traverse($stmts);
Chris@0 69 $this->assertEquals($to, $this->prettyPrint($stmts));
Chris@0 70 }
Chris@0 71
Chris@0 72 private function getParser()
Chris@0 73 {
Chris@0 74 if (!isset($this->parser)) {
Chris@0 75 $parserFactory = new ParserFactory();
Chris@0 76 $this->parser = $parserFactory->createParser();
Chris@0 77 }
Chris@0 78
Chris@0 79 return $this->parser;
Chris@0 80 }
Chris@0 81
Chris@0 82 private function getPrinter()
Chris@0 83 {
Chris@0 84 if (!isset($this->printer)) {
Chris@0 85 $this->printer = new Printer();
Chris@0 86 }
Chris@0 87
Chris@0 88 return $this->printer;
Chris@0 89 }
Chris@0 90
Chris@0 91 private function parseErrorIsEOF(\PhpParser\Error $e)
Chris@0 92 {
Chris@0 93 $msg = $e->getRawMessage();
Chris@0 94
Chris@0 95 return ($msg === 'Unexpected token EOF') || (strpos($msg, 'Syntax error, unexpected EOF') !== false);
Chris@0 96 }
Chris@0 97 }