Skip to content

Commit d02b545

Browse files
committed
refactor: some refactorings for php81
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 4d85156 commit d02b545

File tree

3 files changed

+81
-89
lines changed

3 files changed

+81
-89
lines changed

src/SequenceMatcher.php

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@
1414
final class SequenceMatcher
1515
{
1616
/** @var int 0, opcode: no operation */
17-
const OP_NOP = 0;
17+
public const OP_NOP = 0;
1818

1919
/** @var int 1, opcode: equal */
20-
const OP_EQ = 1 << 0;
20+
public const OP_EQ = 1 << 0;
2121

2222
/** @var int 2, opcode: delete */
23-
const OP_DEL = 1 << 1;
23+
public const OP_DEL = 1 << 1;
2424

2525
/** @var int 4, opcode: insert */
26-
const OP_INS = 1 << 2;
26+
public const OP_INS = 1 << 2;
2727

2828
/** @var int 8, opcode: replace */
29-
const OP_REP = 1 << 3;
29+
public const OP_REP = 1 << 3;
3030

31-
const OP_INT_TO_STR_MAP = [
31+
public const OP_INT_TO_STR_MAP = [
3232
self::OP_NOP => 'nop',
3333
self::OP_EQ => 'eq',
3434
self::OP_DEL => 'del',
3535
self::OP_INS => 'ins',
3636
self::OP_REP => 'rep',
3737
];
3838

39-
const OP_STR_TO_INT_MAP = [
39+
public const OP_STR_TO_INT_MAP = [
4040
'nop' => self::OP_NOP,
4141
'eq' => self::OP_EQ,
4242
'del' => self::OP_DEL,
@@ -50,65 +50,56 @@ final class SequenceMatcher
5050
*
5151
* @var string
5252
*/
53-
const APPENDED_HELPER_LINE = "\u{fcf28}\u{fc232}";
53+
public const APPENDED_HELPER_LINE = "\u{fcf28}\u{fc232}";
5454

5555
/**
56-
* @var null|callable either a string or an array containing a callback function to determine if a line is "junk" or not
56+
* @var null|\Closure either a string or an array containing a callback function to determine if a line is "junk" or not
5757
*/
58-
private $junkCallback;
58+
private ?\Closure $junkCallback;
5959

6060
/**
6161
* @var array the first sequence to compare against
6262
*/
63-
private $a = [];
63+
private array $a = [];
6464

6565
/**
6666
* @var array the second sequence
6767
*/
68-
private $b = [];
68+
private array $b = [];
6969

7070
/**
7171
* @var array the first sequence to compare against (transformed)
7272
*/
73-
private $at = [];
73+
private array $at = [];
7474

7575
/**
7676
* @var array the second sequence (transformed)
7777
*/
78-
private $bt = [];
78+
private array $bt = [];
7979

8080
/**
8181
* @var array array of characters that are considered junk from the second sequence. Characters are the array key.
8282
*/
83-
private $junkDict = [];
83+
private array $junkDict = [];
8484

8585
/**
8686
* @var array array of indices that do not contain junk elements
8787
*/
88-
private $b2j = [];
88+
private array $b2j = [];
8989

90-
/**
91-
* @var array
92-
*/
93-
private $options = [];
90+
private array $options = [];
9491

95-
/**
96-
* @var array
97-
*/
98-
private static $defaultOptions = [
92+
private static array $defaultOptions = [
9993
'ignoreWhitespace' => false,
10094
'ignoreCase' => false,
10195
];
10296

103-
/**
104-
* @var array
105-
*/
106-
private $matchingBlocks = [];
97+
private array $matchingBlocks = [];
10798

10899
/**
109100
* @var array generated opcodes which manipulates seq1 to seq2
110101
*/
111-
private $opcodes = [];
102+
private array $opcodes = [];
112103

113104
/**
114105
* The constructor. With the sequences being passed, they'll be set
@@ -117,10 +108,10 @@ final class SequenceMatcher
117108
*
118109
* @param string[] $a an array containing the lines to compare against
119110
* @param string[] $b an array containing the lines to compare
120-
* @param null|callable $junkCallback either an array or string that references a callback function (if there is one) to determine 'junk' characters
111+
* @param null|\Closure $junkCallback either an array or string that references a callback function (if there is one) to determine 'junk' characters
121112
* @param array $options the options
122113
*/
123-
public function __construct(array $a, array $b, ?callable $junkCallback = null, array $options = [])
114+
public function __construct(array $a, array $b, ?\Closure $junkCallback = null, array $options = [])
124115
{
125116
$this->junkCallback = $junkCallback;
126117
$this->setOptions($options);
@@ -132,7 +123,7 @@ public function __construct(array $a, array $b, ?callable $junkCallback = null,
132123
*
133124
* @param array $options The options
134125
*/
135-
public function setOptions(array $options): self
126+
public function setOptions(array $options): static
136127
{
137128
$needRerunChainB = $this->isAnyOptionChanged($this->options, $options, ['ignoreCase', 'ignoreWhitespace']);
138129

@@ -158,7 +149,7 @@ public function getOptions(): array
158149
/**
159150
* Reset cached results.
160151
*/
161-
public function resetCachedResults(): self
152+
public function resetCachedResults(): static
162153
{
163154
$this->matchingBlocks = [];
164155
$this->opcodes = [];
@@ -175,7 +166,7 @@ public function resetCachedResults(): self
175166
* @param string[] $a an array containing the lines to compare against
176167
* @param string[] $b an array containing the lines to compare
177168
*/
178-
public function setSequences(array $a, array $b): self
169+
public function setSequences(array $a, array $b): static
179170
{
180171
$need_routine = false;
181172

@@ -203,7 +194,7 @@ public function setSequences(array $a, array $b): self
203194
*
204195
* @param string[] $a the sequence to set as the first sequence
205196
*/
206-
public function setSeq1(array $a): self
197+
public function setSeq1(array $a): static
207198
{
208199
if ($this->a !== $a) {
209200
$this->a = $a;
@@ -220,7 +211,7 @@ public function setSeq1(array $a): self
220211
*
221212
* @param string[] $b the sequence to set as the second sequence
222213
*/
223-
public function setSeq2(array $b): self
214+
public function setSeq2(array $b): static
224215
{
225216
if ($this->b !== $b) {
226217
$this->b = $b;
@@ -357,7 +348,7 @@ public function getMatchingBlocks(): array
357348

358349
$matchingBlocks = [];
359350
while (!empty($queue)) {
360-
[$alo, $ahi, $blo, $bhi] = \array_pop($queue);
351+
[$alo, $ahi, $blo, $bhi] = array_pop($queue);
361352
[$i, $j, $k] = $x = $this->findLongestMatch($alo, $ahi, $blo, $bhi);
362353

363354
if ($k) {
@@ -373,10 +364,10 @@ public function getMatchingBlocks(): array
373364
}
374365
}
375366

376-
\usort($matchingBlocks, function (array $a, array $b): int {
367+
usort($matchingBlocks, function (array $a, array $b): int {
377368
$aCount = \count($a);
378369
$bCount = \count($b);
379-
$min = \min($aCount, $bCount);
370+
$min = min($aCount, $bCount);
380371

381372
for ($i = 0; $i < $min; ++$i) {
382373
if ($a[$i] !== $b[$i]) {
@@ -422,13 +413,13 @@ public function getMatchingBlocks(): array
422413
*
423414
* The nested array returned contains an array describing the opcode
424415
* which includes:
425-
* 0 - The type of tag (as described below) for the opcode.
416+
* 0 - The type of op (as described below) for the opcode.
426417
* 1 - The beginning line in the first sequence.
427418
* 2 - The end line in the first sequence.
428419
* 3 - The beginning line in the second sequence.
429420
* 4 - The end line in the second sequence.
430421
*
431-
* The different types of tags include:
422+
* The different types of ops include:
432423
* replace - The string from $i1 to $i2 in $a should be replaced by
433424
* the string in $b from $j1 to $j2.
434425
* delete - The string in $a from $i1 to $j2 should be deleted.
@@ -449,17 +440,17 @@ public function getOpcodes(): array
449440

450441
foreach ($this->getMatchingBlocks() as [$ai, $bj, $size]) {
451442
if ($i < $ai && $j < $bj) {
452-
$tag = self::OP_REP;
443+
$op = self::OP_REP;
453444
} elseif ($i < $ai) {
454-
$tag = self::OP_DEL;
445+
$op = self::OP_DEL;
455446
} elseif ($j < $bj) {
456-
$tag = self::OP_INS;
447+
$op = self::OP_INS;
457448
} else {
458-
$tag = self::OP_NOP;
449+
$op = self::OP_NOP;
459450
}
460451

461-
if ($tag) {
462-
$this->opcodes[] = [$tag, $i, $ai, $j, $bj];
452+
if ($op) {
453+
$this->opcodes[] = [$op, $i, $ai, $j, $bj];
463454
}
464455

465456
$i = $ai + $size;
@@ -502,44 +493,44 @@ public function getGroupedOpcodes(int $context = 3): array
502493
// fix the leading sequence which is out of context.
503494
$opcodes[0] = [
504495
$opcodes[0][0],
505-
\max($opcodes[0][1], $opcodes[0][2] - $context),
496+
max($opcodes[0][1], $opcodes[0][2] - $context),
506497
$opcodes[0][2],
507-
\max($opcodes[0][3], $opcodes[0][4] - $context),
498+
max($opcodes[0][3], $opcodes[0][4] - $context),
508499
$opcodes[0][4],
509500
];
510501
}
511502

512503
$lastItem = \count($opcodes) - 1;
513504
if ($opcodes[$lastItem][0] === self::OP_EQ) {
514-
[$tag, $i1, $i2, $j1, $j2] = $opcodes[$lastItem];
505+
[$op, $i1, $i2, $j1, $j2] = $opcodes[$lastItem];
515506
// fix the trailing sequence which is out of context.
516507
$opcodes[$lastItem] = [
517-
$tag,
508+
$op,
518509
$i1,
519-
\min($i2, $i1 + $context),
510+
min($i2, $i1 + $context),
520511
$j1,
521-
\min($j2, $j1 + $context),
512+
min($j2, $j1 + $context),
522513
];
523514
}
524515

525516
$maxRange = $context << 1;
526517
$groups = $group = [];
527-
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
528-
if ($tag === self::OP_EQ && $i2 - $i1 > $maxRange) {
518+
foreach ($opcodes as [$op, $i1, $i2, $j1, $j2]) {
519+
if ($op === self::OP_EQ && $i2 - $i1 > $maxRange) {
529520
$group[] = [
530-
$tag,
521+
$op,
531522
$i1,
532-
\min($i2, $i1 + $context),
523+
min($i2, $i1 + $context),
533524
$j1,
534-
\min($j2, $j1 + $context),
525+
min($j2, $j1 + $context),
535526
];
536527
$groups[] = $group;
537528
$group = [];
538-
$i1 = \max($i1, $i2 - $context);
539-
$j1 = \max($j1, $j2 - $context);
529+
$i1 = max($i1, $i2 - $context);
530+
$j1 = max($j1, $j2 - $context);
540531
}
541532

542-
$group[] = [$tag, $i1, $i2, $j1, $j2];
533+
$group[] = [$op, $i1, $i2, $j1, $j2];
543534
}
544535

545536
if (
@@ -643,11 +634,11 @@ private function processLineWithOptions(string $line): string
643634
if ($this->options['ignoreWhitespace']) {
644635
static $whitespaces = [' ', "\t", "\r", "\n"];
645636

646-
$line = \str_replace($whitespaces, '', $line);
637+
$line = str_replace($whitespaces, '', $line);
647638
}
648639

649640
if ($this->options['ignoreCase']) {
650-
$line = \strtolower($line);
641+
$line = strtolower($line);
651642
}
652643

653644
return $line;
@@ -657,10 +648,10 @@ private function processLineWithOptions(string $line): string
657648
* Generate the internal arrays containing the list of junk and non-junk
658649
* characters for the second ($b) sequence.
659650
*/
660-
private function chainB(): self
651+
private function chainB(): static
661652
{
662-
$this->at = \array_map([$this, 'processLineWithOptions'], $this->a);
663-
$this->bt = \array_map([$this, 'processLineWithOptions'], $this->b);
653+
$this->at = array_map([$this, 'processLineWithOptions'], $this->a);
654+
$this->bt = array_map([$this, 'processLineWithOptions'], $this->b);
664655

665656
$length = \count($this->bt);
666657
$this->b2j = [];
@@ -684,20 +675,20 @@ private function chainB(): self
684675
}
685676

686677
// remove leftovers
687-
foreach (\array_keys($popularDict) as $char) {
678+
foreach (array_keys($popularDict) as $char) {
688679
unset($this->b2j[$char]);
689680
}
690681

691682
$this->junkDict = [];
692683
if (\is_callable($this->junkCallback)) {
693-
foreach (\array_keys($popularDict) as $char) {
684+
foreach (array_keys($popularDict) as $char) {
694685
if (($this->junkCallback)($char)) {
695686
$this->junkDict[$char] = 1;
696687
unset($popularDict[$char]);
697688
}
698689
}
699690

700-
foreach (\array_keys($this->b2j) as $char) {
691+
foreach (array_keys($this->b2j) as $char) {
701692
if (($this->junkCallback)($char)) {
702693
$this->junkDict[$char] = 1;
703694
unset($this->b2j[$char]);

0 commit comments

Comments
 (0)