private static function mergeTypehintLists()

in src/api-gen/DataMerger.php [298:354]


  private static function mergeTypehintLists(
    vec<ScannedTypehint> $a,
    vec<ScannedTypehint> $b,
  ): vec<ScannedTypehint> {
    if (C\is_empty($a)) {
      return $b;
    }
    if (C\is_empty($b)) {
      return $a;
    }

    return Vec\concat($a, $b)
      |> Dict\group_by(
        $$,
        $th ==> self::normalizeNameForMerge($th->getTypeName()),
      )
      |> Vec\map(
        $$,
        $typehints ==> {
          $first = C\firstx($typehints);
          if (C\count($typehints) === 1) {
            return $first;
          }
          $rest = Vec\drop($typehints, 1);

          $generics = $first->getGenericTypes();
          $kind = $first->getKind();
          $name = $first->getTypeName();
          $nullable = $first->isNullable();
          $shape_fields = $first->isShape() ? $first->getShapeFields() : null;
          $function_typehints = $first->getFunctionTypehints();

          foreach ($rest as $th) {
            $name = self::mergeNames($name, $th->getTypeName());
            $kind ??= $th->getKind();
            $nullable = $nullable || $th->isNullable();
            $generics = self::mergeTypehintLists(
              $generics,
              $th->getGenericTypes(),
            );
            if ($th->isShape()) {
              $shape_fields = $th->getShapeFields();
            }
            $function_typehints ??= $th->getFunctionTypehints();
          }
          return new ScannedTypehint(
            $first->getAST(),
            $kind,
            $name,
            $generics,
            $name !== 'mixed' && $nullable,
            $shape_fields,
            $function_typehints,
          );
        },
      );
  }