Skip to content

Commit 7a45667

Browse files
committed
Tidy codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent e96e971 commit 7a45667

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ public function getChanges(Differ $differ): array
7070
$lastBlock = 0;
7171

7272
foreach ($opcodes as [$op, $i1, $i2, $j1, $j2]) {
73-
if (
74-
$op === SequenceMatcher::OP_REP &&
75-
$i2 - $i1 === $j2 - $j1
76-
) {
77-
for ($i = 0; $i < $i2 - $i1; ++$i) {
78-
$this->renderChangedExtent($lineRenderer, $old[$i1 + $i], $new[$j1 + $i]);
73+
// if there are same amount of lines replaced
74+
// we can render the inner detailed changes with corresponding lines
75+
if ($op === SequenceMatcher::OP_REP && $i2 - $i1 === $j2 - $j1) {
76+
for ($k = 0; $k < $i2 - $i1; ++$k) {
77+
$this->renderChangedExtent($lineRenderer, $old[$i1 + $k], $new[$j1 + $k]);
7978
}
8079
}
8180

@@ -142,13 +141,17 @@ protected function renderWorker(Differ $differ): string
142141
*/
143142
protected function renderArrayWorker(array $differArray): string
144143
{
145-
return $this->redererChanges($this->ensureChangesUseIntTag($differArray));
144+
$this->ensureChangesUseIntTag($differArray);
145+
146+
return $this->redererChanges($differArray);
146147
}
147148

148149
/**
149150
* Render the array of changes.
150151
*
151152
* @param array $changes the changes
153+
*
154+
* @todo rename typo to renderChanges() in v7
152155
*/
153156
abstract protected function redererChanges(array $changes): string;
154157

@@ -159,8 +162,6 @@ abstract protected function redererChanges(array $changes): string;
159162
* @param string $old the old line
160163
* @param string $new the new line
161164
*
162-
* @throws \InvalidArgumentException
163-
*
164165
* @return static
165166
*/
166167
protected function renderChangedExtent(AbstractLineRenderer $lineRenderer, string &$old, string &$new): self
@@ -321,32 +322,17 @@ function (array $matches): string {
321322
*
322323
* @param array $changes the changes
323324
*/
324-
protected function ensureChangesUseIntTag(array $changes): array
325+
protected function ensureChangesUseIntTag(array &$changes): void
325326
{
326-
if (empty($changes)) {
327-
return [];
328-
}
329-
330-
$isTagInt = true;
331-
foreach ($changes as $blocks) {
332-
foreach ($blocks as $change) {
333-
$isTagInt = \is_int($change['tag']);
334-
335-
break 2;
336-
}
327+
// check if the tag is already int type
328+
if (\is_int($changes[0][0]['tag'] ?? null)) {
329+
return;
337330
}
338331

339-
if (!$isTagInt) {
340-
// convert string tags into their int forms
341-
foreach ($changes as &$blocks) {
342-
foreach ($blocks as &$change) {
343-
$change['tag'] = SequenceMatcher::opStrToInt($change['tag']);
344-
}
332+
foreach ($changes as &$hunks) {
333+
foreach ($hunks as &$block) {
334+
$block['tag'] = SequenceMatcher::opStrToInt($block['tag']);
345335
}
346-
347-
unset($blocks, $change);
348336
}
349-
350-
return $changes;
351337
}
352338
}

src/Renderer/Html/Json.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ protected function redererChanges(array $changes): string
5454
*/
5555
protected function convertTagToString(array &$changes): void
5656
{
57-
foreach ($changes as &$blocks) {
58-
foreach ($blocks as &$change) {
59-
$change['tag'] = SequenceMatcher::opIntToStr($change['tag']);
57+
foreach ($changes as &$hunks) {
58+
foreach ($hunks as &$block) {
59+
$block['tag'] = SequenceMatcher::opIntToStr($block['tag']);
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)