src/Primitive/Support/Traits/SliceableTrait.php
protected function adjustBoundary($length, $boundary, $step)
    {
        if ($boundary < 0)
        {
            $boundary += $length;

            if ($boundary < 0)
            {
                $boundary = ($step < 0) ? -1 : 0;
            }
        }
        elseif ($boundary >= $length)
        {
            $boundary = ($step < 0) ? $length - 1 : $length;
        }

        return $boundary;
    }
protected function adjustBoundary($length, $boundary, $step)
    {
        if ($boundary < 0)
        {
            $boundary += $length;

            if ($boundary < 0)
            {
                $boundary = ($step < 0) ? -1 : 0;
            }
        }
        elseif ($boundary >= $length)
        {
            $boundary = ($step < 0) ? $length - 1 : $length;
        }

        return $boundary;
    }
Adjusts the start or stop boundary based on the provided length and step.
The logic here uses CPython's PySlice_GetIndices as a reference. See:
https://github.com/python-git/python/blob/master/Objects/sliceobject.c

@param int $length   The length of the string
@param int $boundary Start or stop value to adjust
@param int $step     The step to be used with the slice

@return int An adjusted boundary value