1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node\Printer;
4:
5: use Override;
6: use PhpParser\Node;
7: use PhpParser\Node\Scalar\String_;
8: use PhpParser\PrettyPrinter\Standard;
9: use PHPStan\DependencyInjection\AutowiredService;
10: use PHPStan\Node\BooleanAndNode;
11: use PHPStan\Node\BooleanOrNode;
12: use PHPStan\Node\Expr\AlwaysRememberedExpr;
13: use PHPStan\Node\Expr\CloneReinitializationExpr;
14: use PHPStan\Node\Expr\ExistingArrayDimFetch;
15: use PHPStan\Node\Expr\ForeachValueByRefExpr;
16: use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
17: use PHPStan\Node\Expr\GetIterableValueTypeExpr;
18: use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
19: use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
20: use PHPStan\Node\Expr\NativeTypeExpr;
21: use PHPStan\Node\Expr\OriginalForeachKeyExpr;
22: use PHPStan\Node\Expr\OriginalForeachValueExpr;
23: use PHPStan\Node\Expr\OriginalPropertyTypeExpr;
24: use PHPStan\Node\Expr\ParameterVariableOriginalValueExpr;
25: use PHPStan\Node\Expr\PossiblyImpureCallExpr;
26: use PHPStan\Node\Expr\PropertyInitializationExpr;
27: use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
28: use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
29: use PHPStan\Node\Expr\TypeExpr;
30: use PHPStan\Node\Expr\UnsetOffsetExpr;
31: use PHPStan\Node\FunctionCallableNode;
32: use PHPStan\Node\InstantiationCallableNode;
33: use PHPStan\Node\IssetExpr;
34: use PHPStan\Node\MethodCallableNode;
35: use PHPStan\Node\StaticMethodCallableNode;
36: use PHPStan\Type\VerbosityLevel;
37: use function preg_match;
38: use function sprintf;
39:
40: /**
41: * @api
42: */
43: #[AutowiredService(as: Printer::class)]
44: final class Printer extends Standard
45: {
46:
47: /**
48: * Normalize curly-brace member access with a constant string name to the
49: * bareword form, so that e.g. `$obj->{'n'}` and `$obj->n` (or `$obj->{'n'}()`
50: * and `$obj->n()`) produce identical expression keys and are treated as the
51: * same member by the analyser.
52: */
53: #[Override]
54: protected function pObjectProperty(Node $node): string
55: {
56: if (
57: $node instanceof String_
58: && preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $node->value) === 1
59: ) {
60: return $node->value;
61: }
62:
63: return parent::pObjectProperty($node);
64: }
65:
66: protected function pPHPStan_Node_TypeExpr(TypeExpr $expr): string // phpcs:ignore
67: {
68: return sprintf('__phpstanType(%s)', $expr->getExprType()->describe(VerbosityLevel::precise()));
69: }
70:
71: protected function pPHPStan_Node_NativeTypeExpr(NativeTypeExpr $expr): string // phpcs:ignore
72: {
73: return sprintf('__phpstanNativeType(%s, %s)', $expr->getPhpDocType()->describe(VerbosityLevel::precise()), $expr->getNativeType()->describe(VerbosityLevel::precise()));
74: }
75:
76: protected function pPHPStan_Node_GetOffsetValueTypeExpr(GetOffsetValueTypeExpr $expr): string // phpcs:ignore
77: {
78: return sprintf('__phpstanGetOffsetValueType(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));
79: }
80:
81: protected function pPHPStan_Node_UnsetOffsetExpr(UnsetOffsetExpr $expr): string // phpcs:ignore
82: {
83: return sprintf('__phpstanUnsetOffset(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));
84: }
85:
86: protected function pPHPStan_Node_GetIterableValueTypeExpr(GetIterableValueTypeExpr $expr): string // phpcs:ignore
87: {
88: return sprintf('__phpstanGetIterableValueType(%s)', $this->p($expr->getExpr()));
89: }
90:
91: protected function pPHPStan_Node_GetIterableKeyTypeExpr(GetIterableKeyTypeExpr $expr): string // phpcs:ignore
92: {
93: return sprintf('__phpstanGetIterableKeyType(%s)', $this->p($expr->getExpr()));
94: }
95:
96: protected function pPHPStan_Node_ExistingArrayDimFetch(ExistingArrayDimFetch $expr): string // phpcs:ignore
97: {
98: return sprintf('__phpstanExistingArrayDimFetch(%s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()));
99: }
100:
101: protected function pPHPStan_Node_OriginalPropertyTypeExpr(OriginalPropertyTypeExpr $expr): string // phpcs:ignore
102: {
103: return sprintf('__phpstanOriginalPropertyType(%s)', $this->p($expr->getPropertyFetch()));
104: }
105:
106: protected function pPHPStan_Node_SetOffsetValueTypeExpr(SetOffsetValueTypeExpr $expr): string // phpcs:ignore
107: {
108: return sprintf('__phpstanSetOffsetValueType(%s, %s, %s)', $this->p($expr->getVar()), $expr->getDim() !== null ? $this->p($expr->getDim()) : 'null', $this->p($expr->getValue()));
109: }
110:
111: protected function pPHPStan_Node_SetExistingOffsetValueTypeExpr(SetExistingOffsetValueTypeExpr $expr): string // phpcs:ignore
112: {
113: return sprintf('__phpstanSetExistingOffsetValueType(%s, %s, %s)', $this->p($expr->getVar()), $this->p($expr->getDim()), $this->p($expr->getValue()));
114: }
115:
116: protected function pPHPStan_Node_AlwaysRememberedExpr(AlwaysRememberedExpr $expr): string // phpcs:ignore
117: {
118: return sprintf('__phpstanRemembered(%s)', $this->p($expr->getExpr()));
119: }
120:
121: protected function pPHPStan_Node_PossiblyImpureCallExpr(PossiblyImpureCallExpr $expr): string // phpcs:ignore
122: {
123: return sprintf('__phpstanPossiblyImpure(%s, %s)', $this->p($expr->callExpr), $this->p($expr->impactedExpr));
124: }
125:
126: protected function pPHPStan_Node_PropertyInitializationExpr(PropertyInitializationExpr $expr): string // phpcs:ignore
127: {
128: return sprintf('__phpstanPropertyInitialization(%s)', $expr->getPropertyName());
129: }
130:
131: protected function pPHPStan_Node_CloneReinitializationExpr(CloneReinitializationExpr $expr): string // phpcs:ignore
132: {
133: return sprintf('__phpstanCloneReinitialization(%s)', $expr->getPropertyName());
134: }
135:
136: protected function pPHPStan_Node_ForeachValueByRefExpr(ForeachValueByRefExpr $expr): string // phpcs:ignore
137: {
138: return sprintf('__phpstanForeachValueByRef(%s)', $this->p($expr->getExpr()));
139: }
140:
141: protected function pPHPStan_Node_ParameterVariableOriginalValueExpr(ParameterVariableOriginalValueExpr $expr): string // phpcs:ignore
142: {
143: return sprintf('__phpstanParameterVariableOriginalValue(%s)', $expr->getVariableName());
144: }
145:
146: protected function pPHPStan_Node_OriginalForeachKeyExpr(OriginalForeachKeyExpr $expr): string // phpcs:ignore
147: {
148: return sprintf('__phpstanOriginalForeachKey(%s)', $expr->getVariableName());
149: }
150:
151: protected function pPHPStan_Node_OriginalForeachValueExpr(OriginalForeachValueExpr $expr): string // phpcs:ignore
152: {
153: return sprintf('__phpstanOriginalForeachValue(%s)', $expr->getVariableName());
154: }
155:
156: protected function pPHPStan_Node_IntertwinedVariableByReferenceWithExpr(IntertwinedVariableByReferenceWithExpr $expr): string // phpcs:ignore
157: {
158: return sprintf('__phpstanIntertwinedVariableByReference(%s, %s, %s)', $expr->getVariableName(), $this->p($expr->getExpr()), $this->p($expr->getAssignedExpr()));
159: }
160:
161: protected function pPHPStan_Node_IssetExpr(IssetExpr $expr): string // phpcs:ignore
162: {
163: return sprintf('__phpstanIssetExpr(%s)', $this->p($expr->getExpr()));
164: }
165:
166: protected function pPHPStan_Node_BooleanOrNode(BooleanOrNode $expr): string // phpcs:ignore
167: {
168: return sprintf('__phpstanBooleanOr(%s, %s)', $this->p($expr->getOriginalNode()->left), $this->p($expr->getOriginalNode()->right));
169: }
170:
171: protected function pPHPStan_Node_BooleanAndNode(BooleanAndNode $expr): string // phpcs:ignore
172: {
173: return sprintf('__phpstanBooleanAnd(%s, %s)', $this->p($expr->getOriginalNode()->left), $this->p($expr->getOriginalNode()->right));
174: }
175:
176: protected function pPHPStan_Node_FunctionCallableNode(FunctionCallableNode $expr): string // phpcs:ignore
177: {
178: return sprintf('__phpstanFunctionCallable(%s)', $this->p($expr->getOriginalNode()));
179: }
180:
181: protected function pPHPStan_Node_MethodCallableNode(MethodCallableNode $expr): string // phpcs:ignore
182: {
183: return sprintf('__phpstanMethodCallable(%s)', $this->p($expr->getOriginalNode()));
184: }
185:
186: protected function pPHPStan_Node_StaticMethodCallableNode(StaticMethodCallableNode $expr): string // phpcs:ignore
187: {
188: return sprintf('__phpstanStaticMethodCallable(%s)', $this->p($expr->getOriginalNode()));
189: }
190:
191: protected function pPHPStan_Node_InstantiationCallableNode(InstantiationCallableNode $expr): string // phpcs:ignore
192: {
193: return sprintf('__phpstanInstantiationCallable(%s)', $this->p($expr->getOriginalNode()));
194: }
195:
196: }
197: