Updated Rector to commit 750713625e27e07175e4c7144802fb623422c036

750713625e Add dump_node() helper function (#5696)
This commit is contained in:
Tomas Votruba 2024-03-06 20:55:59 +00:00
parent 5cc97f5690
commit 7da00fc128
7 changed files with 88 additions and 33 deletions

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '643814dc92ceacb159adbac79c1ab6fb6e5cc2ee';
public const PACKAGE_VERSION = '750713625e27e07175e4c7144802fb623422c036';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-03-06 20:34:50';
public const RELEASE_DATE = '2024-03-06 20:53:31';
/**
* @var int
*/

View File

@ -3,9 +3,8 @@
declare (strict_types=1);
namespace Rector\Console\Command;
use RectorPrefix202403\Nette\Utils\Strings;
use Rector\CustomRules\SimpleNodeDumper;
use Rector\PhpParser\Parser\SimplePhpParser;
use Rector\Util\NodePrinter;
use RectorPrefix202403\Symfony\Component\Console\Command\Command;
use RectorPrefix202403\Symfony\Component\Console\Input\InputInterface;
use RectorPrefix202403\Symfony\Component\Console\Input\InputOption;
@ -15,30 +14,26 @@ use RectorPrefix202403\Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
final class DetectNodeCommand extends Command
{
/**
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $symfonyStyle;
/**
* @readonly
* @var \Rector\PhpParser\Parser\SimplePhpParser
*/
private $simplePhpParser;
/**
* @var string
* @see https://regex101.com/r/Fe8n73/1
* @readonly
* @var \Rector\Util\NodePrinter
*/
private const CLASS_NAME_REGEX = '#(?<class_name>PhpParser(.*?))\\(#ms';
private $nodePrinter;
/**
* @var string
* @see https://regex101.com/r/uQFuvL/1
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private const PROPERTY_KEY_REGEX = '#(?<key>[\\w\\d]+)\\:#';
public function __construct(SymfonyStyle $symfonyStyle, SimplePhpParser $simplePhpParser)
private $symfonyStyle;
public function __construct(SimplePhpParser $simplePhpParser, NodePrinter $nodePrinter, SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
$this->simplePhpParser = $simplePhpParser;
$this->nodePrinter = $nodePrinter;
$this->symfonyStyle = $symfonyStyle;
parent::__construct();
}
protected function configure() : void
@ -60,17 +55,6 @@ final class DetectNodeCommand extends Command
$this->askQuestionAndDumpNode();
return self::SUCCESS;
}
private function addConsoleColors(string $contents) : string
{
// decorate class names
$colorContents = Strings::replace($contents, self::CLASS_NAME_REGEX, static function (array $match) : string {
return '<fg=green>' . $match['class_name'] . '</>(';
});
// decorate keys
return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string {
return '<fg=yellow>' . $match['key'] . '</>:';
});
}
private function askQuestionAndDumpNode() : void
{
$question = new Question('Write short PHP code snippet');
@ -81,10 +65,6 @@ final class DetectNodeCommand extends Command
$this->symfonyStyle->warning('Provide valid PHP code');
return;
}
$dumpedNodesContents = SimpleNodeDumper::dump($nodes);
// colorize
$colorContents = $this->addConsoleColors($dumpedNodesContents);
$this->symfonyStyle->writeln($colorContents);
$this->symfonyStyle->newLine();
$this->nodePrinter->printNodes($nodes);
}
}

53
src/Util/NodePrinter.php Normal file
View File

@ -0,0 +1,53 @@
<?php
declare (strict_types=1);
namespace Rector\Util;
use RectorPrefix202403\Nette\Utils\Strings;
use PhpParser\Node;
use Rector\CustomRules\SimpleNodeDumper;
use RectorPrefix202403\Symfony\Component\Console\Style\SymfonyStyle;
final class NodePrinter
{
/**
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
*/
private $symfonyStyle;
/**
* @var string
* @see https://regex101.com/r/Fe8n73/1
*/
private const CLASS_NAME_REGEX = '#(?<class_name>PhpParser(.*?))\\(#ms';
/**
* @var string
* @see https://regex101.com/r/uQFuvL/1
*/
private const PROPERTY_KEY_REGEX = '#(?<key>[\\w\\d]+)\\:#';
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}
/**
* @param Node|Node[] $nodes
*/
public function printNodes($nodes) : void
{
$dumpedNodesContents = SimpleNodeDumper::dump($nodes);
// colorize
$colorContents = $this->addConsoleColors($dumpedNodesContents);
$this->symfonyStyle->writeln($colorContents);
$this->symfonyStyle->newLine();
}
private function addConsoleColors(string $contents) : string
{
// decorate class names
$colorContents = Strings::replace($contents, self::CLASS_NAME_REGEX, static function (array $match) : string {
return '<fg=green>' . $match['class_name'] . '</>(';
});
// decorate keys
return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string {
return '<fg=yellow>' . $match['key'] . '</>:';
});
}
}

