Skip to content

Commit df25c88

Browse files
committed
Update jfcherng/php-sequence-matcher ^2.0
Signed-off-by: Jack Cherng <[email protected]>
1 parent 0c7b425 commit df25c88

File tree

9 files changed

+55
-43
lines changed

9 files changed

+55
-43
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require": {
2727
"php": "^7.1.3",
2828
"jfcherng/php-mb-string": "^1.3",
29-
"jfcherng/php-sequence-matcher": "^1.0"
29+
"jfcherng/php-sequence-matcher": "^2.0"
3030
},
3131
"require-dev": {
3232
"friendsofphp/php-cs-fixer": "^2.14.1",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Renderer/Html/AbstractHtml.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ abstract class AbstractHtml extends AbstractRenderer
2525
* @var array array of the different opcode tags and how they map to the HTML class
2626
*/
2727
const TAG_CLASS_MAP = [
28-
SequenceMatcher::OPCODE_DELETE => 'del',
29-
SequenceMatcher::OPCODE_EQUAL => 'eq',
30-
SequenceMatcher::OPCODE_INSERT => 'ins',
31-
SequenceMatcher::OPCODE_REPLACE => 'rep',
28+
SequenceMatcher::OP_DEL => 'del',
29+
SequenceMatcher::OP_EQ => 'eq',
30+
SequenceMatcher::OP_INS => 'ins',
31+
SequenceMatcher::OP_REP => 'rep',
3232
];
3333

3434
/**
@@ -61,7 +61,7 @@ public function getChanges(): array
6161

6262
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
6363
if (
64-
$tag === SequenceMatcher::OPCODE_REPLACE &&
64+
$tag === SequenceMatcher::OP_REP &&
6565
$i2 - $i1 === $j2 - $j1
6666
) {
6767
for ($i = 0; $i < $i2 - $i1; ++$i) {
@@ -76,7 +76,7 @@ public function getChanges(): array
7676

7777
$lastTag = $tag;
7878

79-
if ($tag === SequenceMatcher::OPCODE_EQUAL) {
79+
if ($tag === SequenceMatcher::OP_EQ) {
8080
if (!empty($lines = \array_slice($a, $i1, ($i2 - $i1)))) {
8181
$formattedLines = $this->formatLines($lines);
8282

@@ -87,9 +87,19 @@ public function getChanges(): array
8787
continue;
8888
}
8989

90+
/**
91+
* @todo By setting option "useIntOpcodes" for the sequence matcher,
92+
* this "if" could be further optimized by using bit operations.
93+
*
94+
* Like this: "if ($tag & (OP_INT_REP | OP_INT_DEL))"
95+
*
96+
* But int tag would be less readable while debugging.
97+
* Also, this would be a BC break for the output of the JSON renderer.
98+
* Is it worth doing?
99+
*/
90100
if (
91-
$tag === SequenceMatcher::OPCODE_REPLACE ||
92-
$tag === SequenceMatcher::OPCODE_DELETE
101+
$tag === SequenceMatcher::OP_REP ||
102+
$tag === SequenceMatcher::OP_DEL
93103
) {
94104
$lines = \array_slice($a, $i1, ($i2 - $i1));
95105
$lines = $this->formatLines($lines);
@@ -98,12 +108,13 @@ public function getChanges(): array
98108
RendererConstant::HTML_CLOSURES_DEL,
99109
$lines
100110
);
111+
101112
$blocks[$lastBlock]['base']['lines'] += $lines;
102113
}
103114

104115
if (
105-
$tag === SequenceMatcher::OPCODE_REPLACE ||
106-
$tag === SequenceMatcher::OPCODE_INSERT
116+
$tag === SequenceMatcher::OP_REP ||
117+
$tag === SequenceMatcher::OP_INS
107118
) {
108119
$lines = \array_slice($b, $j1, ($j2 - $j1));
109120
$lines = $this->formatLines($lines);
@@ -112,6 +123,7 @@ public function getChanges(): array
112123
RendererConstant::HTML_CLOSURES_INS,
113124
$lines
114125
);
126+
115127
$blocks[$lastBlock]['changed']['lines'] += $lines;
116128
}
117129
}

