Skip to content

Commit 7da00fc

Browse files
committed
Updated Rector to commit 750713625e27e07175e4c7144802fb623422c036
rectorphp/rector-src@7507136 Add dump_node() helper function (#5696)
1 parent 5cc97f5 commit 7da00fc

File tree

7 files changed

+88
-33
lines changed

7 files changed

+88
-33
lines changed

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '643814dc92ceacb159adbac79c1ab6fb6e5cc2ee';
22+
public const PACKAGE_VERSION = '750713625e27e07175e4c7144802fb623422c036';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2024-03-06 20:34:50';
27+
public const RELEASE_DATE = '2024-03-06 20:53:31';
2828
/**
2929
* @var int
3030
*/

src/Console/Command/DetectNodeCommand.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
declare (strict_types=1);
44
namespace Rector\Console\Command;
55

6-
use RectorPrefix202403\Nette\Utils\Strings;
7-
use Rector\CustomRules\SimpleNodeDumper;
86
use Rector\PhpParser\Parser\SimplePhpParser;
7+
use Rector\Util\NodePrinter;
98
use RectorPrefix202403\Symfony\Component\Console\Command\Command;
109
use RectorPrefix202403\Symfony\Component\Console\Input\InputInterface;
1110
use RectorPrefix202403\Symfony\Component\Console\Input\InputOption;
@@ -15,30 +14,26 @@
1514
use Throwable;
1615
final class DetectNodeCommand extends Command
1716
{
18-
/**
19-
* @readonly
20-
* @var \Symfony\Component\Console\Style\SymfonyStyle
21-
*/
22-
private $symfonyStyle;
2317
/**
2418
* @readonly
2519
* @var \Rector\PhpParser\Parser\SimplePhpParser
2620
*/
2721
private $simplePhpParser;
2822
/**
29-
* @var string
30-
* @see https://regex101.com/r/Fe8n73/1
23+
* @readonly
24+
* @var \Rector\Util\NodePrinter
3125
*/
32-
private const CLASS_NAME_REGEX = '#(?<class_name>PhpParser(.*?))\\(#ms';
26+
private $nodePrinter;
3327
/**
34-
* @var string
35-
* @see https://regex101.com/r/uQFuvL/1
28+
* @readonly
29+
* @var \Symfony\Component\Console\Style\SymfonyStyle
3630
*/
37-
private const PROPERTY_KEY_REGEX = '#(?<key>[\\w\\d]+)\\:#';
38-
public function __construct(SymfonyStyle $symfonyStyle, SimplePhpParser $simplePhpParser)
31+
private $symfonyStyle;
32+
public function __construct(SimplePhpParser $simplePhpParser, NodePrinter $nodePrinter, SymfonyStyle $symfonyStyle)
3933
{
40-
$this->symfonyStyle = $symfonyStyle;
4134
$this->simplePhpParser = $simplePhpParser;
35+
$this->nodePrinter = $nodePrinter;
36+
$this->symfonyStyle = $symfonyStyle;
4237
parent::__construct();
4338
}
4439
protected function configure() : void
@@ -60,17 +55,6 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
6055
$this->askQuestionAndDumpNode();
6156
return self::SUCCESS;
6257
}
63-
private function addConsoleColors(string $contents) : string
64-
{
65-
// decorate class names
66-
$colorContents = Strings::replace($contents, self::CLASS_NAME_REGEX, static function (array $match) : string {
67-
return '<fg=green>' . $match['class_name'] . '</>(';
68-
});
69-
// decorate keys
70-
return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string {
71-
return '<fg=yellow>' . $match['key'] . '</>:';
72-
});
73-
}
7458
private function askQuestionAndDumpNode() : void
7559
{
7660
$question = new Question('Write short PHP code snippet');
@@ -81,10 +65,6 @@ private function askQuestionAndDumpNode() : void
8165
$this->symfonyStyle->warning('Provide valid PHP code');
8266
return;
8367
}
84-
$dumpedNodesContents = SimpleNodeDumper::dump($nodes);
85-
// colorize
86-
$colorContents = $this->addConsoleColors($dumpedNodesContents);
87-
$this->symfonyStyle->writeln($colorContents);
88-
$this->symfonyStyle->newLine();
68+
$this->nodePrinter->printNodes($nodes);
8969
}
9070
}

