Copy <?php
include_once('Substring.php');
class ComparisonCompactorOriginal
{
private const ELLIPSIS = '...';
private const DELTA_START = '[';
private const DELTA_END = ']';
/** @var int */
private $_contextLength;
/** @var string|null */
private $_expected;
/** @var string|null */
private $_actual;
/** @var int */
private $_prefix;
/** @var int */
private $_suffix;
/**
* ComparisonCompactorOriginal constructor.
* @param int $contextLength
* @param string|null $expected
* @param string|null $actual
*/
public function __construct(int $contextLength, ?string $expected, ?string $actual)
{
$this->_contextLength = $contextLength;
$this->_expected = $expected;
$this->_actual = $actual;
}
/**
* @param string|null $message
* @return string
*/
public function compact(string $message = null): string
{
if ($this->_expected === null || $this->_actual === null || $this->areStringsEqual()) {
return trim(sprintf('%s expected: {%s} but was: {%s}', $message, $this->_expected, $this->_actual));
}
$this->findCommonPrefix();
$this->findCommonSuffix();
$expected = $this->compactString($this->_expected);
$actual = $this->compactString($this->_actual);
return trim(sprintf('%s expected: {%s} but was: {%s}', $message, $expected, $actual));
}
/**
* @param string $source
* @return string
*/
private function compactString(string $source): string
{
$result =
self::DELTA_START .
substring($source, $this->_prefix, strlen($source) - $this->_suffix + 1) .
self::DELTA_END;
if ($this->_prefix > 0) {
$result = $this->computeCommonPrefix() . $result;
}
if ($this->_suffix > 0) {
$result = $result . $this->computeCommonSuffix();
}
return $result;
}
/**
* @return void
*/
private function findCommonPrefix(): void
{
$this->_prefix = 0;
$end = min(strlen($this->_expected), strlen($this->_actual));
for (; $this->_prefix < $end; $this->_prefix++) {
if ($this->_expected[$this->_prefix] !== $this->_actual[$this->_prefix]) {
break;
}
}
}
/**
* @return void
*/
private function findCommonSuffix(): void
{
$expectedSuffix = strlen($this->_expected) - 1;
$actualSuffix = strlen($this->_actual) - 1;
for (; $actualSuffix >= $this->_prefix && $expectedSuffix >= $this->_prefix; $actualSuffix--, $expectedSuffix--) {
if ($this->_expected[$expectedSuffix] !== $this->_actual[$actualSuffix]) {
break;
}
}
$this->_suffix = strlen($this->_expected) - $expectedSuffix;
}
/**
* @return string
*/
private function computeCommonPrefix(): string
{
return
($this->_prefix > $this->_contextLength ? self::ELLIPSIS : '') .
(substring($this->_expected, max(0, $this->_prefix - $this->_contextLength), $this->_prefix));
}
/**
* @return string
*/
private function computeCommonSuffix(): string
{
$end = min(strlen($this->_expected) - $this->_suffix + 1 + $this->_contextLength, strlen($this->_expected));
return
(substring($this->_expected, strlen($this->_expected) - $this->_suffix + 1, $end)) .
(strlen($this->_expected) - $this->_suffix + 1 < strlen($this->_expected) - $this->_contextLength ? self::ELLIPSIS : '');
}
/**
* @return bool
*/
private function areStringsEqual(): bool
{
return $this->_expected === $this->_actual;
}
}