public function getTypeText()

in src/definitions/ScannedTypehint.hack [81:162]


  public function getTypeText(
    string $relative_to_namespace = '',
    int /* TypeTextOptions */ $options = 0,
  ): string {
    $base = $this->isNullable() ? '?' : '';

    if ($this->shapeFields is nonnull) {
      return $base.
        self::getShapeTypeText(
          $relative_to_namespace,
          $options,
          $this->shapeFields,
        );
    } else if ($this->functionTypehints is nonnull) {
      return $base.
        self::getFunctionTypeText(
          $relative_to_namespace,
          $options,
          ...$this->functionTypehints
        );
    }

    $type_name = $this->typeName;

    invariant(
      \strpbrk($type_name, '<>') === false,
      'Typename "%s" contains <>, which should have been parsed and removed.',
      $type_name,
    );

    if ($this->kind is nonnull) {
      switch ($this->kind) {
        case ResolvedTypeKind::CALLABLE:
        case ResolvedTypeKind::GENERIC_PARAMETER:
          break;
        case ResolvedTypeKind::QUALIFIED_AUTOIMPORTED_TYPE:
          if ($options & TypeTextOptions::STRIP_AUTOIMPORTED_NAMESPACE) {
            $type_name = Str\strip_prefix($type_name, 'HH\\');
          }
          break;
        case ResolvedTypeKind::QUALIFIED_TYPE:
          if ($relative_to_namespace !== '') {
            if (Str\starts_with($type_name, $relative_to_namespace.'\\')) {
              $type_name = Str\strip_prefix(
                $type_name,
                $relative_to_namespace.'\\',
              );
            } else {
              $type_name = '\\'.$type_name;
            }
          }
          break;
      }
    }

    $sub = null;
    $generics = $this->getGenericTypes();
    if ($generics) {
      $sub = Vec\map(
        $generics,
        $g ==> $g->getTypeText($relative_to_namespace, $options),
      );
    }

    if ($type_name === 'tuple') {
      $sub = Str\join($sub as nonnull, ', ');
      $base .= '('.$sub.')';
    } else if ($type_name === self::UNION) {
      $sub = Str\join($sub as nonnull, ' | ');
      $base .= '('.$sub.')';
    } else if ($type_name === self::INTERSECTION) {
      $sub = Str\join($sub as nonnull, ' & ');
      $base .= '('.$sub.')';
    } else {
      $base .= $type_name;
      if ($sub is nonnull) {
        $sub = Str\join($sub, ', ');
        $base .= '<'.$sub.'>';
      }
    }
    return $base;
  }