diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index b51ffbb6b26..82d8a5d0d00 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */ diff --git a/src/Console/Command/DetectNodeCommand.php b/src/Console/Command/DetectNodeCommand.php index bac24ba141b..323c3bb1688 100644 --- a/src/Console/Command/DetectNodeCommand.php +++ b/src/Console/Command/DetectNodeCommand.php @@ -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 = '#(?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 = '#(?[\\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 '' . $match['class_name'] . '('; - }); - // decorate keys - return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string { - return '' . $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); } } diff --git a/src/Util/NodePrinter.php b/src/Util/NodePrinter.php new file mode 100644 index 00000000000..3055bd991b0 --- /dev/null +++ b/src/Util/NodePrinter.php @@ -0,0 +1,53 @@ +PhpParser(.*?))\\(#ms'; + /** + * @var string + * @see https://regex101.com/r/uQFuvL/1 + */ + private const PROPERTY_KEY_REGEX = '#(?[\\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 '' . $match['class_name'] . '('; + }); + // decorate keys + return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string { + return '' . $match['key'] . ':'; + }); + } +} diff --git a/src/functions/node_helper.php b/src/functions/node_helper.php index b0c93034067..931d2008b54 100644 --- a/src/functions/node_helper.php +++ b/src/functions/node_helper.php @@ -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); + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index e391a0f21c9..05193e1680d 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -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', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 047a525686d..8c2a12d2a01 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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', diff --git a/vendor/scoper-autoload.php b/vendor/scoper-autoload.php index 5f8aab77ab7..765fbcc7452 100644 --- a/vendor/scoper-autoload.php +++ b/vendor/scoper-autoload.php @@ -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()); } }