Skip to content

Commit cb54261

Browse files
committed
Tidy codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent 924355a commit cb54261

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

Diff for: src/Differ.php

+16-62
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Jfcherng\Diff;
66

7+
use Jfcherng\Diff\Utility\Arr;
8+
79
/**
810
* A comprehensive library for generating differences between two strings
911
* in multiple formats (unified, side by side HTML etc).
@@ -178,33 +180,31 @@ public function setOptions(array $options): self
178180
}
179181

180182
/**
181-
* Get a range of lines from $start to $end from the old string and return them as an array.
182-
*
183-
* If $end is null, it returns array sliced from the $start to the end.
183+
* Get a range of lines from $start to $end from the old.
184184
*
185-
* @param int $start the starting number. If null, the whole array will be returned.
186-
* @param null|int $end the ending number. If null, only the item in $start will be returned.
185+
* @param int $start the starting index (negative = count from backward)
186+
* @param null|int $end the ending index (negative = count from backward)
187+
* if is null, it returns a slice from $start to the end
187188
*
188189
* @return string[] array of all of the lines between the specified range
189190
*/
190191
public function getOld(int $start = 0, ?int $end = null): array
191192
{
192-
return $this->getText($this->old, $start, $end);
193+
return Arr::getPartialByIndex($this->old, $start, $end);
193194
}
194195

195196
/**
196-
* Get a range of lines from $start to $end from the new string and return them as an array.
197-
*
198-
* If $end is null, it returns array sliced from the $start to the end.
197+
* Get a range of lines from $start to $end from the new.
199198
*
200-
* @param int $start the starting number
201-
* @param null|int $end the ending number
199+
* @param int $start the starting index (negative = count from backward)
200+
* @param null|int $end the ending index (negative = count from backward)
201+
* if is null, it returns a slice from $start to the end
202202
*
203203
* @return string[] array of all of the lines between the specified range
204204
*/
205205
public function getNew(int $start = 0, ?int $end = null): array
206206
{
207-
return $this->getText($this->new, $start, $end);
207+
return Arr::getPartialByIndex($this->new, $start, $end);
208208
}
209209

210210
/**
@@ -291,8 +291,8 @@ public function getGroupedOpcodesGnu(): array
291291

292292
return $this->groupedOpcodesGnu = $this->sequenceMatcher
293293
->setSequences(
294-
$this->makeLinesGnuCompatible($this->old),
295-
$this->makeLinesGnuCompatible($this->new)
294+
$this->createGnuCompatibleLines($this->old),
295+
$this->createGnuCompatibleLines($this->new)
296296
)
297297
->getGroupedOpcodes($this->options['context']);
298298
}
@@ -335,61 +335,15 @@ private function resetCachedResults(): self
335335
return $this;
336336
}
337337

338-
/**
339-
* The work horse of getOld() and getNew().
340-
*
341-
* If $end is null, it returns array sliced from the $start to the end.
342-
*
343-
* @param string[] $lines the array of lines
344-
* @param int $start the starting number
345-
* @param null|int $end the ending number
346-
*
347-
* @return string[] array of all of the lines between the specified range
348-
*/
349-
private function getText(array $lines, int $start = 0, ?int $end = null): array
350-
{
351-
$arrayLength = \count($lines);
352-
353-
// make $end set
354-
$end = $end ?? $arrayLength;
355-
356-
// make $start non-negative
357-
if ($start < 0) {
358-
$start += $arrayLength;
359-
360-
if ($start < 0) {
361-
$start = 0;
362-
}
363-
}
364-
365-
// may prevent from calling array_slice()
366-
if ($start === 0 && $end >= $arrayLength) {
367-
return $lines;
368-
}
369-
370-
// make $end non-negative
371-
if ($end < 0) {
372-
$end += $arrayLength;
373-
374-
if ($end < 0) {
375-
$end = 0;
376-
}
377-
}
378-
379-
// now both $start and $end are non-negative
380-
// hence the length for array_slice() must be non-negative
381-
return \array_slice($lines, $start, \max(0, $end - $start));
382-
}
383-
384338
/**
385339
* Make the lines to be prepared for GNU-style diff.
386340
*
387341
* This method checks whether $lines has no EOL at EOF and append a special
388342
* indicator to the last line.
389343
*
390-
* @param string[] $lines the lines
344+
* @param string[] $lines the lines created by simply explode("\n", $string)
391345
*/
392-
private function makeLinesGnuCompatible(array $lines): array
346+
private function createGnuCompatibleLines(array $lines): array
393347
{
394348
// note that the $lines should not be empty at this point
395349
// they have at least one element "" in the array because explode("\n", "") === [""]

Diff for: src/Utility/Arr.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jfcherng\Diff\Utility;
6+
7+
final class Arr
8+
{
9+
/**
10+
* Get a partial array slice with start/end indexes.
11+
*
12+
* @param array $array the array
13+
* @param int $start the starting index (negative = count from backward)
14+
* @param null|int $end the ending index (negative = count from backward)
15+
* if is null, it returns a slice from $start to the end
16+
*
17+
* @return array array of all of the lines between the specified range
18+
*/
19+
public static function getPartialByIndex(array $array, int $start = 0, ?int $end = null): array
20+
{
21+
$count = \count($array);
22+
23+
// make $end set
24+
$end = $end ?? $count;
25+
26+
// make $start non-negative
27+
if ($start < 0) {
28+
$start += $count;
29+
30+
if ($start < 0) {
31+
$start = 0;
32+
}
33+
}
34+
35+
// make $end non-negative
36+
if ($end < 0) {
37+
$end += $count;
38+
39+
if ($end < 0) {
40+
$end = 0;
41+
}
42+
}
43+
44+
// make the length non-negative
45+
return \array_slice($array, $start, \max(0, $end - $start));
46+
}
47+
}

0 commit comments

Comments
 (0)