-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dump_node() helper function (#5696)
* working dump nodes helper function * PHPStan fixes * naming conventions --------- Co-authored-by: Peter Fox <peter.fox@peterfox.me>
- Loading branch information
1 parent
643814d
commit 7507136
Showing
4 changed files
with
94 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] . '</>:' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters