private function splitSeparatedPair()

in agent/php/ElasticApm/Impl/HttpDistributedTracing.php [632:705]


    private function splitSeparatedPair(
        string $pair,
        string $separator,
        string $dbgFirstPartType,
        string &$firstPart,
        ?int $firstPartMaxLen,
        ?string $firstPartRegex,
        string $dbgSecondPartType,
        string &$secondPart,
        ?int $secondPartMaxLen,
        ?string $secondPartRegex
    ): bool {
        $localLogger = $this->logger->inherit();
        $localLogger->addContext('pair', $pair)->addContext('separator', $separator);

        $dbgPairType = $dbgFirstPartType . $separator . $dbgSecondPartType;
        $parts = explode($separator, $pair, /* limit: */ 3);
        if (count($parts) > 2) {
            ($loggerProxy = $localLogger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
            && $loggerProxy->log(
                $dbgPairType . ' string is invalid because separator appears more than once',
                ['parts' => $parts]
            );
            return false;
        }
        if (count($parts) < 2) {
            ($loggerProxy = $localLogger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
            && $loggerProxy->log(
                $dbgPairType . ' string is invalid because separator (' . $separator . ') is missing',
                ['parts' => $parts]
            );
            return false;
        }

        $dbgPartType = $dbgFirstPartType;
        $maxLen = $firstPartMaxLen;
        $regex = $firstPartRegex;
        foreach ($parts as $part) {
            if (TextUtil::isEmptyString($part)) {
                ($loggerProxy = $localLogger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
                && $loggerProxy->log(
                    $dbgPairType . ' string is invalid because ' . $dbgPartType .  ' is empty/whitespace-only string',
                    [$dbgFirstPartType => $parts[0], $dbgSecondPartType => $parts[1]]
                );
                return false;
            }

            if ($maxLen !== null && ($partLen = strlen($part)) > $maxLen) {
                ($loggerProxy = $localLogger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
                && $loggerProxy->log(
                    $dbgPairType . ' string is invalid because ' . $dbgPartType .  ' is longer than max allowed',
                    [$dbgPartType . 'length' => $partLen, 'max' => $maxLen, $dbgPartType => $part]
                );
                return false;
            }

            if ($regex !== null && ($pregMatchRetVal = preg_match($regex, $part)) !== 1) {
                ($loggerProxy = $localLogger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
                && $loggerProxy->log(
                    $dbgPairType . ' string is invalid because ' . $dbgPartType . ' does not match the pattern',
                    [$dbgPartType => $part, 'regex' => $regex, 'pregMatchRetVal' => $pregMatchRetVal]
                );
                return false;
            }

            $dbgPartType = $dbgSecondPartType;
            $maxLen = $secondPartMaxLen;
            $regex = $secondPartRegex;
        }

        $firstPart = $parts[0];
        $secondPart = $parts[1];
        return true;
    }