1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node\Printer;
4:
5: use PhpParser\Node\Expr;
6: use PHPStan\DependencyInjection\AutowiredService;
7:
8: /**
9: * @api
10: */
11: #[AutowiredService]
12: final class ExprPrinter
13: {
14:
15: public const ATTRIBUTE_CACHE_KEY = 'phpstan_cache_printer';
16:
17: public function __construct(private Printer $printer)
18: {
19: }
20:
21: public function printExpr(Expr $expr): string
22: {
23: /** @var string|null $exprString */
24: $exprString = $expr->getAttribute(self::ATTRIBUTE_CACHE_KEY);
25: if ($exprString === null) {
26: $exprString = $this->printer->prettyPrintExpr($expr);
27: $expr->setAttribute(self::ATTRIBUTE_CACHE_KEY, $exprString);
28: }
29:
30: return $exprString;
31: }
32:
33: }
34: