| 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: | |
| 41: | |
| 42: | #[AutowiredService(as: Printer::class)] |
| 43: | final class Printer extends Standard |
| 44: | { |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 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: | |
| 76: | |
| 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: | |
| 92: | |
| 93: | |
| 94: | |
| 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 |
| 110: | { |
| 111: | return sprintf('__phpstanType(%s)', $expr->getExprType()->describe(VerbosityLevel::precise())); |
| 112: | } |
| 113: | |
| 114: | protected function pPHPStan_Node_NativeTypeExpr(NativeTypeExpr $expr): string |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 140: | { |
| 141: | return sprintf('__phpstanRemembered(%s)', $this->p($expr->getExpr())); |
| 142: | } |
| 143: | |
| 144: | protected function pPHPStan_Node_PropertyInitializationExpr(PropertyInitializationExpr $expr): string |
| 145: | { |
| 146: | return sprintf('__phpstanPropertyInitialization(%s)', $expr->getPropertyName()); |
| 147: | } |
| 148: | |
| 149: | protected function pPHPStan_Node_CloneReinitializationExpr(CloneReinitializationExpr $expr): string |
| 150: | { |
| 151: | return sprintf('__phpstanCloneReinitialization(%s)', $expr->getPropertyName()); |
| 152: | } |
| 153: | |
| 154: | protected function pPHPStan_Node_ForeachValueByRefExpr(ForeachValueByRefExpr $expr): string |
| 155: | { |
| 156: | return sprintf('__phpstanForeachValueByRef(%s)', $this->p($expr->getExpr())); |
| 157: | } |
| 158: | |
| 159: | protected function pPHPStan_Node_ParameterVariableOriginalValueExpr(ParameterVariableOriginalValueExpr $expr): string |
| 160: | { |
| 161: | return sprintf('__phpstanParameterVariableOriginalValue(%s)', $expr->getVariableName()); |
| 162: | } |
| 163: | |
| 164: | protected function pPHPStan_Node_OriginalForeachKeyExpr(OriginalForeachKeyExpr $expr): string |
| 165: | { |
| 166: | return sprintf('__phpstanOriginalForeachKey(%s)', $expr->getVariableName()); |
| 167: | } |
| 168: | |
| 169: | protected function pPHPStan_Node_OriginalForeachValueExpr(OriginalForeachValueExpr $expr): string |
| 170: | { |
| 171: | return sprintf('__phpstanOriginalForeachValue(%s)', $expr->getVariableName()); |
| 172: | } |
| 173: | |
| 174: | protected function pPHPStan_Node_IntertwinedVariableByReferenceWithExpr(IntertwinedVariableByReferenceWithExpr $expr): string |
| 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 |
| 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 |
| 185: | { |
| 186: | return sprintf('__phpstanOffsetWriteBase(%s)', $this->p($expr->getDimFetch())); |
| 187: | } |
| 188: | |
| 189: | protected function pPHPStan_Node_IssetExpr(IssetExpr $expr): string |
| 190: | { |
| 191: | return sprintf('__phpstanIssetExpr(%s)', $this->p($expr->getExpr())); |
| 192: | } |
| 193: | |
| 194: | protected function pPHPStan_Node_BooleanOrNode(BooleanOrNode $expr): string |
| 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 |
| 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 |
| 205: | { |
| 206: | return sprintf('__phpstanFunctionCallable(%s)', $this->p($expr->getOriginalNode())); |
| 207: | } |
| 208: | |
| 209: | protected function pPHPStan_Node_MethodCallableNode(MethodCallableNode $expr): string |
| 210: | { |
| 211: | return sprintf('__phpstanMethodCallable(%s)', $this->p($expr->getOriginalNode())); |
| 212: | } |
| 213: | |
| 214: | protected function pPHPStan_Node_StaticMethodCallableNode(StaticMethodCallableNode $expr): string |
| 215: | { |
| 216: | return sprintf('__phpstanStaticMethodCallable(%s)', $this->p($expr->getOriginalNode())); |
| 217: | } |
| 218: | |
| 219: | protected function pPHPStan_Node_InstantiationCallableNode(InstantiationCallableNode $expr): string |
| 220: | { |
| 221: | return sprintf('__phpstanInstantiationCallable(%s)', $this->p($expr->getOriginalNode())); |
| 222: | } |
| 223: | |
| 224: | } |
| 225: | |