createParser(); } $this->parser = $parser; $this->printer = $printer ?: new Printer(); $this->traverser = $traverser ?: new NodeTraverser(); foreach ($this->getDefaultPasses() as $pass) { $this->traverser->addVisitor($pass); } } /** * Get default CodeCleaner passes. * * @return array */ private function getDefaultPasses() { return array( new AbstractClassPass(), new AssignThisVariablePass(), new FunctionReturnInWriteContextPass(), new CallTimePassByReferencePass(), new CalledClassPass(), new InstanceOfPass(), new LeavePsyshAlonePass(), new LegacyEmptyPass(), new ImplicitReturnPass(), new UseStatementPass(), // must run before namespace and validation passes new NamespacePass($this), // must run after the implicit return pass new StrictTypesPass(), new StaticConstructorPass(), new ValidFunctionNamePass(), new ValidClassNamePass(), new ValidConstantPass(), new MagicConstantsPass(), new ExitPass(), ); } /** * Clean the given array of code. * * @throws ParseErrorException if the code is invalid PHP, and cannot be coerced into valid PHP. * * @param array $codeLines * @param bool $requireSemicolons * * @return string|false Cleaned PHP code, False if the input is incomplete. */ public function clean(array $codeLines, $requireSemicolons = false) { $stmts = $this->parse('traverser->traverse($stmts); return $this->printer->prettyPrint($stmts); } /** * Set the current local namespace. * * @param null|array $namespace (default: null) * * @return null|array */ public function setNamespace(array $namespace = null) { $this->namespace = $namespace; } /** * Get the current local namespace. * * @return null|array */ public function getNamespace() { return $this->namespace; } /** * Lex and parse a block of code. * * @see Parser::parse * * @param string $code * @param bool $requireSemicolons * * @return array A set of statements */ protected function parse($code, $requireSemicolons = false) { try { return $this->parser->parse($code); } catch (\PhpParser\Error $e) { if ($this->parseErrorIsUnclosedString($e, $code)) { return false; } if (!$this->parseErrorIsEOF($e)) { throw ParseErrorException::fromParseError($e); } if ($requireSemicolons) { return false; } try { // Unexpected EOF, try again with an implicit semicolon return $this->parser->parse($code . ';'); } catch (\PhpParser\Error $e) { return false; } } } private function parseErrorIsEOF(\PhpParser\Error $e) { $msg = $e->getRawMessage(); return ($msg === 'Unexpected token EOF') || (strpos($msg, 'Syntax error, unexpected EOF') !== false); } /** * A special test for unclosed single-quoted strings. * * Unlike (all?) other unclosed statements, single quoted strings have * their own special beautiful snowflake syntax error just for * themselves. * * @param \PhpParser\Error $e * @param string $code * * @return bool */ private function parseErrorIsUnclosedString(\PhpParser\Error $e, $code) { if ($e->getRawMessage() !== 'Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE') { return false; } try { $this->parser->parse($code . "';"); } catch (\Exception $e) { return false; } return true; } }