From 0bb7bdb5121582d7c27d7713f360a8c876ec8001 Mon Sep 17 00:00:00 2001 From: Daniel Khalil Date: Mon, 22 Jun 2026 14:34:02 +0200 Subject: [PATCH] fix: use strict comparison for old/new equality check PHP 8's <=> operator compares numeric string arrays using numeric semantics, causing "00" and "0" (or "1" and "01", "10" and "1e1") to be treated as identical. This made the renderer short-circuit and return empty output for inputs that differ only in numeric string representation. Use === for the equality check and fall back to <=> only for ordering, forcing a non-zero result when arrays are strictly different. --- src/Differ.php | 2 +- tests/DifferTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Differ.php b/src/Differ.php index f03206e..956bdea 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -483,7 +483,7 @@ private function finalize(): self $this->oldNoEolAtEofIdx = $this->getOld(-1) === [''] ? -1 : \count($this->old); $this->newNoEolAtEofIdx = $this->getNew(-1) === [''] ? -1 : \count($this->new); - $this->oldNewComparison = $this->old <=> $this->new; + $this->oldNewComparison = $this->old === $this->new ? 0 : ($this->old <=> $this->new ?: 1); $this->sequenceMatcher->setOptions($this->options->toSequenceMatcherOptions()); } diff --git a/tests/DifferTest.php b/tests/DifferTest.php index 985e201..4d1c46a 100644 --- a/tests/DifferTest.php +++ b/tests/DifferTest.php @@ -18,6 +18,19 @@ */ final class DifferTest extends TestCase { + /** + * Test that numeric strings that are equal numerically but not identical + * (e.g. "00" vs "0") are correctly detected as different. + * + * @covers \Jfcherng\Diff\Differ::getOldNewComparison + */ + public function testNumericStringComparisonIsStrict(): void + { + $differ = new Differ(['00'], ['0']); + + self::assertNotSame(0, $differ->getOldNewComparison()); + } + /** * Test the Differ::getGroupedOpcodes. *