| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\PhpDocParser\Printer; |
| 4: | |
| 5: | use LogicException; |
| 6: | use PHPStan\PhpDocParser\Ast\Attribute; |
| 7: | use PHPStan\PhpDocParser\Ast\Comment; |
| 8: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode; |
| 9: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode; |
| 10: | use PHPStan\PhpDocParser\Ast\Node; |
| 11: | use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode; |
| 12: | use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagPropertyValueNode; |
| 13: | use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode; |
| 14: | use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineAnnotation; |
| 15: | use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArgument; |
| 16: | use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArray; |
| 17: | use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArrayItem; |
| 18: | use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineTagValueNode; |
| 19: | use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode; |
| 20: | use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode; |
| 21: | use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode; |
| 22: | use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueParameterNode; |
| 23: | use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode; |
| 24: | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamClosureThisTagValueNode; |
| 25: | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamImmediatelyInvokedCallableTagValueNode; |
| 26: | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamLaterInvokedCallableTagValueNode; |
| 27: | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamOutTagValueNode; |
| 28: | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 29: | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode; |
| 30: | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; |
| 31: | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 32: | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; |
| 33: | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; |
| 34: | use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode; |
| 35: | use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode; |
| 36: | use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode; |
| 37: | use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode; |
| 38: | use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode; |
| 39: | use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 40: | use PHPStan\PhpDocParser\Ast\PhpDoc\SealedTagValueNode; |
| 41: | use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode; |
| 42: | use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode; |
| 43: | use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode; |
| 44: | use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode; |
| 45: | use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode; |
| 46: | use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode; |
| 47: | use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 48: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode; |
| 49: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; |
| 50: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeUnsealedTypeNode; |
| 51: | use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; |
| 52: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; |
| 53: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode; |
| 54: | use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode; |
| 55: | use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode; |
| 56: | use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode; |
| 57: | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
| 58: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 59: | use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; |
| 60: | use PHPStan\PhpDocParser\Ast\Type\InvalidTypeNode; |
| 61: | use PHPStan\PhpDocParser\Ast\Type\NameNode; |
| 62: | use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; |
| 63: | use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode; |
| 64: | use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; |
| 65: | use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; |
| 66: | use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; |
| 67: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 68: | use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 69: | use PHPStan\PhpDocParser\Lexer\Lexer; |
| 70: | use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 71: | use function array_keys; |
| 72: | use function array_map; |
| 73: | use function array_values; |
| 74: | use function assert; |
| 75: | use function count; |
| 76: | use function get_class; |
| 77: | use function get_object_vars; |
| 78: | use function implode; |
| 79: | use function in_array; |
| 80: | use function is_array; |
| 81: | use function preg_match; |
| 82: | use function preg_match_all; |
| 83: | use function preg_replace_callback; |
| 84: | use function sprintf; |
| 85: | use function str_replace; |
| 86: | use function strlen; |
| 87: | use function strpos; |
| 88: | use function substr; |
| 89: | use function trim; |
| 90: | use const PREG_SET_ORDER; |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | final class Printer |
| 99: | { |
| 100: | |
| 101: | |
| 102: | private Differ $differ; |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | private string $lineStart = "\n *"; |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | private array $listInsertionMap = [ |
| 117: | PhpDocNode::class . '->children' => "\n * ", |
| 118: | UnionTypeNode::class . '->types' => '|', |
| 119: | IntersectionTypeNode::class . '->types' => '&', |
| 120: | ArrayShapeNode::class . '->items' => ', ', |
| 121: | ObjectShapeNode::class . '->items' => ', ', |
| 122: | CallableTypeNode::class . '->parameters' => ', ', |
| 123: | CallableTypeNode::class . '->templateTypes' => ', ', |
| 124: | GenericTypeNode::class . '->genericTypes' => ', ', |
| 125: | ConstExprArrayNode::class . '->items' => ', ', |
| 126: | MethodTagValueNode::class . '->parameters' => ', ', |
| 127: | DoctrineArray::class . '->items' => ', ', |
| 128: | DoctrineAnnotation::class . '->arguments' => ', ', |
| 129: | ]; |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | private array $emptyListInsertionMap = [ |
| 137: | CallableTypeNode::class . '->parameters' => ['(', '', ''], |
| 138: | ArrayShapeNode::class . '->items' => ['{', '', ''], |
| 139: | ObjectShapeNode::class . '->items' => ['{', '', ''], |
| 140: | DoctrineArray::class . '->items' => ['{', '', ''], |
| 141: | DoctrineAnnotation::class . '->arguments' => ['(', '', ''], |
| 142: | ]; |
| 143: | |
| 144: | |
| 145: | private array $parenthesesMap = [ |
| 146: | CallableTypeNode::class . '->returnType' => [ |
| 147: | CallableTypeNode::class, |
| 148: | UnionTypeNode::class, |
| 149: | IntersectionTypeNode::class, |
| 150: | ], |
| 151: | ArrayTypeNode::class . '->type' => [ |
| 152: | CallableTypeNode::class, |
| 153: | UnionTypeNode::class, |
| 154: | IntersectionTypeNode::class, |
| 155: | ConstTypeNode::class, |
| 156: | NullableTypeNode::class, |
| 157: | ], |
| 158: | OffsetAccessTypeNode::class . '->type' => [ |
| 159: | CallableTypeNode::class, |
| 160: | UnionTypeNode::class, |
| 161: | IntersectionTypeNode::class, |
| 162: | NullableTypeNode::class, |
| 163: | ], |
| 164: | NullableTypeNode::class . '->type' => [ |
| 165: | UnionTypeNode::class, |
| 166: | IntersectionTypeNode::class, |
| 167: | NullableTypeNode::class, |
| 168: | ], |
| 169: | ConditionalTypeNode::class . '->subjectType' => [ |
| 170: | UnionTypeNode::class, |
| 171: | IntersectionTypeNode::class, |
| 172: | NullableTypeNode::class, |
| 173: | ], |
| 174: | ]; |
| 175: | |
| 176: | |
| 177: | private array $parenthesesListMap = [ |
| 178: | IntersectionTypeNode::class . '->types' => [ |
| 179: | IntersectionTypeNode::class, |
| 180: | UnionTypeNode::class, |
| 181: | NullableTypeNode::class, |
| 182: | ], |
| 183: | UnionTypeNode::class . '->types' => [ |
| 184: | IntersectionTypeNode::class, |
| 185: | UnionTypeNode::class, |
| 186: | NullableTypeNode::class, |
| 187: | ], |
| 188: | ]; |
| 189: | |
| 190: | public function printFormatPreserving(PhpDocNode $node, PhpDocNode $originalNode, TokenIterator $originalTokens): string |
| 191: | { |
| 192: | $lineStart = $this->lineStart; |
| 193: | $this->lineStart = $this->detectLineStart($originalTokens); |
| 194: | try { |
| 195: | return $this->printFormatPreservingWithLineStart($node, $originalNode, $originalTokens); |
| 196: | } finally { |
| 197: | $this->lineStart = $lineStart; |
| 198: | } |
| 199: | } |
| 200: | |
| 201: | private function printFormatPreservingWithLineStart(PhpDocNode $node, PhpDocNode $originalNode, TokenIterator $originalTokens): string |
| 202: | { |
| 203: | $this->differ = new Differ(static function ($a, $b) { |
| 204: | if ($a instanceof Node && $b instanceof Node) { |
| 205: | return $a === $b->getAttribute(Attribute::ORIGINAL_NODE); |
| 206: | } |
| 207: | |
| 208: | return false; |
| 209: | }); |
| 210: | |
| 211: | $tokenIndex = 0; |
| 212: | $result = $this->printArrayFormatPreserving( |
| 213: | $node->children, |
| 214: | $originalNode->children, |
| 215: | $originalTokens, |
| 216: | $tokenIndex, |
| 217: | PhpDocNode::class, |
| 218: | 'children', |
| 219: | ); |
| 220: | if ($result !== null) { |
| 221: | return $result . $originalTokens->getContentBetween($tokenIndex, $originalTokens->getTokenCount()); |
| 222: | } |
| 223: | |
| 224: | return $this->print($node); |
| 225: | } |
| 226: | |
| 227: | public function print(Node $node): string |
| 228: | { |
| 229: | if ($node instanceof PhpDocNode) { |
| 230: | return "/**\n *" . implode("\n *", array_map( |
| 231: | function (PhpDocChildNode $child): string { |
| 232: | $s = $this->addLineStarts($this->print($child)); |
| 233: | return $s === '' ? '' : ' ' . $s; |
| 234: | }, |
| 235: | $node->children, |
| 236: | )) . "\n */"; |
| 237: | } |
| 238: | if ($node instanceof PhpDocTextNode) { |
| 239: | return $node->text; |
| 240: | } |
| 241: | if ($node instanceof PhpDocTagNode) { |
| 242: | if ($node->value instanceof DoctrineTagValueNode) { |
| 243: | return $this->print($node->value); |
| 244: | } |
| 245: | |
| 246: | return trim(sprintf('%s %s', $node->name, $this->print($node->value))); |
| 247: | } |
| 248: | if ($node instanceof PhpDocTagValueNode) { |
| 249: | return $this->printTagValue($node); |
| 250: | } |
| 251: | if ($node instanceof TypeNode) { |
| 252: | return $this->printType($node); |
| 253: | } |
| 254: | if ($node instanceof ConstExprNode) { |
| 255: | return $this->printConstExpr($node); |
| 256: | } |
| 257: | if ($node instanceof MethodTagValueParameterNode) { |
| 258: | $type = $node->type !== null ? $this->print($node->type) . ' ' : ''; |
| 259: | $isReference = $node->isReference ? '&' : ''; |
| 260: | $isVariadic = $node->isVariadic ? '...' : ''; |
| 261: | $default = $node->defaultValue !== null ? ' = ' . $this->print($node->defaultValue) : ''; |
| 262: | return "{$type}{$isReference}{$isVariadic}{$node->parameterName}{$default}"; |
| 263: | } |
| 264: | if ($node instanceof CallableTypeParameterNode) { |
| 265: | $type = $this->print($node->type) . ' '; |
| 266: | $isReference = $node->isReference ? '&' : ''; |
| 267: | $isVariadic = $node->isVariadic ? '...' : ''; |
| 268: | $isOptional = $node->isOptional ? '=' : ''; |
| 269: | return trim("{$type}{$isReference}{$isVariadic}{$node->parameterName}") . $isOptional; |
| 270: | } |
| 271: | if ($node instanceof ArrayShapeUnsealedTypeNode) { |
| 272: | if ($node->keyType !== null) { |
| 273: | return sprintf('<%s, %s>', $this->printType($node->keyType), $this->printType($node->valueType)); |
| 274: | } |
| 275: | return sprintf('<%s>', $this->printType($node->valueType)); |
| 276: | } |
| 277: | if ($node instanceof DoctrineAnnotation) { |
| 278: | return (string) $node; |
| 279: | } |
| 280: | if ($node instanceof DoctrineArgument) { |
| 281: | return (string) $node; |
| 282: | } |
| 283: | if ($node instanceof DoctrineArray) { |
| 284: | return (string) $node; |
| 285: | } |
| 286: | if ($node instanceof DoctrineArrayItem) { |
| 287: | return (string) $node; |
| 288: | } |
| 289: | if ($node instanceof ArrayShapeItemNode) { |
| 290: | if ($node->keyName !== null) { |
| 291: | return sprintf( |
| 292: | '%s%s: %s', |
| 293: | $this->print($node->keyName), |
| 294: | $node->optional ? '?' : '', |
| 295: | $this->printType($node->valueType), |
| 296: | ); |
| 297: | } |
| 298: | |
| 299: | return $this->printType($node->valueType); |
| 300: | } |
| 301: | if ($node instanceof ObjectShapeItemNode) { |
| 302: | if ($node->keyName !== null) { |
| 303: | return sprintf( |
| 304: | '%s%s: %s', |
| 305: | $this->print($node->keyName), |
| 306: | $node->optional ? '?' : '', |
| 307: | $this->printType($node->valueType), |
| 308: | ); |
| 309: | } |
| 310: | |
| 311: | return $this->printType($node->valueType); |
| 312: | } |
| 313: | |
| 314: | throw new LogicException(sprintf('Unknown node type %s', get_class($node))); |
| 315: | } |
| 316: | |
| 317: | private function printTagValue(PhpDocTagValueNode $node): string |
| 318: | { |
| 319: | |
| 320: | |
| 321: | |
| 322: | if ($node instanceof AssertTagMethodValueNode) { |
| 323: | $isNegated = $node->isNegated ? '!' : ''; |
| 324: | $isEquality = $node->isEquality ? '=' : ''; |
| 325: | $type = $this->printType($node->type); |
| 326: | return trim("{$isNegated}{$isEquality}{$type} {$node->parameter}->{$node->method}() {$node->description}"); |
| 327: | } |
| 328: | if ($node instanceof AssertTagPropertyValueNode) { |
| 329: | $isNegated = $node->isNegated ? '!' : ''; |
| 330: | $isEquality = $node->isEquality ? '=' : ''; |
| 331: | $type = $this->printType($node->type); |
| 332: | return trim("{$isNegated}{$isEquality}{$type} {$node->parameter}->{$node->property} {$node->description}"); |
| 333: | } |
| 334: | if ($node instanceof AssertTagValueNode) { |
| 335: | $isNegated = $node->isNegated ? '!' : ''; |
| 336: | $isEquality = $node->isEquality ? '=' : ''; |
| 337: | $type = $this->printType($node->type); |
| 338: | return trim("{$isNegated}{$isEquality}{$type} {$node->parameter} {$node->description}"); |
| 339: | } |
| 340: | if ($node instanceof ExtendsTagValueNode || $node instanceof ImplementsTagValueNode) { |
| 341: | $type = $this->printType($node->type); |
| 342: | return trim("{$type} {$node->description}"); |
| 343: | } |
| 344: | if ($node instanceof MethodTagValueNode) { |
| 345: | $static = $node->isStatic ? 'static ' : ''; |
| 346: | $returnType = $node->returnType !== null ? $this->printType($node->returnType) . ' ' : ''; |
| 347: | $parameters = implode(', ', array_map(fn (MethodTagValueParameterNode $parameter): string => $this->print($parameter), $node->parameters)); |
| 348: | $description = $node->description !== '' ? " {$node->description}" : ''; |
| 349: | $templateTypes = count($node->templateTypes) > 0 ? '<' . implode(', ', array_map(fn (TemplateTagValueNode $templateTag): string => $this->print($templateTag), $node->templateTypes)) . '>' : ''; |
| 350: | return "{$static}{$returnType}{$node->methodName}{$templateTypes}({$parameters}){$description}"; |
| 351: | } |
| 352: | if ($node instanceof MixinTagValueNode) { |
| 353: | $type = $this->printType($node->type); |
| 354: | return trim("{$type} {$node->description}"); |
| 355: | } |
| 356: | if ($node instanceof RequireExtendsTagValueNode) { |
| 357: | $type = $this->printType($node->type); |
| 358: | return trim("{$type} {$node->description}"); |
| 359: | } |
| 360: | if ($node instanceof RequireImplementsTagValueNode) { |
| 361: | $type = $this->printType($node->type); |
| 362: | return trim("{$type} {$node->description}"); |
| 363: | } |
| 364: | if ($node instanceof SealedTagValueNode) { |
| 365: | $type = $this->printType($node->type); |
| 366: | return trim("{$type} {$node->description}"); |
| 367: | } |
| 368: | if ($node instanceof ParamOutTagValueNode) { |
| 369: | $type = $this->printType($node->type); |
| 370: | return trim("{$type} {$node->parameterName} {$node->description}"); |
| 371: | } |
| 372: | if ($node instanceof ParamTagValueNode) { |
| 373: | $reference = $node->isReference ? '&' : ''; |
| 374: | $variadic = $node->isVariadic ? '...' : ''; |
| 375: | $type = $this->printType($node->type); |
| 376: | return trim("{$type} {$reference}{$variadic}{$node->parameterName} {$node->description}"); |
| 377: | } |
| 378: | if ($node instanceof ParamImmediatelyInvokedCallableTagValueNode) { |
| 379: | return trim("{$node->parameterName} {$node->description}"); |
| 380: | } |
| 381: | if ($node instanceof ParamLaterInvokedCallableTagValueNode) { |
| 382: | return trim("{$node->parameterName} {$node->description}"); |
| 383: | } |
| 384: | if ($node instanceof ParamClosureThisTagValueNode) { |
| 385: | return trim("{$node->type} {$node->parameterName} {$node->description}"); |
| 386: | } |
| 387: | if ($node instanceof PureUnlessCallableIsImpureTagValueNode) { |
| 388: | return trim("{$node->parameterName} {$node->description}"); |
| 389: | } |
| 390: | if ($node instanceof PureUnlessParameterIsPassedTagValueNode) { |
| 391: | return trim("{$node->parameterName} {$node->description}"); |
| 392: | } |
| 393: | if ($node instanceof PropertyTagValueNode) { |
| 394: | $type = $this->printType($node->type); |
| 395: | return trim("{$type} {$node->propertyName} {$node->description}"); |
| 396: | } |
| 397: | if ($node instanceof ReturnTagValueNode) { |
| 398: | $type = $this->printType($node->type); |
| 399: | return trim("{$type} {$node->description}"); |
| 400: | } |
| 401: | if ($node instanceof SelfOutTagValueNode) { |
| 402: | $type = $this->printType($node->type); |
| 403: | return trim($type . ' ' . $node->description); |
| 404: | } |
| 405: | if ($node instanceof TemplateTagValueNode) { |
| 406: | $upperBound = $node->bound !== null ? ' of ' . $this->printType($node->bound) : ''; |
| 407: | $lowerBound = $node->lowerBound !== null ? ' super ' . $this->printType($node->lowerBound) : ''; |
| 408: | $default = $node->default !== null ? ' = ' . $this->printType($node->default) : ''; |
| 409: | return trim("{$node->name}{$upperBound}{$lowerBound}{$default} {$node->description}"); |
| 410: | } |
| 411: | if ($node instanceof ThrowsTagValueNode) { |
| 412: | $type = $this->printType($node->type); |
| 413: | return trim("{$type} {$node->description}"); |
| 414: | } |
| 415: | if ($node instanceof TypeAliasImportTagValueNode) { |
| 416: | return trim( |
| 417: | "{$node->importedAlias} from " . $this->printType($node->importedFrom) |
| 418: | . ($node->importedAs !== null ? " as {$node->importedAs}" : ''), |
| 419: | ); |
| 420: | } |
| 421: | if ($node instanceof TypeAliasTagValueNode) { |
| 422: | $type = $this->printType($node->type); |
| 423: | return trim("{$node->alias} {$type}"); |
| 424: | } |
| 425: | if ($node instanceof UsesTagValueNode) { |
| 426: | $type = $this->printType($node->type); |
| 427: | return trim("{$type} {$node->description}"); |
| 428: | } |
| 429: | if ($node instanceof VarTagValueNode) { |
| 430: | $type = $this->printType($node->type); |
| 431: | return trim("{$type} " . trim("{$node->variableName} {$node->description}")); |
| 432: | } |
| 433: | |
| 434: | return (string) $node; |
| 435: | } |
| 436: | |
| 437: | private function printType(TypeNode $node): string |
| 438: | { |
| 439: | if ($node instanceof ArrayShapeNode) { |
| 440: | $items = array_map(fn (ArrayShapeItemNode $item): string => $this->print($item), $node->items); |
| 441: | |
| 442: | if (! $node->sealed) { |
| 443: | $items[] = '...' . ($node->unsealedType === null ? '' : $this->print($node->unsealedType)); |
| 444: | } |
| 445: | |
| 446: | return $node->kind . '{' . implode(', ', $items) . '}'; |
| 447: | } |
| 448: | if ($node instanceof ArrayTypeNode) { |
| 449: | return $this->printOffsetAccessType($node->type) . '[]'; |
| 450: | } |
| 451: | if ($node instanceof CallableTypeNode) { |
| 452: | if ($node->returnType instanceof CallableTypeNode || $node->returnType instanceof UnionTypeNode || $node->returnType instanceof IntersectionTypeNode) { |
| 453: | $returnType = $this->wrapInParentheses($node->returnType); |
| 454: | } else { |
| 455: | $returnType = $this->printType($node->returnType); |
| 456: | } |
| 457: | $template = $node->templateTypes !== [] |
| 458: | ? '<' . implode(', ', array_map(fn (TemplateTagValueNode $templateNode): string => $this->print($templateNode), $node->templateTypes)) . '>' |
| 459: | : ''; |
| 460: | $parameters = implode(', ', array_map(fn (CallableTypeParameterNode $parameterNode): string => $this->print($parameterNode), $node->parameters)); |
| 461: | return $this->printType($node->identifier) . "{$template}({$parameters}): {$returnType}"; |
| 462: | } |
| 463: | if ($node instanceof ConditionalTypeForParameterNode) { |
| 464: | return sprintf( |
| 465: | '(%s %s %s ? %s : %s)', |
| 466: | $node->parameterName, |
| 467: | $node->negated ? 'is not' : 'is', |
| 468: | $this->printType($node->targetType), |
| 469: | $this->printType($node->if), |
| 470: | $this->printType($node->else), |
| 471: | ); |
| 472: | } |
| 473: | if ($node instanceof ConditionalTypeNode) { |
| 474: | return sprintf( |
| 475: | '(%s %s %s ? %s : %s)', |
| 476: | $this->printConditionalSubjectType($node->subjectType), |
| 477: | $node->negated ? 'is not' : 'is', |
| 478: | $this->printType($node->targetType), |
| 479: | $this->printType($node->if), |
| 480: | $this->printType($node->else), |
| 481: | ); |
| 482: | } |
| 483: | if ($node instanceof ConstTypeNode) { |
| 484: | return $this->printConstExpr($node->constExpr); |
| 485: | } |
| 486: | if ($node instanceof GenericTypeNode) { |
| 487: | $genericTypes = []; |
| 488: | |
| 489: | foreach ($node->genericTypes as $index => $type) { |
| 490: | $variance = $node->variances[$index] ?? GenericTypeNode::VARIANCE_INVARIANT; |
| 491: | if ($variance === GenericTypeNode::VARIANCE_INVARIANT) { |
| 492: | $genericTypes[] = $this->printType($type); |
| 493: | } elseif ($variance === GenericTypeNode::VARIANCE_BIVARIANT) { |
| 494: | $genericTypes[] = '*'; |
| 495: | } else { |
| 496: | $genericTypes[] = sprintf('%s %s', $variance, $this->print($type)); |
| 497: | } |
| 498: | } |
| 499: | |
| 500: | return $this->printType($node->type) . '<' . implode(', ', $genericTypes) . '>'; |
| 501: | } |
| 502: | if ($node instanceof IdentifierTypeNode) { |
| 503: | return $node->name; |
| 504: | } |
| 505: | if ($node instanceof NameNode) { |
| 506: | return $node->toCodeString(); |
| 507: | } |
| 508: | if ($node instanceof IntersectionTypeNode || $node instanceof UnionTypeNode) { |
| 509: | $items = []; |
| 510: | foreach ($node->types as $type) { |
| 511: | if ( |
| 512: | $type instanceof IntersectionTypeNode |
| 513: | || $type instanceof UnionTypeNode |
| 514: | || $type instanceof NullableTypeNode |
| 515: | ) { |
| 516: | $items[] = $this->wrapInParentheses($type); |
| 517: | continue; |
| 518: | } |
| 519: | |
| 520: | $items[] = $this->printType($type); |
| 521: | } |
| 522: | |
| 523: | return implode($node instanceof IntersectionTypeNode ? '&' : '|', $items); |
| 524: | } |
| 525: | if ($node instanceof InvalidTypeNode) { |
| 526: | return (string) $node; |
| 527: | } |
| 528: | if ($node instanceof NullableTypeNode) { |
| 529: | if ( |
| 530: | $node->type instanceof IntersectionTypeNode |
| 531: | || $node->type instanceof UnionTypeNode |
| 532: | || $node->type instanceof NullableTypeNode |
| 533: | ) { |
| 534: | return '?(' . $this->printType($node->type) . ')'; |
| 535: | } |
| 536: | |
| 537: | return '?' . $this->printType($node->type); |
| 538: | } |
| 539: | if ($node instanceof ObjectShapeNode) { |
| 540: | $items = array_map(fn (ObjectShapeItemNode $item): string => $this->print($item), $node->items); |
| 541: | |
| 542: | return 'object{' . implode(', ', $items) . '}'; |
| 543: | } |
| 544: | if ($node instanceof OffsetAccessTypeNode) { |
| 545: | return $this->printOffsetAccessType($node->type) . '[' . $this->printType($node->offset) . ']'; |
| 546: | } |
| 547: | if ($node instanceof ThisTypeNode) { |
| 548: | return (string) $node; |
| 549: | } |
| 550: | |
| 551: | throw new LogicException(sprintf('Unknown node type %s', get_class($node))); |
| 552: | } |
| 553: | |
| 554: | private function wrapInParentheses(TypeNode $node): string |
| 555: | { |
| 556: | return '(' . $this->printType($node) . ')'; |
| 557: | } |
| 558: | |
| 559: | |
| 560: | |
| 561: | |
| 562: | |
| 563: | |
| 564: | |
| 565: | |
| 566: | |
| 567: | private function printConditionalSubjectType(TypeNode $type): string |
| 568: | { |
| 569: | if ( |
| 570: | $type instanceof UnionTypeNode |
| 571: | || $type instanceof IntersectionTypeNode |
| 572: | || $type instanceof NullableTypeNode |
| 573: | ) { |
| 574: | return $this->wrapInParentheses($type); |
| 575: | } |
| 576: | |
| 577: | return $this->printType($type); |
| 578: | } |
| 579: | |
| 580: | private function printOffsetAccessType(TypeNode $type): string |
| 581: | { |
| 582: | if ( |
| 583: | $type instanceof CallableTypeNode |
| 584: | || $type instanceof UnionTypeNode |
| 585: | || $type instanceof IntersectionTypeNode |
| 586: | || $type instanceof NullableTypeNode |
| 587: | ) { |
| 588: | return $this->wrapInParentheses($type); |
| 589: | } |
| 590: | |
| 591: | return $this->printType($type); |
| 592: | } |
| 593: | |
| 594: | private function printConstExpr(ConstExprNode $node): string |
| 595: | { |
| 596: | |
| 597: | return (string) $node; |
| 598: | } |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | |
| 604: | private function printArrayFormatPreserving(array $nodes, array $originalNodes, TokenIterator $originalTokens, int &$tokenIndex, string $parentNodeClass, string $subNodeName): ?string |
| 605: | { |
| 606: | |
| 607: | $nodes = array_values($nodes); |
| 608: | $originalNodes = array_values($originalNodes); |
| 609: | |
| 610: | $diff = $this->differ->diffWithReplacements($originalNodes, $nodes); |
| 611: | $mapKey = $parentNodeClass . '->' . $subNodeName; |
| 612: | $insertStr = $this->listInsertionMap[$mapKey] ?? null; |
| 613: | $result = ''; |
| 614: | $beforeFirstKeepOrReplace = true; |
| 615: | $skipRemovedNode = false; |
| 616: | $delayedAdd = []; |
| 617: | |
| 618: | $insertNewline = false; |
| 619: | [$isMultiline, $beforeAsteriskIndent, $afterAsteriskIndent] = $this->isMultiline($tokenIndex, $originalNodes, $originalTokens); |
| 620: | |
| 621: | if ($insertStr === "\n * ") { |
| 622: | $lastChildIndent = $this->detectLastChildIndent($originalNodes, $originalTokens); |
| 623: | if ($lastChildIndent !== null) { |
| 624: | [$beforeAsteriskIndent, $afterAsteriskIndent] = $lastChildIndent; |
| 625: | } |
| 626: | |
| 627: | $insertStr = sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 628: | } |
| 629: | |
| 630: | foreach ($diff as $i => $diffElem) { |
| 631: | $diffType = $diffElem->type; |
| 632: | $arrItem = $diffElem->new; |
| 633: | $origArrayItem = $diffElem->old; |
| 634: | if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) { |
| 635: | $beforeFirstKeepOrReplace = false; |
| 636: | if (!$arrItem instanceof Node || !$origArrayItem instanceof Node) { |
| 637: | return null; |
| 638: | } |
| 639: | |
| 640: | |
| 641: | $itemStartPos = $origArrayItem->getAttribute(Attribute::START_INDEX); |
| 642: | |
| 643: | |
| 644: | $itemEndPos = $origArrayItem->getAttribute(Attribute::END_INDEX); |
| 645: | |
| 646: | if ($itemStartPos < 0 || $itemEndPos < 0 || $itemStartPos < $tokenIndex) { |
| 647: | throw new LogicException(); |
| 648: | } |
| 649: | |
| 650: | $comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? []; |
| 651: | $origComments = $origArrayItem->getAttribute(Attribute::COMMENTS) ?? []; |
| 652: | |
| 653: | $commentStartPos = count($origComments) > 0 ? $origComments[0]->startIndex : $itemStartPos; |
| 654: | assert($commentStartPos >= 0); |
| 655: | |
| 656: | if ($skipRemovedNode) { |
| 657: | |
| 658: | $skipRemovedNode = false; |
| 659: | } else { |
| 660: | $result .= $originalTokens->getContentBetween($tokenIndex, $itemStartPos); |
| 661: | } |
| 662: | |
| 663: | if (count($delayedAdd) > 0) { |
| 664: | foreach ($delayedAdd as $delayedAddNode) { |
| 665: | $parenthesesNeeded = isset($this->parenthesesListMap[$mapKey]) |
| 666: | && in_array(get_class($delayedAddNode), $this->parenthesesListMap[$mapKey], true); |
| 667: | if ($parenthesesNeeded) { |
| 668: | $result .= '('; |
| 669: | } |
| 670: | |
| 671: | if ($insertNewline) { |
| 672: | $delayedAddComments = $delayedAddNode->getAttribute(Attribute::COMMENTS) ?? []; |
| 673: | if (count($delayedAddComments) > 0) { |
| 674: | $result .= $this->printComments($delayedAddComments, $beforeAsteriskIndent, $afterAsteriskIndent); |
| 675: | $result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 676: | } |
| 677: | } |
| 678: | |
| 679: | $result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent); |
| 680: | if ($parenthesesNeeded) { |
| 681: | $result .= ')'; |
| 682: | } |
| 683: | |
| 684: | if ($insertNewline) { |
| 685: | $result .= $insertStr . sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 686: | } else { |
| 687: | $result .= $insertStr; |
| 688: | } |
| 689: | } |
| 690: | |
| 691: | $delayedAdd = []; |
| 692: | } |
| 693: | |
| 694: | $parenthesesNeeded = isset($this->parenthesesListMap[$mapKey]) |
| 695: | && in_array(get_class($arrItem), $this->parenthesesListMap[$mapKey], true) |
| 696: | && !in_array(get_class($origArrayItem), $this->parenthesesListMap[$mapKey], true); |
| 697: | $addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($itemStartPos, $itemEndPos); |
| 698: | if ($addParentheses) { |
| 699: | $result .= '('; |
| 700: | } |
| 701: | |
| 702: | if ($comments !== $origComments) { |
| 703: | if (count($comments) > 0) { |
| 704: | $result .= $this->printComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent); |
| 705: | $result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 706: | } |
| 707: | } |
| 708: | |
| 709: | $result .= $this->printNodeFormatPreserving($arrItem, $originalTokens); |
| 710: | if ($addParentheses) { |
| 711: | $result .= ')'; |
| 712: | } |
| 713: | $tokenIndex = $itemEndPos + 1; |
| 714: | |
| 715: | } elseif ($diffType === DiffElem::TYPE_ADD) { |
| 716: | if ($insertStr === null) { |
| 717: | return null; |
| 718: | } |
| 719: | if (!$arrItem instanceof Node) { |
| 720: | return null; |
| 721: | } |
| 722: | |
| 723: | if ($insertStr === ', ' && $isMultiline || count($arrItem->getAttribute(Attribute::COMMENTS) ?? []) > 0) { |
| 724: | $insertStr = ','; |
| 725: | $insertNewline = true; |
| 726: | } |
| 727: | |
| 728: | if ($beforeFirstKeepOrReplace) { |
| 729: | |
| 730: | $delayedAdd[] = $arrItem; |
| 731: | continue; |
| 732: | } |
| 733: | |
| 734: | |
| 735: | $itemEndPos = $tokenIndex - 1; |
| 736: | if ($insertNewline) { |
| 737: | $comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? []; |
| 738: | $result .= $insertStr; |
| 739: | if (count($comments) > 0) { |
| 740: | $result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 741: | $result .= $this->printComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent); |
| 742: | } |
| 743: | $result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 744: | } else { |
| 745: | $result .= $insertStr; |
| 746: | } |
| 747: | |
| 748: | $parenthesesNeeded = isset($this->parenthesesListMap[$mapKey]) |
| 749: | && in_array(get_class($arrItem), $this->parenthesesListMap[$mapKey], true); |
| 750: | if ($parenthesesNeeded) { |
| 751: | $result .= '('; |
| 752: | } |
| 753: | |
| 754: | $result = $this->appendNode($result, $this->printNodeFormatPreserving($arrItem, $originalTokens), $afterAsteriskIndent); |
| 755: | if ($parenthesesNeeded) { |
| 756: | $result .= ')'; |
| 757: | } |
| 758: | |
| 759: | $tokenIndex = $itemEndPos + 1; |
| 760: | |
| 761: | } elseif ($diffType === DiffElem::TYPE_REMOVE) { |
| 762: | if (!$origArrayItem instanceof Node) { |
| 763: | return null; |
| 764: | } |
| 765: | |
| 766: | |
| 767: | $itemStartPos = $origArrayItem->getAttribute(Attribute::START_INDEX); |
| 768: | |
| 769: | |
| 770: | $itemEndPos = $origArrayItem->getAttribute(Attribute::END_INDEX); |
| 771: | if ($itemStartPos < 0 || $itemEndPos < 0) { |
| 772: | throw new LogicException(); |
| 773: | } |
| 774: | |
| 775: | if ($i === 0) { |
| 776: | |
| 777: | |
| 778: | if ($mapKey === PhpDocNode::class . '->children') { |
| 779: | |
| 780: | $originalTokensArray = $originalTokens->getTokens(); |
| 781: | for ($j = $tokenIndex; $j < $itemStartPos; $j++) { |
| 782: | if ($originalTokensArray[$j][Lexer::TYPE_OFFSET] === Lexer::TOKEN_PHPDOC_EOL) { |
| 783: | break; |
| 784: | } |
| 785: | $result .= $originalTokensArray[$j][Lexer::VALUE_OFFSET]; |
| 786: | } |
| 787: | } else { |
| 788: | $result .= $originalTokens->getContentBetween($tokenIndex, $itemStartPos); |
| 789: | $skipRemovedNode = true; |
| 790: | } |
| 791: | } |
| 792: | |
| 793: | $tokenIndex = $itemEndPos + 1; |
| 794: | } |
| 795: | } |
| 796: | |
| 797: | if (count($delayedAdd) > 0) { |
| 798: | if (!isset($this->emptyListInsertionMap[$mapKey])) { |
| 799: | return null; |
| 800: | } |
| 801: | |
| 802: | [$findToken, $extraLeft, $extraRight] = $this->emptyListInsertionMap[$mapKey]; |
| 803: | if ($findToken !== null) { |
| 804: | $originalTokensArray = $originalTokens->getTokens(); |
| 805: | for (; $tokenIndex < count($originalTokensArray); $tokenIndex++) { |
| 806: | $result .= $originalTokensArray[$tokenIndex][Lexer::VALUE_OFFSET]; |
| 807: | if ($originalTokensArray[$tokenIndex][Lexer::VALUE_OFFSET] !== $findToken) { |
| 808: | continue; |
| 809: | } |
| 810: | |
| 811: | $tokenIndex++; |
| 812: | break; |
| 813: | } |
| 814: | } |
| 815: | $first = true; |
| 816: | $result .= $extraLeft; |
| 817: | foreach ($delayedAdd as $delayedAddNode) { |
| 818: | if (!$first) { |
| 819: | $result .= $insertStr; |
| 820: | if ($insertNewline) { |
| 821: | $result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent); |
| 822: | } |
| 823: | } |
| 824: | |
| 825: | $result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent); |
| 826: | $first = false; |
| 827: | } |
| 828: | $result .= $extraRight; |
| 829: | } |
| 830: | |
| 831: | return $result; |
| 832: | } |
| 833: | |
| 834: | |
| 835: | |
| 836: | |
| 837: | private function appendNode(string $result, string $printedNode, string $afterAsteriskIndent): string |
| 838: | { |
| 839: | $lineStart = '*' . $afterAsteriskIndent; |
| 840: | if ($printedNode !== '' || $afterAsteriskIndent === '' || substr($result, -strlen($lineStart)) !== $lineStart) { |
| 841: | return $result . $printedNode; |
| 842: | } |
| 843: | |
| 844: | return substr($result, 0, -strlen($afterAsteriskIndent)); |
| 845: | } |
| 846: | |
| 847: | |
| 848: | |
| 849: | |
| 850: | private function printComments(array $comments, string $beforeAsteriskIndent, string $afterAsteriskIndent): string |
| 851: | { |
| 852: | $formattedComments = []; |
| 853: | |
| 854: | foreach ($comments as $comment) { |
| 855: | $formattedComments[] = str_replace("\n", "\n" . $beforeAsteriskIndent . '*' . $afterAsteriskIndent, $comment->getReformattedText()); |
| 856: | } |
| 857: | |
| 858: | return implode("\n$beforeAsteriskIndent*$afterAsteriskIndent", $formattedComments); |
| 859: | } |
| 860: | |
| 861: | |
| 862: | |
| 863: | |
| 864: | |
| 865: | private function isMultiline(int $initialIndex, array $nodes, TokenIterator $originalTokens): array |
| 866: | { |
| 867: | $isMultiline = count($nodes) > 1; |
| 868: | $pos = $initialIndex; |
| 869: | $allText = ''; |
| 870: | |
| 871: | foreach ($nodes as $node) { |
| 872: | if (!$node instanceof Node) { |
| 873: | continue; |
| 874: | } |
| 875: | |
| 876: | $endPos = $node->getAttribute(Attribute::END_INDEX) + 1; |
| 877: | $text = $originalTokens->getContentBetween($pos, $endPos); |
| 878: | $allText .= $text; |
| 879: | if (strpos($text, "\n") === false) { |
| 880: | |
| 881: | |
| 882: | |
| 883: | $isMultiline = false; |
| 884: | } |
| 885: | $pos = $endPos; |
| 886: | } |
| 887: | |
| 888: | $c = preg_match_all('~\n(?<before>[\\x09\\x20]*)\*(?<after>\\x20*)~', $allText, $matches, PREG_SET_ORDER); |
| 889: | if ($c === 0) { |
| 890: | return [$isMultiline, ' ', ' ']; |
| 891: | } |
| 892: | |
| 893: | $before = ''; |
| 894: | $after = ''; |
| 895: | foreach ($matches as $match) { |
| 896: | if (strlen($match['before']) > strlen($before)) { |
| 897: | $before = $match['before']; |
| 898: | } |
| 899: | if (strlen($match['after']) <= strlen($after)) { |
| 900: | continue; |
| 901: | } |
| 902: | |
| 903: | $after = $match['after']; |
| 904: | } |
| 905: | |
| 906: | $before = strlen($before) === 0 ? ' ' : $before; |
| 907: | $after = strlen($after) === 0 ? ' ' : $after; |
| 908: | |
| 909: | return [$isMultiline, $before, $after]; |
| 910: | } |
| 911: | |
| 912: | |
| 913: | |
| 914: | |
| 915: | |
| 916: | |
| 917: | |
| 918: | |
| 919: | private function detectLastChildIndent(array $originalNodes, TokenIterator $originalTokens): ?array |
| 920: | { |
| 921: | $tokens = $originalTokens->getTokens(); |
| 922: | for ($i = count($originalNodes) - 1; $i >= 0; $i--) { |
| 923: | $index = $originalNodes[$i]->getAttribute(Attribute::START_INDEX) - 1; |
| 924: | $lineStart = ''; |
| 925: | while (isset($tokens[$index]) && $tokens[$index][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) { |
| 926: | $lineStart = $tokens[$index][Lexer::VALUE_OFFSET] . $lineStart; |
| 927: | $index--; |
| 928: | } |
| 929: | |
| 930: | if (!isset($tokens[$index]) || $tokens[$index][Lexer::TYPE_OFFSET] !== Lexer::TOKEN_PHPDOC_EOL) { |
| 931: | continue; |
| 932: | } |
| 933: | |
| 934: | $lineStart = $tokens[$index][Lexer::VALUE_OFFSET] . $lineStart; |
| 935: | if (preg_match('~\n(?<before>[\x09\x20]*)\*(?<after>[\x09\x20]*)$~', $lineStart, $match) !== 1) { |
| 936: | continue; |
| 937: | } |
| 938: | |
| 939: | return [$match['before'], $match['after']]; |
| 940: | } |
| 941: | |
| 942: | return null; |
| 943: | } |
| 944: | |
| 945: | private function printNodeFormatPreserving(Node $node, TokenIterator $originalTokens): string |
| 946: | { |
| 947: | |
| 948: | $originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE); |
| 949: | if ($originalNode === null) { |
| 950: | return $this->printReplacement($node); |
| 951: | } |
| 952: | |
| 953: | $class = get_class($node); |
| 954: | if ($class !== get_class($originalNode)) { |
| 955: | throw new LogicException(); |
| 956: | } |
| 957: | |
| 958: | $startPos = $originalNode->getAttribute(Attribute::START_INDEX); |
| 959: | $endPos = $originalNode->getAttribute(Attribute::END_INDEX); |
| 960: | if ($startPos < 0 || $endPos < 0) { |
| 961: | throw new LogicException(); |
| 962: | } |
| 963: | |
| 964: | $result = ''; |
| 965: | $pos = $startPos; |
| 966: | $subNodeNames = array_keys(get_object_vars($node)); |
| 967: | foreach ($subNodeNames as $subNodeName) { |
| 968: | $subNode = $node->$subNodeName; |
| 969: | $origSubNode = $originalNode->$subNodeName; |
| 970: | |
| 971: | if ( |
| 972: | (!$subNode instanceof Node && $subNode !== null) |
| 973: | || (!$origSubNode instanceof Node && $origSubNode !== null) |
| 974: | ) { |
| 975: | if ($subNode === $origSubNode) { |
| 976: | |
| 977: | continue; |
| 978: | } |
| 979: | |
| 980: | if ($this->isRenamedTag($node, $subNodeName, $originalTokens, $startPos)) { |
| 981: | |
| 982: | $result .= $subNode; |
| 983: | $pos = $startPos + 1; |
| 984: | continue; |
| 985: | } |
| 986: | |
| 987: | if ( |
| 988: | $node instanceof DoctrineAnnotation |
| 989: | && $subNodeName === 'name' |
| 990: | && $node->getAttribute(Attribute::ORIGINAL_NODE)->name !== $originalTokens->getContentBetween($startPos, $startPos + 1) |
| 991: | ) { |
| 992: | |
| 993: | continue; |
| 994: | } |
| 995: | |
| 996: | if (is_array($subNode) && is_array($origSubNode)) { |
| 997: | |
| 998: | $listResult = $this->printArrayFormatPreserving( |
| 999: | $subNode, |
| 1000: | $origSubNode, |
| 1001: | $originalTokens, |
| 1002: | $pos, |
| 1003: | $class, |
| 1004: | $subNodeName, |
| 1005: | ); |
| 1006: | |
| 1007: | if ($listResult === null) { |
| 1008: | return $this->printReplacement($node); |
| 1009: | } |
| 1010: | |
| 1011: | $result .= $listResult; |
| 1012: | continue; |
| 1013: | } |
| 1014: | |
| 1015: | return $this->printReplacement($node); |
| 1016: | } |
| 1017: | |
| 1018: | if ($origSubNode === null) { |
| 1019: | if ($subNode === null) { |
| 1020: | |
| 1021: | continue; |
| 1022: | } |
| 1023: | |
| 1024: | return $this->printReplacement($node); |
| 1025: | } |
| 1026: | |
| 1027: | $subStartPos = $origSubNode->getAttribute(Attribute::START_INDEX); |
| 1028: | $subEndPos = $origSubNode->getAttribute(Attribute::END_INDEX); |
| 1029: | if ($subStartPos < 0 || $subEndPos < 0) { |
| 1030: | throw new LogicException(); |
| 1031: | } |
| 1032: | |
| 1033: | if ($subEndPos < $subStartPos) { |
| 1034: | |
| 1035: | if ($subNode !== null && $this->print($subNode) === $this->print($origSubNode)) { |
| 1036: | continue; |
| 1037: | } |
| 1038: | |
| 1039: | return $this->printReplacement($node); |
| 1040: | } |
| 1041: | |
| 1042: | if ($subNode === null) { |
| 1043: | return $this->printReplacement($node); |
| 1044: | } |
| 1045: | |
| 1046: | $result .= $originalTokens->getContentBetween($pos, $subStartPos); |
| 1047: | $mapKey = get_class($node) . '->' . $subNodeName; |
| 1048: | $parenthesesNeeded = isset($this->parenthesesMap[$mapKey]) |
| 1049: | && in_array(get_class($subNode), $this->parenthesesMap[$mapKey], true); |
| 1050: | |
| 1051: | if ($subNode->getAttribute(Attribute::ORIGINAL_NODE) !== null) { |
| 1052: | $parenthesesNeeded = $parenthesesNeeded |
| 1053: | && !in_array(get_class($subNode->getAttribute(Attribute::ORIGINAL_NODE)), $this->parenthesesMap[$mapKey], true); |
| 1054: | } |
| 1055: | |
| 1056: | $addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($subStartPos, $subEndPos); |
| 1057: | if ($addParentheses) { |
| 1058: | $result .= '('; |
| 1059: | } |
| 1060: | |
| 1061: | $result .= $this->printNodeFormatPreserving($subNode, $originalTokens); |
| 1062: | if ($addParentheses) { |
| 1063: | $result .= ')'; |
| 1064: | } |
| 1065: | |
| 1066: | $pos = $subEndPos + 1; |
| 1067: | } |
| 1068: | |
| 1069: | return $result . $originalTokens->getContentBetween($pos, $endPos + 1); |
| 1070: | } |
| 1071: | |
| 1072: | private function isRenamedTag(Node $node, string $subNodeName, TokenIterator $originalTokens, int $startPos): bool |
| 1073: | { |
| 1074: | if (!$node instanceof PhpDocTagNode || $subNodeName !== 'name') { |
| 1075: | return false; |
| 1076: | } |
| 1077: | |
| 1078: | $originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE); |
| 1079: | if (!$originalNode instanceof PhpDocTagNode) { |
| 1080: | return false; |
| 1081: | } |
| 1082: | |
| 1083: | return $originalTokens->getContentBetween($startPos, $startPos + 1) === $originalNode->name; |
| 1084: | } |
| 1085: | |
| 1086: | |
| 1087: | |
| 1088: | |
| 1089: | |
| 1090: | |
| 1091: | |
| 1092: | private function printReplacement(Node $node): string |
| 1093: | { |
| 1094: | if (!$node instanceof DoctrineTagValueNode) { |
| 1095: | return $this->addLineStarts($this->print($node)); |
| 1096: | } |
| 1097: | |
| 1098: | $arguments = '(' . implode(', ', $node->annotation->arguments) . ')'; |
| 1099: | if ($node->annotation->arguments === []) { |
| 1100: | $originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE); |
| 1101: | if ( |
| 1102: | $originalNode instanceof DoctrineTagValueNode |
| 1103: | && $originalNode->annotation->getAttribute(Attribute::END_INDEX) < $originalNode->annotation->getAttribute(Attribute::START_INDEX) |
| 1104: | ) { |
| 1105: | $arguments = ''; |
| 1106: | } |
| 1107: | } |
| 1108: | |
| 1109: | return $this->addLineStarts(trim($arguments . ' ' . $node->description)); |
| 1110: | } |
| 1111: | |
| 1112: | |
| 1113: | |
| 1114: | |
| 1115: | |
| 1116: | private function addLineStarts(string $s): string |
| 1117: | { |
| 1118: | return preg_replace_callback( |
| 1119: | '~\r?\n(?=(.?))~', |
| 1120: | fn (array $matches): string => $matches[1] === '' || $matches[1] === "\r" || $matches[1] === "\n" |
| 1121: | ? $this->lineStart |
| 1122: | : $this->lineStart . ' ', |
| 1123: | $s, |
| 1124: | ) ?? $s; |
| 1125: | } |
| 1126: | |
| 1127: | private function detectLineStart(TokenIterator $originalTokens): string |
| 1128: | { |
| 1129: | foreach ($originalTokens->getTokens() as $token) { |
| 1130: | if ($token[Lexer::TYPE_OFFSET] !== Lexer::TOKEN_PHPDOC_EOL) { |
| 1131: | continue; |
| 1132: | } |
| 1133: | |
| 1134: | $asteriskPosition = strpos($token[Lexer::VALUE_OFFSET], '*'); |
| 1135: | if ($asteriskPosition === false) { |
| 1136: | continue; |
| 1137: | } |
| 1138: | |
| 1139: | return substr($token[Lexer::VALUE_OFFSET], 0, $asteriskPosition + 1); |
| 1140: | } |
| 1141: | |
| 1142: | return ($originalTokens->getDetectedNewline() ?? "\n") . ' *'; |
| 1143: | } |
| 1144: | |
| 1145: | } |
| 1146: | |