private static function mergeGenerics()

in src/api-gen/DataMerger.php [237:296]


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

    $grouped = Dict\group_by(
      Vec\concat($a, $b),
      $generic ==> self::normalizeNameForMerge($generic->getName()),
    );

    $generics = vec[];
    foreach ($grouped as $for_type) {
      $name = C\firstx($for_type)->getName();
      $variance = VarianceToken::INVARIANT;
      foreach ($for_type as $generic) {
        if (Str\starts_with($generic->getName(), "HH\\")) {
          $name = $generic->getName();
        }
        if ($generic->isContravariant()) {
          $variance = VarianceToken::CONTRAVARIANT;
        } else if ($generic->isCovariant()) {
          $variance = VarianceToken::COVARIANT;
        }
      }
      $constraints = $for_type
        |> Vec\map($$, $g ==> $g->getConstraints())
        |> Vec\flatten($$)
        |> Dict\group_by(
          $$,
          $c ==> self::normalizeNameForMerge($c['type']->getTypeText()),
        )
        |> Vec\map(
          $$,
          $constraints_for_type ==> {
            $type = C\firstx($constraints_for_type)['type'];
            foreach ($constraints_for_type as $constraint) {
              if (Str\starts_with($constraint['type']->getTypeText(), "HH\\")) {
                $type = $constraint['type'];
                break;
              }
            }
            return $constraints_for_type
              |> Keyset\map($$, $c ==> $c['relationship'])
              |> Vec\map(
                $$,
                $r ==> shape('type' => $type, 'relationship' => $r),
              );
          },
        )
        |> Vec\flatten($$);
      $generics[] = new ScannedGeneric($name, $variance, $constraints);
    }
    return $generics;
  }