Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

NoFragmentCycles::detectCycleRecursiveprotectedWC 1.0

Method of the class: NoFragmentCycles{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->detectCycleRecursive( $fragment, $context ): void;
$fragment(FragmentDefinitionNode) (required)
.
$context(QueryValidationContext) (required)
.

NoFragmentCycles::detectCycleRecursive() code WC 10.9.1

protected function detectCycleRecursive(FragmentDefinitionNode $fragment, QueryValidationContext $context): void
{
    if (isset($this->visitedFrags[$fragment->name->value])) {
        return;
    }

    $fragmentName = $fragment->name->value;
    $this->visitedFrags[$fragmentName] = true;

    $spreadNodes = $context->getFragmentSpreads($fragment);

    if ($spreadNodes === []) {
        return;
    }

    $this->spreadPathIndexByName[$fragmentName] = count($this->spreadPath);

    foreach ($spreadNodes as $spreadNode) {
        $spreadName = $spreadNode->name->value;
        $cycleIndex = $this->spreadPathIndexByName[$spreadName] ?? null;

        $this->spreadPath[] = $spreadNode;
        if ($cycleIndex === null) {
            $spreadFragment = $context->getFragment($spreadName);
            if ($spreadFragment !== null) {
                $this->detectCycleRecursive($spreadFragment, $context);
            }
        } else {
            $cyclePath = array_slice($this->spreadPath, $cycleIndex);
            $fragmentNames = [];
            foreach (array_slice($cyclePath, 0, -1) as $frag) {
                $fragmentNames[] = $frag->name->value;
            }

            $context->reportError(new Error(
                static::cycleErrorMessage($spreadName, $fragmentNames),
                $cyclePath
            ));
        }

        array_pop($this->spreadPath);
    }

    $this->spreadPathIndexByName[$fragmentName] = null;
}