1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node\Printer;
4:
5: use Override;
6: use PhpParser\Node;
7: use PhpParser\Node\Expr;
8: use PhpParser\Node\Scalar\String_;
9: use PhpParser\PrettyPrinter\Standard;
10: use PHPStan\DependencyInjection\AutowiredService;
11: use PHPStan\Node\BooleanAndNode;
12: use PHPStan\Node\BooleanOrNode;
13: use PHPStan\Node\Expr\AlwaysRememberedExpr;
14: use PHPStan\Node\Expr\CloneReinitializationExpr;
15: use PHPStan\Node\Expr\ExistingArrayDimFetch;
16: use PHPStan\Node\Expr\ForeachValueByRefExpr;
17: use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
18: use PHPStan\Node\Expr\NativeTypeExpr;
19: use PHPStan\Node\Expr\ObjectAliasExpr;
20: use PHPStan\Node\Expr\OffsetWriteBaseExpr;
21: use PHPStan\Node\Expr\OriginalForeachKeyExpr;
22: use PHPStan\Node\Expr\OriginalForeachValueExpr;
23: use PHPStan\Node\Expr\ParameterVariableOriginalValueExpr;
24: use PHPStan\Node\Expr\PropertyInitializationExpr;
25: use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
26: use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
27: use PHPStan\Node\Expr\TypeExpr;
28: use PHPStan\Node\Expr\UnsetOffsetExpr;
29: use PHPStan\Node\FunctionCallableNode;
30: use PHPStan\Node\InstantiationCallableNode;
31: use PHPStan\Node\IssetExpr;
32: use PHPStan\Node\MethodCallableNode;
33: use PHPStan\Node\StaticMethodCallableNode;
34: use PHPStan\Type\VerbosityLevel;
35: use function preg_match;
36: use function sprintf;
37: use function str_contains;
38:
39: /**
40: * @api
41: */
42: #[AutowiredService(as: Printer::class)]
43: final class Printer extends Standard
44: {
45:
46: /**
47: * Every construct the printer descends into comes back through here, and
48: * each level of a chain is printed again in turn for its own expression
49: * key, so a chain of depth N costs O(N^2). Remembering each expression's
50: * printed form on its node makes the levels below it O(1) — and it is the
51: * very same cache ExprPrinter fills, so a key printed once is never
52: * rebuilt, wherever it was first reached from.
53: *
54: * Only the standalone form is remembered: at a lower precedence the
55: * printer may parenthesise, and a form containing a newline was indented
56: * for the level it was printed at, while expression keys are printed at
57: * the top level.
58: */
59: #[Override]
60: protected function p(
61: Node $node,
62: int $precedence = self::MAX_PRECEDENCE,
63: int $lhsPrecedence = self::MAX_PRECEDENCE,
64: bool $parentFormatPreserved = false,
65: ): string
66: {
67: if (
68: !$node instanceof Expr
69: || $precedence !== self::MAX_PRECEDENCE
70: || $lhsPrecedence !== self::MAX_PRECEDENCE
71: ) {
72: return parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);
73: }
74:
75: // ExprPrinter::printExpr() caches a multi-line form too, printed at the top level - nested
76: // here, it would need the current indentation
77: $printed = $node->getAttribute(ExprPrinter::ATTRIBUTE_CACHE_KEY);
78: if ($printed !== null && !str_contains($printed, "\n")) {
79: return $printed;
80: }
81:
82: $printed = parent::p($node, parentFormatPreserved: $parentFormatPreserved);
83: if (!str_contains($printed, "\n")) {
84: $node->setAttribute(ExprPrinter::ATTRIBUTE_CACHE_KEY, $printed);
85: }
86:
87: return $printed;
88: }
89:
90: /**
91: * Normalize curly-brace member access with a constant string name to the
92: * bareword form, so that e.g. `$obj->{'n'}` and `$obj->n` (or `$obj->{'n'}()`
93: * and `$obj->n()`) produce identical expression keys and are treated as the
94: * same member by the analyser.
95: */
96: #[Override]
97: protected function pObjectProperty(Node $node): string
98: {
99: if (
100: $node instanceof String_
101: && preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $node->value) === 1
102: ) {
103: return $node->value;
104: }
105:
106: return parent::pObjectProperty($node);
107: }
108:
109: protected function pPHPStan_Node_TypeExpr(TypeExpr $expr): string // phpcs:ignore
110: {
111: return sprintf('__phpstanType(%s)', $expr->getExprType()->describe(VerbosityLevel::precise()));
112: }
113:
114: protected function pPHPStan_Node_NativeTypeExpr(NativeTypeExpr $expr): string // phpcs:ignore
115: {
116: return sprintf('__phpstanNativeType(%s, %s)', $expr->getPhpDocType()->describe(VerbosityLevel::precise()), $expr->getNativeType()->describe(VerbosityLevel::precise()));
117: }
118:
119: protected function pPHPStan_Node_UnsetOffsetExpr(UnsetOffsetExpr $expr): string // phpcs:ignore
120: {
121: return sprintf('__phpstanUnsetOffset(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));
122: }
123:
124: protected function pPHPStan_Node_ExistingArrayDimFetch(ExistingArrayDimFetch $expr): string // phpcs:ignore
125: {
126: return sprintf('__phpstanExistingArrayDimFetch(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));
127: }
128:
129: protected function pPHPStan_Node_SetOffsetValueTypeExpr(SetOffsetValueTypeExpr $expr): string // phpcs:ignore
130: {
131: return sprintf('__phpstanSetOffsetValueType(%s, %s, %s)', $this->p($expr->getVar()), $expr->getDim() !== null ? $this->p($expr->getDim()) : 'null', $this->p($expr->getValue()));
132: }
133:
134: protected function pPHPStan_Node_SetExistingOffsetValueTypeExpr(SetExistingOffsetValueTypeExpr $expr): string // phpcs:ignore
135: {
136: return sprintf('__phpstanSetExistingOffsetValueType(%s, %s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()), $this->p($expr->getValue()));
137: }
138:
139: protected function pPHPStan_Node_AlwaysRememberedExpr(AlwaysRememberedExpr $expr): string // phpcs:ignore
140: {
141: return sprintf('__phpstanRemembered(%s)', $this->p($expr->getExpr()));
142: }
143:
144: protected function pPHPStan_Node_PropertyInitializationExpr(PropertyInitializationExpr $expr): string // phpcs:ignore
145: {
146: return sprintf('__phpstanPropertyInitialization(%s)', $expr->getPropertyName());
147: }
148:
149: protected function pPHPStan_Node_CloneReinitializationExpr(CloneReinitializationExpr $expr): string // phpcs:ignore
150: {
151: return sprintf('__phpstanCloneReinitialization(%s)', $expr->getPropertyName());
152: }
153:
154: protected function pPHPStan_Node_ForeachValueByRefExpr(ForeachValueByRefExpr $expr): string // phpcs:ignore
155: {
156: return sprintf('__phpstanForeachValueByRef(%s)', $this->p($expr->getExpr()));
157: }
158:
159: protected function pPHPStan_Node_ParameterVariableOriginalValueExpr(ParameterVariableOriginalValueExpr $expr): string // phpcs:ignore
160: {
161: return sprintf('__phpstanParameterVariableOriginalValue(%s)', $expr->getVariableName());
162: }
163:
164: protected function pPHPStan_Node_OriginalForeachKeyExpr(OriginalForeachKeyExpr $expr): string // phpcs:ignore
165: {
166: return sprintf('__phpstanOriginalForeachKey(%s)', $expr->getVariableName());
167: }
168:
169: protected function pPHPStan_Node_OriginalForeachValueExpr(OriginalForeachValueExpr $expr): string // phpcs:ignore
170: {
171: return sprintf('__phpstanOriginalForeachValue(%s)', $expr->getVariableName());
172: }
173:
174: protected function pPHPStan_Node_IntertwinedVariableByReferenceWithExpr(IntertwinedVariableByReferenceWithExpr $expr): string // phpcs:ignore
175: {
176: return sprintf('__phpstanIntertwinedVariableByReference(%s, %s, %s)', $expr->getVariableName(), $this->p($expr->getExpr()), $this->p($expr->getAssignedExpr()));
177: }
178:
179: protected function pPHPStan_Node_ObjectAliasExpr(ObjectAliasExpr $expr): string // phpcs:ignore
180: {
181: return sprintf('__phpstanObjectAlias(%s, %s)', $this->p($expr->getExpr()), $this->p($expr->getAliasedExpr()));
182: }
183:
184: protected function pPHPStan_Node_OffsetWriteBaseExpr(OffsetWriteBaseExpr $expr): string // phpcs:ignore
185: {
186: return sprintf('__phpstanOffsetWriteBase(%s)', $this->p($expr->getDimFetch()));
187: }
188:
189: protected function pPHPStan_Node_IssetExpr(IssetExpr $expr): string // phpcs:ignore
190: {
191: return sprintf('__phpstanIssetExpr(%s)', $this->p($expr->getExpr()));
192: }
193:
194: protected function pPHPStan_Node_BooleanOrNode(BooleanOrNode $expr): string // phpcs:ignore
195: {
196: return sprintf('__phpstanBooleanOr(%s, %s)', $this->p($expr->getOriginalNode()->left), $this->p($expr->getOriginalNode()->right));
197: }
198:
199: protected function pPHPStan_Node_BooleanAndNode(BooleanAndNode $expr): string // phpcs:ignore
200: {
201: return sprintf('__phpstanBooleanAnd(%s, %s)', $this->p($expr->getOriginalNode()->left), $this->p($expr->getOriginalNode()->right));
202: }
203:
204: protected function pPHPStan_Node_FunctionCallableNode(FunctionCallableNode $expr): string // phpcs:ignore
205: {
206: return sprintf('__phpstanFunctionCallable(%s)', $this->p($expr->getOriginalNode()));
207: }
208:
209: protected function pPHPStan_Node_MethodCallableNode(MethodCallableNode $expr): string // phpcs:ignore
210: {
211: return sprintf('__phpstanMethodCallable(%s)', $this->p($expr->getOriginalNode()));
212: }
213:
214: protected function pPHPStan_Node_StaticMethodCallableNode(StaticMethodCallableNode $expr): string // phpcs:ignore
215: {
216: return sprintf('__phpstanStaticMethodCallable(%s)', $this->p($expr->getOriginalNode()));
217: }
218:
219: protected function pPHPStan_Node_InstantiationCallableNode(InstantiationCallableNode $expr): string // phpcs:ignore
220: {
221: return sprintf('__phpstanInstantiationCallable(%s)', $this->p($expr->getOriginalNode()));
222: }
223:
224: }
225: