Aggiornato Composer
This commit is contained in:
61
vendor/psy/psysh/src/Command/ParseCommand.php
vendored
61
vendor/psy/psysh/src/Command/ParseCommand.php
vendored
@@ -3,7 +3,7 @@
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
* (c) 2012-2023 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace Psy\Command;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Parser;
|
||||
use Psy\Context;
|
||||
use Psy\ContextAware;
|
||||
use Psy\Input\CodeArgument;
|
||||
@@ -37,16 +36,14 @@ class ParseCommand extends Command implements ContextAware, PresenterAware
|
||||
protected $context;
|
||||
|
||||
private $presenter;
|
||||
private $parserFactory;
|
||||
private $parsers;
|
||||
private $parser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($name = null)
|
||||
{
|
||||
$this->parserFactory = new ParserFactory();
|
||||
$this->parsers = [];
|
||||
$this->parser = (new ParserFactory())->createParser();
|
||||
|
||||
parent::__construct($name);
|
||||
}
|
||||
@@ -90,16 +87,11 @@ class ParseCommand extends Command implements ContextAware, PresenterAware
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$kindMsg = 'One of PhpParser\\ParserFactory constants: '
|
||||
.\implode(', ', ParserFactory::getPossibleKinds())
|
||||
." (default is based on current interpreter's version).";
|
||||
|
||||
$this
|
||||
->setName('parse')
|
||||
->setDefinition([
|
||||
new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'),
|
||||
new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
|
||||
new InputOption('kind', '', InputOption::VALUE_REQUIRED, $kindMsg, $this->parserFactory->getDefaultKind()),
|
||||
])
|
||||
->setDescription('Parse PHP code and show the abstract syntax tree.')
|
||||
->setHelp(
|
||||
@@ -119,58 +111,17 @@ HELP
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$code = $input->getArgument('code');
|
||||
if (\strpos($code, '<?') === false) {
|
||||
$code = '<?php '.$code;
|
||||
}
|
||||
|
||||
$parserKind = $input->getOption('kind');
|
||||
$depth = $input->getOption('depth');
|
||||
$nodes = $this->parse($this->getParser($parserKind), $code);
|
||||
|
||||
$nodes = $this->parser->parse($code);
|
||||
$output->page($this->presenter->present($nodes, $depth));
|
||||
|
||||
$this->context->setReturnValue($nodes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lex and parse a string of code into statements.
|
||||
*
|
||||
* @param Parser $parser
|
||||
* @param string $code
|
||||
*
|
||||
* @return array Statements
|
||||
*/
|
||||
private function parse(Parser $parser, string $code): array
|
||||
{
|
||||
try {
|
||||
return $parser->parse($code);
|
||||
} catch (\PhpParser\Error $e) {
|
||||
if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
// If we got an unexpected EOF, let's try it again with a semicolon.
|
||||
return $parser->parse($code.';');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get (or create) the Parser instance.
|
||||
*
|
||||
* @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
|
||||
*
|
||||
* @return Parser
|
||||
*/
|
||||
private function getParser(string $kind = null): Parser
|
||||
{
|
||||
if (!\array_key_exists($kind, $this->parsers)) {
|
||||
$this->parsers[$kind] = $this->parserFactory->createParser($kind);
|
||||
}
|
||||
|
||||
return $this->parsers[$kind];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user