src/Patch.php
public function addContext(PatchObject $patch, $text)
    {
        if (!mb_strlen($text)) {
            return;
        }
        $padding = 0;
        $pattern = mb_substr($text, $patch->getStart1(), $patch->getLength1());

        // Look for the first and last matches of pattern in text.
        // If two different matches are found, increase the pattern length.
        $match = $this->getMatch();
        while (
            (!$pattern || mb_strpos($text, $pattern) !== mb_strrpos($text, $pattern)) &&
            ($match->getMaxBits() == 0 || mb_strlen($pattern) < $match->getMaxBits() - 2 * $this->getMargin())
        ) {
            $padding += $this->getMargin();
            $pattern = mb_substr(
                $text,
                max(0, $patch->getStart2() - $padding),
                $patch->getStart2() + $patch->getLength1() + $padding - max(0, $patch->getStart2() - $padding)
            );
        }
        // Add one chunk for good luck.
        $padding += $this->getMargin();

        // Add the prefix.
        $prefix = mb_substr($text, max(0, $patch->getStart2() - $padding), min($patch->getStart2(), $padding));
        if ($prefix != '') {
            $patch->prependChanges(array(Diff::EQUAL, $prefix));
        }
        // Add the suffix.
        $suffix = mb_substr($text, $patch->getStart2() + $patch->getLength1(), $padding);
        if ($suffix != '') {
            $patch->appendChanges(array(Diff::EQUAL, $suffix));
        }

        // Roll back the start points.
        $prefixLen = mb_strlen($prefix);
        $patch->setStart1($patch->getStart1() - $prefixLen);
        $patch->setStart2($patch->getStart2() - $prefixLen);
        // Extend lengths.
        $suffixLen = mb_strlen($suffix);
        $patch->setLength1($patch->getLength1() + $prefixLen + $suffixLen);
        $patch->setLength2($patch->getLength2() + $prefixLen + $suffixLen);
    }
public function addContext(PatchObject $patch, $text)
    {
        if (!mb_strlen($text)) {
            return;
        }
        $padding = 0;
        $pattern = mb_substr($text, $patch->getStart1(), $patch->getLength1());

        // Look for the first and last matches of pattern in text.
        // If two different matches are found, increase the pattern length.
        $match = $this->getMatch();
        while (
            (!$pattern || mb_strpos($text, $pattern) !== mb_strrpos($text, $pattern)) &&
            ($match->getMaxBits() == 0 || mb_strlen($pattern) < $match->getMaxBits() - 2 * $this->getMargin())
        ) {
            $padding += $this->getMargin();
            $pattern = mb_substr(
                $text,
                max(0, $patch->getStart2() - $padding),
                $patch->getStart2() + $patch->getLength1() + $padding - max(0, $patch->getStart2() - $padding)
            );
        }
        // Add one chunk for good luck.
        $padding += $this->getMargin();

        // Add the prefix.
        $prefix = mb_substr($text, max(0, $patch->getStart2() - $padding), min($patch->getStart2(), $padding));
        if ($prefix != '') {
            $patch->prependChanges(array(Diff::EQUAL, $prefix));
        }
        // Add the suffix.
        $suffix = mb_substr($text, $patch->getStart2() + $patch->getLength1(), $padding);
        if ($suffix != '') {
            $patch->appendChanges(array(Diff::EQUAL, $suffix));
        }

        // Roll back the start points.
        $prefixLen = mb_strlen($prefix);
        $patch->setStart1($patch->getStart1() - $prefixLen);
        $patch->setStart2($patch->getStart2() - $prefixLen);
        // Extend lengths.
        $suffixLen = mb_strlen($suffix);
        $patch->setLength1($patch->getLength1() + $prefixLen + $suffixLen);
        $patch->setLength2($patch->getLength2() + $prefixLen + $suffixLen);
    }
Increase the context until it is unique, but don't let the pattern expand beyond Match->maxBits.

@param PatchObject $patch The patch to grow.
@param string      $text Source text.