33declare (strict_types=1 );
44namespace Rector \Console \Command ;
55
6- use RectorPrefix202403 \Nette \Utils \Strings ;
7- use Rector \CustomRules \SimpleNodeDumper ;
86use Rector \PhpParser \Parser \SimplePhpParser ;
7+ use Rector \Util \NodePrinter ;
98use RectorPrefix202403 \Symfony \Component \Console \Command \Command ;
109use RectorPrefix202403 \Symfony \Component \Console \Input \InputInterface ;
1110use RectorPrefix202403 \Symfony \Component \Console \Input \InputOption ;
1514use Throwable ;
1615final 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}
0 commit comments