Skip to content

Commit

Permalink
Gracefully handle non-contiguous arrays in Differ
Browse files Browse the repository at this point in the history
Fixes #909.
  • Loading branch information
nikic committed May 21, 2023
1 parent 8490c0e commit 289756d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/PhpParser/Internal/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function __construct(callable $isEqual) {
* @return DiffElem[] Diff (edit script)
*/
public function diff(array $old, array $new): array {
$old = \array_values($old);
$new = \array_values($new);
list($trace, $x, $y) = $this->calculateTrace($old, $new);
return $this->extractDiff($trace, $x, $y, $old, $new);
}
Expand Down
11 changes: 11 additions & 0 deletions test/PhpParser/Internal/DifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ public function provideTestDiffWithReplacements() {
['abcde', 'axyzue', 'a-b-c-d+x+y+z+ue'],
];
}

public function testNonContiguousIndices() {
$differ = new Differ(function ($a, $b) {
return $a === $b;
});
$diff = $differ->diff([0 => 'a', 2 => 'b'], [0 => 'a', 3 => 'b']);
$this->assertEquals([
new DiffElem(DiffElem::TYPE_KEEP, 'a', 'a'),
new DiffElem(DiffElem::TYPE_KEEP, 'b', 'b'),
], $diff);
}
}
14 changes: 13 additions & 1 deletion test/code/formatPreservation/listRemoval.test
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,16 @@ class Foo
function getBaz()
{
}
}
}
-----
<?php
class Test {
use A, B;
}
-----
unset($stmts[0]->stmts[0]->traits[0]);
-----
<?php
class Test {
use B;
}

0 comments on commit 289756d

Please sign in to comment.