src/Util/NodePrinter.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Util;
5+
6+
use RectorPrefix202403\Nette\Utils\Strings;
7+
use PhpParser\Node;
8+
use Rector\CustomRules\SimpleNodeDumper;
9+
use RectorPrefix202403\Symfony\Component\Console\Style\SymfonyStyle;
10+
final class NodePrinter
11+
{
12+
/**
13+
* @readonly
14+
* @var \Symfony\Component\Console\Style\SymfonyStyle
15+
*/
16+
private $symfonyStyle;
17+
/**
18+
* @var string
19+
* @see https://regex101.com/r/Fe8n73/1
20+
*/
21+
private const CLASS_NAME_REGEX = '#(?<class_name>PhpParser(.*?))\\(#ms';
22+
/**
23+
* @var string
24+
* @see https://regex101.com/r/uQFuvL/1
25+
*/
26+
private const PROPERTY_KEY_REGEX = '#(?<key>[\\w\\d]+)\\:#';
27+
public function __construct(SymfonyStyle $symfonyStyle)
28+
{
29+
$this->symfonyStyle = $symfonyStyle;
30+
}
31+
/**
32+
* @param Node|Node[] $nodes
33+
*/
34+
public function printNodes($nodes) : void
35+
{
36+
$dumpedNodesContents = SimpleNodeDumper::dump($nodes);
37+
// colorize
38+
$colorContents = $this->addConsoleColors($dumpedNodesContents);
39+
$this->symfonyStyle->writeln($colorContents);
40+
$this->symfonyStyle->newLine();
41+
}
42+
private function addConsoleColors(string $contents) : string
43+
{
44+
// decorate class names
45+
$colorContents = Strings::replace($contents, self::CLASS_NAME_REGEX, static function (array $match) : string {
46+
return '<fg=green>' . $match['class_name'] . '</>(';
47+
});
48+
// decorate keys
49+
return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static function (array $match) : string {
50+
return '<fg=yellow>' . $match['key'] . '</>:';
51+
});
52+
}
53+
}

src/functions/node_helper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
declare (strict_types=1);
44
namespace RectorPrefix202403;
55

6+
use RectorPrefix202403\Illuminate\Container\Container;
67
use PhpParser\Node;
78
use PhpParser\PrettyPrinter\Standard;
9+
use Rector\Console\Style\SymfonyStyleFactory;
10+
use Rector\Util\NodePrinter;
11+
use RectorPrefix202403\Symfony\Component\Console\Output\OutputInterface;
812
if (!\function_exists('print_node')) {
913
/**
1014
* @param Node|Node[] $node
@@ -19,3 +23,18 @@ function print_node($node) : void
1923
}
2024
}
2125
}
26+
if (!\function_exists('dump_node')) {
27+
/**
28+
* @param Node|Node[] $node
29+
*/
30+
function dump_node($node) : void
31+
{
32+
$symfonyStyle = Container::getInstance()->make(SymfonyStyleFactory::class)->create();
33+
// we turn up the verbosity so it's visible in tests overriding the
34+
// default which is to be quite during tests
35+
$symfonyStyle->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
36+
$symfonyStyle->newLine();
37+
$nodePrinter = new NodePrinter($symfonyStyle);
38+
$nodePrinter->printNodes($node);
39+
}
40+
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,7 @@
24302430
'Rector\\Util\\FileHasher' => $baseDir . '/src/Util/FileHasher.php',
24312431
'Rector\\Util\\MemoryLimiter' => $baseDir . '/src/Util/MemoryLimiter.php',
24322432
'Rector\\Util\\NewLineSplitter' => $baseDir . '/src/Util/NewLineSplitter.php',
2433+
'Rector\\Util\\NodePrinter' => $baseDir . '/src/Util/NodePrinter.php',
24332434
'Rector\\Util\\PhpVersionFactory' => $baseDir . '/src/Util/PhpVersionFactory.php',
24342435
'Rector\\Util\\Reflection\\PrivatesAccessor' => $baseDir . '/src/Util/Reflection/PrivatesAccessor.php',
24352436
'Rector\\Util\\StringUtils' => $baseDir . '/src/Util/StringUtils.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
26492649
'Rector\\Util\\FileHasher' => __DIR__ . '/../..' . '/src/Util/FileHasher.php',
26502650
'Rector\\Util\\MemoryLimiter' => __DIR__ . '/../..' . '/src/Util/MemoryLimiter.php',
26512651
'Rector\\Util\\NewLineSplitter' => __DIR__ . '/../..' . '/src/Util/NewLineSplitter.php',
2652+
'Rector\\Util\\NodePrinter' => __DIR__ . '/../..' . '/src/Util/NodePrinter.php',
26522653
'Rector\\Util\\PhpVersionFactory' => __DIR__ . '/../..' . '/src/Util/PhpVersionFactory.php',
26532654
'Rector\\Util\\Reflection\\PrivatesAccessor' => __DIR__ . '/../..' . '/src/Util/Reflection/PrivatesAccessor.php',
26542655
'Rector\\Util\\StringUtils' => __DIR__ . '/../..' . '/src/Util/StringUtils.php',

vendor/scoper-autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) {
3535

3636
// Function aliases. For more information see:
3737
// https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases
38+
if (!function_exists('dump_node')) { function dump_node() { return \RectorPrefix202403\dump_node(...func_get_args()); } }
3839
if (!function_exists('formatErrorMessage')) { function formatErrorMessage() { return \RectorPrefix202403\formatErrorMessage(...func_get_args()); } }
3940
if (!function_exists('includeIfExists')) { function includeIfExists() { return \RectorPrefix202403\includeIfExists(...func_get_args()); } }
4041
if (!function_exists('mb_check_encoding')) { function mb_check_encoding() { return \RectorPrefix202403\mb_check_encoding(...func_get_args()); } }

0 commit comments

Comments
 (0)