src/Renderer/Html/Inline.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ protected function renderTableBlock(array $change): string
9393
switch ($change['tag']) {
9494
default:
9595
// equal changes should be shown on both sides of the diff
96-
case SequenceMatcher::OPCODE_EQUAL:
96+
case SequenceMatcher::OP_EQ:
9797
$html .= $this->renderTableEqual($change);
9898
break;
9999
// added lines only on the r side
100-
case SequenceMatcher::OPCODE_INSERT:
100+
case SequenceMatcher::OP_INS:
101101
$html .= $this->renderTableInsert($change);
102102
break;
103103
// show deleted lines only on the l side
104-
case SequenceMatcher::OPCODE_DELETE:
104+
case SequenceMatcher::OP_DEL:
105105
$html .= $this->renderTableDelete($change);
106106
break;
107107
// show modified lines on both sides
108-
case SequenceMatcher::OPCODE_REPLACE:
108+
case SequenceMatcher::OP_REP:
109109
$html .= $this->renderTableReplace($change);
110110
break;
111111
}

src/Renderer/Html/LineRenderer/Char.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public function render(MbString $mbFrom, MbString $mbTo): LineRendererInterface
2121
// reversely iterate opcodes
2222
foreach (ReverseIterator::fromArray($opcodes) as [$tag, $i1, $i2, $j1, $j2]) {
2323
switch ($tag) {
24-
case SequenceMatcher::OPCODE_DELETE:
24+
case SequenceMatcher::OP_DEL:
2525
$mbFrom->str_enclose_i(RendererConstant::HTML_CLOSURES, $i1, $i2 - $i1);
2626
break;
27-
case SequenceMatcher::OPCODE_INSERT:
27+
case SequenceMatcher::OP_INS:
2828
$mbTo->str_enclose_i(RendererConstant::HTML_CLOSURES, $j1, $j2 - $j1);
2929
break;
30-
case SequenceMatcher::OPCODE_REPLACE:
30+
case SequenceMatcher::OP_REP:
3131
$mbFrom->str_enclose_i(RendererConstant::HTML_CLOSURES, $i1, $i2 - $i1);
3232
$mbTo->str_enclose_i(RendererConstant::HTML_CLOSURES, $j1, $j2 - $j1);
3333
break;

src/Renderer/Html/LineRenderer/Word.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public function render(MbString $mbFrom, MbString $mbTo): LineRendererInterface
4848
// reversely iterate opcodes
4949
foreach (ReverseIterator::fromArray($opcodes) as [$tag, $i1, $i2, $j1, $j2]) {
5050
switch ($tag) {
51-
case SequenceMatcher::OPCODE_DELETE:
51+
case SequenceMatcher::OP_DEL:
5252
$fromWords[$i1] = RendererConstant::HTML_CLOSURES[0] . $fromWords[$i1];
5353
$fromWords[$i2 - 1] .= RendererConstant::HTML_CLOSURES[1];
5454
break;
55-
case SequenceMatcher::OPCODE_INSERT:
55+
case SequenceMatcher::OP_INS:
5656
$toWords[$j1] = RendererConstant::HTML_CLOSURES[0] . $toWords[$j1];
5757
$toWords[$j2 - 1] .= RendererConstant::HTML_CLOSURES[1];
5858
break;
59-
case SequenceMatcher::OPCODE_REPLACE:
59+
case SequenceMatcher::OP_REP:
6060
$fromWords[$i1] = RendererConstant::HTML_CLOSURES[0] . $fromWords[$i1];
6161
$fromWords[$i2 - 1] .= RendererConstant::HTML_CLOSURES[1];
6262
$toWords[$j1] = RendererConstant::HTML_CLOSURES[0] . $toWords[$j1];

src/Renderer/Html/SideBySide.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,19 @@ protected function renderTableBlock(array $change): string
9191
switch ($change['tag']) {
9292
default:
9393
// equal changes should be shown on both sides of the diff
94-
case SequenceMatcher::OPCODE_EQUAL:
94+
case SequenceMatcher::OP_EQ:
9595
$html .= $this->renderTableEqual($change);
9696
break;
9797
// added lines only on the r side
98-
case SequenceMatcher::OPCODE_INSERT:
98+
case SequenceMatcher::OP_INS:
9999
$html .= $this->renderTableInsert($change);
100100
break;
101101
// show deleted lines only on the l side
102-
case SequenceMatcher::OPCODE_DELETE:
102+
case SequenceMatcher::OP_DEL:
103103
$html .= $this->renderTableDelete($change);
104104
break;
105105
// show modified lines on both sides
106-
case SequenceMatcher::OPCODE_REPLACE:
106+
case SequenceMatcher::OP_REP:
107107
$html .= $this->renderTableReplace($change);
108108
break;
109109
}

src/Renderer/Text/Context.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class Context extends AbstractText
2424
* @var array array of the different opcode tags and how they map to the context diff equivalent
2525
*/
2626
const TAG_MAP = [
27-
SequenceMatcher::OPCODE_DELETE => '-',
28-
SequenceMatcher::OPCODE_EQUAL => ' ',
29-
SequenceMatcher::OPCODE_INSERT => '+',
30-
SequenceMatcher::OPCODE_REPLACE => '!',
27+
SequenceMatcher::OP_DEL => '-',
28+
SequenceMatcher::OP_EQ => ' ',
29+
SequenceMatcher::OP_INS => '+',
30+
SequenceMatcher::OP_REP => '!',
3131
];
3232

3333
/**
@@ -85,7 +85,7 @@ protected function renderBlockFrom(array $opcodes): string
8585
$ret = '';
8686

8787
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
88-
if ($tag === SequenceMatcher::OPCODE_INSERT) {
88+
if ($tag === SequenceMatcher::OP_INS) {
8989
continue;
9090
}
9191

@@ -110,7 +110,7 @@ protected function renderBlockTo(array $opcodes): string
110110
$ret = '';
111111

112112
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
113-
if ($tag === SequenceMatcher::OPCODE_DELETE) {
113+
if ($tag === SequenceMatcher::OP_DEL) {
114114
continue;
115115
}
116116

src/Renderer/Text/Unified.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ public function render(): string
4242
$ret .= $this->renderBlockHeader($i1 + 1, $i2 - $i1, $j1 + 1, $j2 - $j1);
4343

4444
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
45-
if ($tag === SequenceMatcher::OPCODE_EQUAL) {
45+
if ($tag === SequenceMatcher::OP_EQ) {
4646
$ret .= $this->renderContext(' ', $this->diff->getA($i1, $i2));
4747

4848
continue;
4949
}
5050

5151
if (
52-
$tag === SequenceMatcher::OPCODE_REPLACE ||
53-
$tag === SequenceMatcher::OPCODE_DELETE
52+
$tag === SequenceMatcher::OP_REP ||
53+
$tag === SequenceMatcher::OP_DEL
5454
) {
5555
$ret .= $this->renderContext('-', $this->diff->getA($i1, $i2));
5656
}
5757

5858
if (
59-
$tag === SequenceMatcher::OPCODE_REPLACE ||
60-
$tag === SequenceMatcher::OPCODE_INSERT
59+
$tag === SequenceMatcher::OP_REP ||
60+
$tag === SequenceMatcher::OP_INS
6161
) {
6262
$ret .= $this->renderContext('+', $this->diff->getB($j1, $j2));
6363
}

0 commit comments

Comments
 (0)