Skip to content

Commit b4b20fb

Browse files
committed
fix: property caching bug
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent e61c314 commit b4b20fb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/SequenceMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public function setSeq1(array $a): self
187187
{
188188
if ($this->a !== $a) {
189189
$this->a = $a;
190+
$this->chainB();
190191
$this->resetCachedResults();
191192
}
192193

@@ -203,9 +204,8 @@ public function setSeq2(array $b): self
203204
{
204205
if ($this->b !== $b) {
205206
$this->b = $b;
206-
$this->resetCachedResults();
207-
208207
$this->chainB();
208+
$this->resetCachedResults();
209209
}
210210

211211
return $this;

tests/SequenceMatcherTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,29 @@ public function testGetOpcodesIgnoreWhitespaces(string $old, string $new, array
345345

346346
static::assertSame($expected, $this->sm->getGroupedOpcodes(0));
347347
}
348+
349+
/**
350+
* @see https://github.com/jfcherng/php-sequence-matcher/issues/2
351+
*/
352+
public function testCachingBugIssue2(): void
353+
{
354+
$old = ['a'];
355+
$new = ['a', 'b', 'c'];
356+
357+
$this->sm->setSeq1($old)->setSeq2($new);
358+
359+
$old = ['a', 'b'];
360+
$new = ['a', 'b', 'c'];
361+
362+
$this->sm->setSeq1($old)->setSeq2($new);
363+
364+
try {
365+
// Throws ErrorException: "Undefined array key 1"
366+
$this->sm->getOpcodes();
367+
} catch (\Exception $e) {
368+
static::fail((string) $e);
369+
}
370+
371+
static::assertTrue(true);
372+
}
348373
}

0 commit comments

Comments
 (0)