Skip to content

Commit 6d7332b

Browse files
DKhaliljfcherng
authored andcommitted
fix: numeric string arrays incorrectly treated as identical (#95)
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 773e535 commit 6d7332b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/Differ.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private function finalize(): self
498498

499499
$this->oldNoEolAtEofIdx = $this->getOld(-1) === [''] ? -1 : \count($this->old);
500500
$this->newNoEolAtEofIdx = $this->getNew(-1) === [''] ? -1 : \count($this->new);
501-
$this->oldNewComparison = $this->old <=> $this->new;
501+
$this->oldNewComparison = $this->old === $this->new ? 0 : ($this->old <=> $this->new ?: 1);
502502

503503
$this->sequenceMatcher->setOptions($this->options);
504504
}

‎tests/DifferTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
*/
1616
final class DifferTest extends TestCase
1717
{
18+
/**
19+
* Test that numeric strings that are equal numerically but not identical
20+
* (e.g. "00" vs "0") are correctly detected as different.
21+
*
22+
* @covers \Jfcherng\Diff\Differ::getOldNewComparison
23+
*/
24+
public function testNumericStringComparisonIsStrict(): void
25+
{
26+
$differ = new Differ(['00'], ['0']);
27+
28+
self::assertNotSame(0, $differ->getOldNewComparison());
29+
}
30+
1831
/**
1932
* Data provider for Differ::getGroupedOpcodes.
2033
*

0 commit comments

Comments
 (0)