Bolt\Helpers\Excerpt::determineSnipLocation PHP Method

determineSnipLocation() private method

When checking for matches we only change the location if there is a better match. The only exception is where we have only two matches in which case we just take the first as will be equally distant.
private determineSnipLocation ( array $locations, integer $prevCount ) : integer
$locations array
$prevCount integer
return integer
    private function determineSnipLocation(array $locations, $prevCount)
    {
        // If we only have 1 match we don't actually do the for loop so set to the first
        $startPos = (int) reset($locations);
        $loccount = count($locations);
        $smallestDiff = PHP_INT_MAX;
        // If we only have 2, skip as it's probably equally relevant
        if (count($locations) > 2) {
            // skip the first as we check 1 behind
            for ($i = 1; $i < $loccount; $i++) {
                if ($i === $loccount - 1) {
                    // at the end
                    $diff = $locations[$i] - $locations[$i - 1];
                } else {
                    $diff = $locations[$i + 1] - $locations[$i];
                }
                if ($smallestDiff > $diff) {
                    $smallestDiff = $diff;
                    $startPos = $locations[$i];
                }
            }
        }
        $startPos = $startPos > $prevCount ? $startPos - $prevCount : 0;
        return $startPos;
    }