#!/usr/bin/env php ['startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments']]); $parser = (new \PhpParser\ParserFactory())->create(\PhpParser\ParserFactory::PREFER_PHP7, $lexer); $dumper = new \PhpParser\NodeDumper(['dumpComments' => \true, 'dumpPositions' => $attributes['with-positions']]); $prettyPrinter = new \PhpParser\PrettyPrinter\Standard(); $traverser = new \PhpParser\NodeTraverser(); $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver()); foreach ($files as $file) { if (\strpos($file, ' Code {$code}\n"); } else { if (!\file_exists($file)) { \fwrite(\STDERR, "File {$file} does not exist.\n"); exit(1); } $code = \file_get_contents($file); \fwrite(\STDERR, "====> File {$file}:\n"); } if ($attributes['with-recovery']) { $errorHandler = new \PhpParser\ErrorHandler\Collecting(); $stmts = $parser->parse($code, $errorHandler); foreach ($errorHandler->getErrors() as $error) { $message = \RectorPrefix20220314\formatErrorMessage($error, $code, $attributes['with-column-info']); \fwrite(\STDERR, $message . "\n"); } if (null === $stmts) { continue; } } else { try { $stmts = $parser->parse($code); } catch (\PhpParser\Error $error) { $message = \RectorPrefix20220314\formatErrorMessage($error, $code, $attributes['with-column-info']); \fwrite(\STDERR, $message . "\n"); exit(1); } } foreach ($operations as $operation) { if ('dump' === $operation) { \fwrite(\STDERR, "==> Node dump:\n"); echo $dumper->dump($stmts, $code), "\n"; } elseif ('pretty-print' === $operation) { \fwrite(\STDERR, "==> Pretty print:\n"); echo $prettyPrinter->prettyPrintFile($stmts), "\n"; } elseif ('json-dump' === $operation) { \fwrite(\STDERR, "==> JSON dump:\n"); echo \json_encode($stmts, \JSON_PRETTY_PRINT), "\n"; } elseif ('var-dump' === $operation) { \fwrite(\STDERR, "==> var_dump():\n"); \var_dump($stmts); } elseif ('resolve-names' === $operation) { \fwrite(\STDERR, "==> Resolved names.\n"); $stmts = $traverser->traverse($stmts); } } } function formatErrorMessage(\PhpParser\Error $e, $code, $withColumnInfo) { if ($withColumnInfo && $e->hasColumnInfo()) { return $e->getMessageWithColumnInfo($code); } else { return $e->getMessage(); } } function showHelp($error = '') { if ($error) { \fwrite(\STDERR, $error . "\n\n"); } \fwrite($error ? \STDERR : \STDOUT, << \false, 'with-positions' => \false, 'with-recovery' => \false]; \array_shift($args); $parseOptions = \true; foreach ($args as $arg) { if (!$parseOptions) { $files[] = $arg; continue; } switch ($arg) { case '--dump': case '-d': $operations[] = 'dump'; break; case '--pretty-print': case '-p': $operations[] = 'pretty-print'; break; case '--json-dump': case '-j': $operations[] = 'json-dump'; break; case '--var-dump': $operations[] = 'var-dump'; break; case '--resolve-names': case '-N': $operations[] = 'resolve-names'; break; case '--with-column-info': case '-c': $attributes['with-column-info'] = \true; break; case '--with-positions': case '-P': $attributes['with-positions'] = \true; break; case '--with-recovery': case '-r': $attributes['with-recovery'] = \true; break; case '--help': case '-h': \RectorPrefix20220314\showHelp(); break; case '--': $parseOptions = \false; break; default: if ($arg[0] === '-') { \RectorPrefix20220314\showHelp("Invalid operation {$arg}."); } else { $files[] = $arg; } } } return [$operations, $files, $attributes]; }