Skip to content

Commit 550312a

Browse files
committed
Optimize code base #103
1 parent 054d249 commit 550312a

18 files changed

+76
-77
lines changed

example/cli.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use jblond\Diff\Renderer\Text\UnifiedCli;
77

88
// Validate the interpreter.
9-
if (php_sapi_name() !== 'cli') {
9+
if (PHP_SAPI !== 'cli') {
1010
echo 'This script demonstrates console support for the php-diff package.<br>';
1111
echo 'Please execute it from a cli interpreter.';
1212
throw new RuntimeException('Script for CLI use only!');
@@ -17,8 +17,8 @@
1717
require '../vendor/autoload.php';
1818

1919
// Include two sample files for comparison.
20-
$sampleA = file_get_contents(dirname(__FILE__) . '/a.txt');
21-
$sampleB = file_get_contents(dirname(__FILE__) . '/b.txt');
20+
$sampleA = file_get_contents(__DIR__ . '/a.txt');
21+
$sampleB = file_get_contents(__DIR__ . '/b.txt');
2222

2323
$customOptions = [
2424
'context' => 2,

example/example.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
require '../vendor/autoload.php';
1212

1313
// Include two sample files for comparison.
14-
$sampleA = file_get_contents(dirname(__FILE__) . '/a.txt');
15-
$sampleB = file_get_contents(dirname(__FILE__) . '/b.txt');
14+
$sampleA = file_get_contents(__DIR__ . '/a.txt');
15+
$sampleB = file_get_contents(__DIR__ . '/b.txt');
1616

1717
// Options for generating the diff.
1818
$diffOptions = [
1919
'context' => 2,
2020
'trimEqual' => false,
2121
'ignoreWhitespace' => true,
2222
'ignoreCase' => true,
23-
'ignoreLines' => Diff::DIFF_IGNORE_LINE_EMPTY,
23+
'ignoreLines' => Diff\ConstantsInterface::DIFF_IGNORE_LINE_EMPTY,
2424
];
2525

2626
// Choose one of the initializations.
@@ -29,7 +29,7 @@
2929

3030
// Options for rendering the diff.
3131
$rendererOptions = [
32-
'inlineMarking' => $_GET['inlineMarking'] ?? Diff\Renderer\MainRenderer::CHANGE_LEVEL_LINE,
32+
'inlineMarking' => $_GET['inlineMarking'] ?? Diff\Renderer\MainRendererAbstract::CHANGE_LEVEL_LINE,
3333
]
3434
?>
3535
<!DOCTYPE html>

lib/jblond/Diff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function getArgumentType($var): int
148148
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
149149
* Any other keyName (and it's value) will be added as an option, but will not be used if not implemented.
150150
*/
151-
public function setOptions(array $options)
151+
public function setOptions(array $options): void
152152
{
153153
$this->options = array_merge($this->defaultOptions, $options);
154154
}

lib/jblond/Diff/DiffUtils.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public static function tupleSort(array $aArray, array $bArray): int
3030
for ($counter = 0; $counter < $max; ++$counter) {
3131
if ($aArray[$counter] < $bArray[$counter]) {
3232
return -1;
33-
} elseif ($aArray[$counter] > $bArray[$counter]) {
33+
}
34+
35+
if ($aArray[$counter] > $bArray[$counter]) {
3436
return 1;
3537
}
3638
}

lib/jblond/Diff/Renderer/Html/Merged.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function __construct(array $options = [])
6262
*/
6363
public function render()
6464
{
65-
$changes = parent::renderSequences();
65+
$changes = $this->renderSequences();
6666

67-
return parent::renderOutput($changes, $this);
67+
return $this->renderOutput($changes, $this);
6868
}
6969

7070
/**

lib/jblond/Diff/Renderer/Html/SideBySide.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function __construct(array $options = [])
6060
*/
6161
public function render()
6262
{
63-
$changes = parent::renderSequences();
63+
$changes = $this->renderSequences();
6464

65-
return parent::renderOutput($changes, $this);
65+
return $this->renderOutput($changes, $this);
6666
}
6767

6868
/**
@@ -284,7 +284,7 @@ public function generateDiffFooter(): string
284284
}
285285

286286
/**
287-
* @inheritDoc
287+
*
288288
*
289289
* @return string Html code representing table rows showing ignored text.
290290
*/

lib/jblond/Diff/Renderer/Html/Unified.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function __construct(array $options = [])
6060
*/
6161
public function render()
6262
{
63-
$changes = parent::renderSequences();
63+
$changes = $this->renderSequences();
6464

65-
return parent::renderOutput($changes, $this);
65+
return $this->renderOutput($changes, $this);
6666
}
6767

6868
/**
@@ -220,7 +220,7 @@ public function generateLinesReplace(array $changes): string
220220
}
221221

222222
/**
223-
* @inheritDoc
223+
*
224224
*
225225
* @return string Html code representing table rows showing modified text.
226226
*/

lib/jblond/Diff/Renderer/MainRenderer.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function renderOutput(array $changes, object $subRenderer)
6868
$deprecationTriggered = false;
6969
foreach ($blocks as $change) {
7070
if (
71-
$subRenderer instanceof MainRenderer &&
71+
$subRenderer instanceof self &&
7272
!method_exists($subRenderer, 'generateLinesIgnore') &&
73-
$change['tag'] == 'ignore'
73+
$change['tag'] === 'ignore'
7474
) {
7575
if (!$deprecationTriggered) {
7676
trigger_error(
@@ -157,7 +157,7 @@ protected function renderSequences(): array
157157
$blockSizeOld = $endOld - $startOld;
158158
$blockSizeNew = $endNew - $startNew;
159159

160-
if (($tag == 'replace') && ($blockSizeOld == $blockSizeNew)) {
160+
if (($tag === 'replace') && ($blockSizeOld == $blockSizeNew)) {
161161
// Inline differences between old and new block.
162162
$this->markInlineChanges($oldText, $newText, $startOld, $endOld, $startNew);
163163
}
@@ -168,14 +168,14 @@ protected function renderSequences(): array
168168
$oldBlock = $this->formatLines(array_slice($oldText, $startOld, $blockSizeOld));
169169
$newBlock = $this->formatLines(array_slice($newText, $startNew, $blockSizeNew));
170170

171-
if ($tag != 'delete' && $tag != 'insert') {
171+
if ($tag !== 'delete' && $tag !== 'insert') {
172172
// Old block "equals" New block or is replaced.
173173
$blocks[$lastBlock]['base']['lines'] += $oldBlock;
174174
$blocks[$lastBlock]['changed']['lines'] += $newBlock;
175175
continue;
176176
}
177177

178-
if ($tag == 'delete') {
178+
if ($tag === 'delete') {
179179
// Block of version1 doesn't exist in version2.
180180
$blocks[$lastBlock]['base']['lines'] += $oldBlock;
181181
continue;
@@ -266,14 +266,14 @@ private function markInnerChange(array &$oldText, array &$newText, int $startOld
266266

267267
foreach ($opCodes as $group) {
268268
foreach ($group as [$tag, $changeStartOld, $changeEndOld, $changeStartNew, $changeEndNew]) {
269-
if ($tag == 'equal') {
269+
if ($tag === 'equal') {
270270
continue;
271271
}
272-
if ($tag == 'replace' || $tag == 'delete') {
272+
if ($tag === 'replace' || $tag === 'delete') {
273273
$oldLine[$changeStartOld] = "\0" . $oldLine[$changeStartOld];
274274
$oldLine[$changeEndOld] = "\1" . $oldLine[$changeEndOld];
275275
}
276-
if ($tag == 'replace' || $tag == 'insert') {
276+
if ($tag === 'replace' || $tag === 'insert') {
277277
$newLine[$changeStartNew] = "\0" . $newLine[$changeStartNew];
278278
$newLine[$changeEndNew] = "\1" . $newLine[$changeEndNew];
279279
}
@@ -380,7 +380,7 @@ private function getOuterChange(string $oldString, string $newString): array
380380
}
381381

382382
$end = -1;
383-
$limit = $limit - $start;
383+
$limit -= $start;
384384

385385
// Find the position of the last character which is different between old and new.
386386
// Starts at the end of the shortest string.
@@ -453,10 +453,10 @@ function ($line) {
453453
);
454454
}
455455

456-
if (strtolower($this->options['format']) == 'html') {
456+
if (strtolower($this->options['format']) === 'html') {
457457
// Convert special characters to HTML entities
458458
$strings = array_map(
459-
function ($line) {
459+
static function ($line) {
460460
return htmlspecialchars($line, ENT_NOQUOTES);
461461
},
462462
$strings
@@ -466,7 +466,7 @@ function ($line) {
466466
foreach ($strings as &$line) {
467467
$line = preg_replace_callback(
468468
'/(^[ \0\1]*)/',
469-
function ($matches) {
469+
static function ($matches) {
470470
return str_replace(' ', '&nbsp;', $matches[0]);
471471
},
472472
$line

lib/jblond/Diff/Renderer/MainRendererAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct(array $options = [])
9595
* @see MainRendererAbstract::$mainOptions
9696
*
9797
*/
98-
public function setOptions(array $options)
98+
public function setOptions(array $options): void
9999
{
100100
$this->options = array_merge($this->mainOptions, $this->options, $options);
101101
}

lib/jblond/Diff/Renderer/Text/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private function filterGroups(array $groups, string $excludedTag): array
110110
{
111111
return array_filter(
112112
$groups,
113-
function ($operation) use ($excludedTag) {
113+
static function ($operation) use ($excludedTag) {
114114
return $operation[0] != $excludedTag;
115115
}
116116
);

0 commit comments

Comments
 (0)