forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExprPrinter.php
More file actions
38 lines (29 loc) · 816 Bytes
/
ExprPrinter.php
File metadata and controls
38 lines (29 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php declare(strict_types = 1);
namespace PHPStan\Node\Printer;
use PhpParser\Node\Expr;
use PHPStan\DependencyInjection\AutowiredService;
/**
* @api
*/
#[AutowiredService]
final class ExprPrinter
{
public const ATTRIBUTE_CACHE_KEY = 'phpstan_cache_printer';
public function __construct(private Printer $printer)
{
}
public function printExpr(Expr $expr): string
{
// perf optimize for the most common path
if ($expr instanceof Expr\Variable && !$expr->name instanceof Expr) {
return '$' . $expr->name;
}
/** @var string|null $exprString */
$exprString = $expr->getAttribute(self::ATTRIBUTE_CACHE_KEY);
if ($exprString === null) {
$exprString = $this->printer->prettyPrintExpr($expr);
$expr->setAttribute(self::ATTRIBUTE_CACHE_KEY, $exprString);
}
return $exprString;
}
}