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