View File

@ -3,8 +3,12 @@
declare (strict_types=1);
namespace RectorPrefix202403;
use RectorPrefix202403\Illuminate\Container\Container;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Util\NodePrinter;
use RectorPrefix202403\Symfony\Component\Console\Output\OutputInterface;
if (!\function_exists('print_node')) {
/**
* @param Node|Node[] $node
@ -19,3 +23,18 @@ if (!\function_exists('print_node')) {
}
}
}
if (!\function_exists('dump_node')) {
/**
* @param Node|Node[] $node
*/
function dump_node($node) : void
{
$symfonyStyle = Container::getInstance()->make(SymfonyStyleFactory::class)->create();
// we turn up the verbosity so it's visible in tests overriding the
// default which is to be quite during tests
$symfonyStyle->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$symfonyStyle->newLine();
$nodePrinter = new NodePrinter($symfonyStyle);
$nodePrinter->printNodes($node);
}
}

View File

@ -2430,6 +2430,7 @@ return array(
'Rector\\Util\\FileHasher' => $baseDir . '/src/Util/FileHasher.php',
'Rector\\Util\\MemoryLimiter' => $baseDir . '/src/Util/MemoryLimiter.php',
'Rector\\Util\\NewLineSplitter' => $baseDir . '/src/Util/NewLineSplitter.php',
'Rector\\Util\\NodePrinter' => $baseDir . '/src/Util/NodePrinter.php',
'Rector\\Util\\PhpVersionFactory' => $baseDir . '/src/Util/PhpVersionFactory.php',
'Rector\\Util\\Reflection\\PrivatesAccessor' => $baseDir . '/src/Util/Reflection/PrivatesAccessor.php',
'Rector\\Util\\StringUtils' => $baseDir . '/src/Util/StringUtils.php',

View File

@ -2649,6 +2649,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
'Rector\\Util\\FileHasher' => __DIR__ . '/../..' . '/src/Util/FileHasher.php',
'Rector\\Util\\MemoryLimiter' => __DIR__ . '/../..' . '/src/Util/MemoryLimiter.php',
'Rector\\Util\\NewLineSplitter' => __DIR__ . '/../..' . '/src/Util/NewLineSplitter.php',
'Rector\\Util\\NodePrinter' => __DIR__ . '/../..' . '/src/Util/NodePrinter.php',
'Rector\\Util\\PhpVersionFactory' => __DIR__ . '/../..' . '/src/Util/PhpVersionFactory.php',
'Rector\\Util\\Reflection\\PrivatesAccessor' => __DIR__ . '/../..' . '/src/Util/Reflection/PrivatesAccessor.php',
'Rector\\Util\\StringUtils' => __DIR__ . '/../..' . '/src/Util/StringUtils.php',

View File

@ -35,6 +35,7 @@ humbug_phpscoper_expose_class('Product', 'RectorPrefix202403\Product');
// Function aliases. For more information see:
// https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases
if (!function_exists('dump_node')) { function dump_node() { return \RectorPrefix202403\dump_node(...func_get_args()); } }
if (!function_exists('formatErrorMessage')) { function formatErrorMessage() { return \RectorPrefix202403\formatErrorMessage(...func_get_args()); } }
if (!function_exists('includeIfExists')) { function includeIfExists() { return \RectorPrefix202403\includeIfExists(...func_get_args()); } }
if (!function_exists('mb_check_encoding')) { function mb_check_encoding() { return \RectorPrefix202403\mb_check_encoding(...func_get_args()); } }