Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

OverlappingFieldsCanBeMerged::collectConflictsWithinprotectedWC 1.0

Collect all Conflicts "within" one collection of fields.

Method of the class: OverlappingFieldsCanBeMerged{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->collectConflictsWithin( $context, $conflicts, $fieldMap ): void;
$context(QueryValidationContext) (required)
.
$conflicts(array) (required)
.
$fieldMap(array) (required)
.

OverlappingFieldsCanBeMerged::collectConflictsWithin() code WC 10.9.1

protected function collectConflictsWithin(
    QueryValidationContext $context,
    array &$conflicts,
    array $fieldMap
): void {
    // A field map is a keyed collection, where each key represents a response
    // name and the value at that key is a list of all fields which provide that
    // response name. For every response name, if there are multiple fields, they
    // must be compared to find a potential conflict.
    foreach ($fieldMap as $responseName => $fields) {
        // This compares every field in the list to every other field in this list
        // (except to itself). If the list only has one item, nothing needs to
        // be compared.
        $fieldsLength = count($fields);
        if ($fieldsLength <= 1) {
            continue;
        }

        // Deduplicate structurally identical fields to avoid O(n²) blowup
        // when a query repeats the same field many times.
        $fields = $this->deduplicateFields($fields);
        $fieldsLength = count($fields);
        if ($fieldsLength <= 1) {
            continue;
        }

        for ($i = 0; $i < $fieldsLength; ++$i) {
            for ($j = $i + 1; $j < $fieldsLength; ++$j) {
                $conflict = $this->findConflict(
                    $context,
                    false, // within one collection is never mutually exclusive
                    $responseName,
                    $fields[$i],
                    $fields[$j]
                );
                if ($conflict !== null) {
                    $conflicts[] = $conflict;
                }
            }
        }
    }
}