Skip to content

Commit

Permalink
Add dump_node() helper function (#5696)
Browse files Browse the repository at this point in the history
* working dump nodes helper function

* PHPStan fixes

* naming conventions

---------

Co-authored-by: Peter Fox <peter.fox@peterfox.me>
  • Loading branch information
TomasVotruba and peterfox committed Mar 6, 2024
1 parent 643814d commit 7507136
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 39 deletions.
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,8 @@ parameters:
-
message: '#Offset \(int\|string\) on non\-empty\-array<PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocChildNode> in isset\(\) always exists and is not nullable#'
path: src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php

# false positive
-
message: '#Parameters should use "array" types as the only types passed to this method#'
path: src/Util/NodePrinter.php
43 changes: 4 additions & 39 deletions src/Console/Command/DetectNodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Rector\Console\Command;

use Nette\Utils\Strings;
use Rector\CustomRules\SimpleNodeDumper;
use Rector\PhpParser\Parser\SimplePhpParser;
use Rector\Util\NodePrinter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -17,21 +16,10 @@

final class DetectNodeCommand extends Command
{
/**
* @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(
private readonly SymfonyStyle $symfonyStyle,
private readonly SimplePhpParser $simplePhpParser,
private readonly NodePrinter $nodePrinter,
private readonly SymfonyStyle $symfonyStyle
) {
parent::__construct();
}
Expand Down Expand Up @@ -61,23 +49,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function addConsoleColors(string $contents): string
{
// decorate class names
$colorContents = Strings::replace(
$contents,
self::CLASS_NAME_REGEX,
static fn (array $match): string => '<fg=green>' . $match['class_name'] . '</>('
);

// decorate keys
return Strings::replace(
$colorContents,
self::PROPERTY_KEY_REGEX,
static fn (array $match): string => '<fg=yellow>' . $match['key'] . '</>:'
);
}

private function askQuestionAndDumpNode(): void
{
$question = new Question('Write short PHP code snippet');
Expand All @@ -90,12 +61,6 @@ private function askQuestionAndDumpNode(): void
return;
}

$dumpedNodesContents = SimpleNodeDumper::dump($nodes);

// colorize
$colorContents = $this->addConsoleColors($dumpedNodesContents);
$this->symfonyStyle->writeln($colorContents);

$this->symfonyStyle->newLine();
$this->nodePrinter->printNodes($nodes);
}
}
61 changes: 61 additions & 0 deletions src/Util/NodePrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Rector\Util;

use Nette\Utils\Strings;
use PhpParser\Node;
use Rector\CustomRules\SimpleNodeDumper;
use Symfony\Component\Console\Style\SymfonyStyle;

final readonly class NodePrinter
{
/**
* @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(
private SymfonyStyle $symfonyStyle
) {
}

/**
* @param Node|Node[] $nodes
*/
public function printNodes(Node|array $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 fn (array $match): string => '<fg=green>' . $match['class_name'] . '</>('
);

// decorate keys
return Strings::replace(
$colorContents,
self::PROPERTY_KEY_REGEX,
static fn (array $match): string => '<fg=yellow>' . $match['key'] . '</>:'
);
}
}
24 changes: 24 additions & 0 deletions src/functions/node_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

declare(strict_types=1);

use Illuminate\Container\Container;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Util\NodePrinter;
use Symfony\Component\Console\Output\OutputInterface;

if (! function_exists('print_node')) {
/**
Expand All @@ -21,3 +25,23 @@ function print_node(Node | array $node): void
}
}
}

if (! function_exists('dump_node')) {
/**
* @param Node|Node[] $node
*/
function dump_node(Node|array $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);
}
}

0 comments on commit 7507136

Please sign in to comment.