Skip to content

Commit 51b3777

Browse files
authored
Merge pull request #54 from JBlond/php-diff-52
Resolve #52 and fixes.
2 parents 10986ac + 2be0c51 commit 51b3777

12 files changed

+126
-98
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## Introduction
88

99
A comprehensive library for generating differences between two hashable objects (strings or arrays).
10-
Generated differences can be rendered in all of the standard formats including:
10+
Generated differences can be rendered in all the standard formats including:
1111

1212
* Unified
1313
* Context
@@ -35,8 +35,8 @@ use jblond\Diff\Renderer\Html\SideBySide;
3535
// Installed via composer...
3636
require 'vendor/autoload.php';
3737

38-
$a = file_get_contents(dirname(__FILE__).'/a.txt');
39-
$b = file_get_contents(dirname(__FILE__).'/b.txt');
38+
$sampleA = file_get_contents(dirname(__FILE__).'/a.txt');
39+
$sampleB = file_get_contents(dirname(__FILE__).'/b.txt');
4040

4141
// Options for generating the diff.
4242
$options = [
@@ -47,20 +47,26 @@ $options = [
4747
];
4848

4949
// Initialize the diff class.
50-
$diff = new Diff($a, $b /*, $options */);
50+
$diff = new Diff($sampleA, $sampleB /*, $options */);
5151

5252
// Choose Renderer.
5353
$renderer = new SideBySide([
54-
'title1' => 'Custom title for OLD version',
55-
'title2' => 'Custom title for NEW version',
54+
'title1' => 'Custom title for sample A',
55+
'title2' => 'Custom title for sample B',
5656
]);
5757

58-
// Show it.
58+
// Show the output of the difference renderer.
5959
echo $diff->Render($renderer);
60+
61+
// Alternative
62+
// Show the differences or a message.
63+
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>' ;
64+
6065
```
6166

6267
### Example Output
63-
A quick usage example can be found in the `example/` directory and under example.php. Included is a light theme and a dark theme.
68+
File `example.php` contains a quick demo and can be found in the `example/` directory.
69+
Included is a light and a dark theme.
6470

6571
#### HTML Side By Side Example
6672

example/example.php

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,73 @@
2323
];
2424

2525
// Choose one of the initializations.
26-
$diff = new Diff($sampleA, $sampleB); // Initialize the diff class with default options.
27-
//$diff = new Diff($a, $b, $customOptions); // Initialize the diff class with custom options.
26+
$diff = new Diff($sampleA, $sampleB); // Initialize the diff class with default options.
27+
//$diff = new Diff($sampleA, $sampleB, $customOptions); // Initialize the diff class with custom options.
2828
?><!DOCTYPE html>
2929
<html lang="en">
30-
<head>
31-
<meta charset="utf-8"/>
32-
<title>PHP LibDiff - Examples</title>
33-
<link rel="stylesheet" type="text/css" href="styles.css" />
34-
<script>
35-
function changeCSS(cssFile, cssLinkIndex) {
36-
37-
const oldLink = document.getElementsByTagName('link').item(cssLinkIndex);
38-
39-
const newLink = document.createElement('link');
40-
newLink.setAttribute('rel', 'stylesheet');
41-
newLink.setAttribute('type', 'text/css');
42-
newLink.setAttribute('href', cssFile);
43-
44-
document.getElementsByTagName('head').item(0).replaceChild(newLink, oldLink);
45-
}
46-
</script>
47-
</head>
48-
<body>
49-
<h1>PHP LibDiff - Examples</h1>
50-
<aside>
51-
<h2>Change Theme</h2>
52-
<a href="#" onclick="changeCSS('styles.css', 0);">Light Theme</a>
53-
<a href="#" onclick="changeCSS('dark-theme.css', 0);">Dark Theme</a>
54-
</aside>
55-
<hr>
56-
57-
<h2>HTML Side by Side Diff</h2>
58-
59-
<?php
30+
<head>
31+
<meta charset="utf-8"/>
32+
<title>PHP LibDiff - Examples</title>
33+
<link rel="stylesheet" type="text/css" href="styles.css"/>
34+
<script>
35+
function changeCSS(cssFile, cssLinkIndex) {
36+
37+
const oldLink = document.getElementsByTagName('link').item(cssLinkIndex);
38+
39+
const newLink = document.createElement('link');
40+
newLink.setAttribute('rel', 'stylesheet');
41+
newLink.setAttribute('type', 'text/css');
42+
newLink.setAttribute('href', cssFile);
43+
44+
document.getElementsByTagName('head').item(0).replaceChild(newLink, oldLink);
45+
}
46+
</script>
47+
</head>
48+
<body>
49+
<h1>PHP LibDiff - Examples</h1>
50+
<aside>
51+
<h2>Change Theme</h2>
52+
<a href="#" onclick="changeCSS('styles.css', 0);">Light Theme</a>
53+
<a href="#" onclick="changeCSS('dark-theme.css', 0);">Dark Theme</a>
54+
</aside>
55+
<hr>
56+
57+
<h2>HTML Side by Side Diff</h2>
58+
59+
<?php
6060
// Generate a side by side diff.
6161
$renderer = new SideBySide();
62-
echo $diff->Render($renderer);
63-
?>
64-
65-
<h2>HTML Inline Diff</h2>
66-
67-
<?php
62+
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
63+
?>
6864

65+
<h2>HTML Inline Diff</h2>
66+
<?php
6967
// Generate an inline diff.
7068
$renderer = new Inline();
71-
echo $diff->render($renderer);
72-
?>
69+
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
70+
?>
7371

74-
<h2>HTML Unified Diff</h2>
75-
<?php
72+
<h2>HTML Unified Diff</h2>
73+
<?php
7674
// Generate a unified diff.
7775
$renderer = new HtmlUnified();
78-
echo "<pre>{$diff->render($renderer)}</pre>";
79-
?>
76+
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . $diff->Render($renderer) . '</pre>';
77+
?>
8078

81-
<h2>Text Unified Diff</h2>
82-
<?php
79+
<h2>Text Unified Diff</h2>
80+
<?php
8381
// Generate a unified diff.
8482
$renderer = new Unified();
85-
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
86-
?>
83+
echo $diff->isIdentical() ?
84+
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
85+
?>
8786

88-
<h2>Text Context Diff</h2>
89-
<?php
87+
<h2>Text Context Diff</h2>
88+
<?php
9089
// Generate a context diff.
9190
$renderer = new Context();
92-
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
93-
?>
94-
</body>
91+
echo $diff->isIdentical() ?
92+
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
93+
?>
94+
</body>
9595
</html>

lib/jblond/Diff.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
*
2222
* PHP version 7.2 or greater
2323
*
24-
* @package jblond
25-
* @author Chris Boulton <[email protected]>
26-
* @author Mario Brandt <[email protected]>
27-
* @author Ferry Cools <[email protected]>
24+
* @package jblond
25+
* @author Chris Boulton <[email protected]>
26+
* @author Mario Brandt <[email protected]>
27+
* @author Ferry Cools <[email protected]>
2828
* @copyright (c) 2020 Mario Brandt
29-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
30-
* @version 2.1.1
31-
* @link https://github.com/JBlond/php-diff
29+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
30+
* @version 2.1.1
31+
* @link https://github.com/JBlond/php-diff
3232
*/
3333
class Diff
3434
{
@@ -70,9 +70,14 @@ class Diff
7070
/**
7171
* @var array Associative array containing the options that will be applied for generating the diff.
7272
* The key-value pairs are set at the constructor of this class.
73+
*
7374
* @see Diff::setOptions()
7475
*/
7576
private $options = [];
77+
/**
78+
* @var bool True when compared versions are identical, False otherwise.
79+
*/
80+
private $identical;
7681

7782
/**
7883
* The constructor.
@@ -169,9 +174,9 @@ public function getVersion2(): array
169174
* Render a diff-view using a rendering class and get its results.
170175
*
171176
* @param object|Context|Unified|UnifiedHtml|Inline|SideBySide $renderer An instance of the rendering object,
172-
* used for generating the diff-view.
177+
* used for generating the diff-view.
173178
*
174-
* @return mixed The generated diff-view. The type of the return value depends on the applied rendereder.
179+
* @return mixed The generated diff-view. The type of the return value depends on the applied renderer.
175180
*/
176181
public function render(object $renderer)
177182
{
@@ -221,6 +226,20 @@ public function getArrayRange(array $array, int $start = 0, $end = null): array
221226
return array_slice($array, $start, $length);
222227
}
223228

229+
/**
230+
* Get if the compared versions are identical or have differences.
231+
*
232+
* @return bool True when identical.
233+
*/
234+
public function isIdentical(): bool
235+
{
236+
if ($this->groupedCodes === null) {
237+
$this->getGroupedOpCodes();
238+
}
239+
240+
return $this->identical;
241+
}
242+
224243
/**
225244
* Generate a list of the compiled and grouped op-codes for the differences between two strings.
226245
*
@@ -240,6 +259,8 @@ public function getGroupedOpCodes(): array
240259
//Get and cache the grouped op-codes.
241260
$sequenceMatcher = new SequenceMatcher($this->version1, $this->version2, $this->options);
242261
$this->groupedCodes = $sequenceMatcher->getGroupedOpCodes();
262+
$opCodes = $sequenceMatcher->getOpCodes();
263+
$this->identical = count($opCodes) == 1 && $opCodes[0][0] == 'equal';
243264

244265
return $this->groupedCodes;
245266
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function __construct(array $options = [])
5757
/**
5858
* Render and return a diff-view with changes between the two sequences displayed inline (under each other).
5959
*
60-
* @return string The generated inline diff-view.
60+
* @return string|false The generated diff-view or false when there's no difference.
6161
*/
62-
public function render(): string
62+
public function render()
6363
{
6464
$changes = parent::renderSequences();
6565

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function __construct(array $options = [])
5757
/**
5858
* Render a and return diff-view with changes between the two sequences displayed side by side.
5959
*
60-
* @return string The generated side by side diff-view.
60+
* @return string|false The generated diff-view or false when there's no difference.
6161
*/
62-
public function render(): string
62+
public function render()
6363
{
6464
$changes = parent::renderSequences();
6565

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function __construct(array $options = [])
5757
/**
5858
* Render a and return diff-view with changes between the two sequences (under each other).
5959
*
60-
* @return string The generated unified diff-view.
60+
* @return string|false The generated diff-view or false when there's no difference.
6161
*/
62-
public function render(): string
62+
public function render()
6363
{
6464
$changes = parent::renderSequences();
6565

lib/jblond/Diff/Renderer/MainRenderer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class MainRenderer extends MainRendererAbstract
2222
{
2323
/**
24-
* @var int
24+
* @var int Character count of the line marker with the most characters.
2525
*/
2626
protected $maxLineMarkerWidth = 0;
2727
/**
@@ -31,20 +31,20 @@ class MainRenderer extends MainRendererAbstract
3131
private $lastTag;
3232

3333
/**
34-
* Generate a string representation of changes between the "old and "new" sequences.
34+
* Generate a string representation of changes between version1 and version2.
3535
*
3636
* This method is called by the renderers which extends this class.
3737
*
38-
* @param array $changes Contains the op-codes about the differences between "old and "new".
38+
* @param array $changes Contains the op-codes about the differences between version1 and version2.
3939
* @param object $subRenderer Renderer which is subClass of this class.
4040
*
41-
* @return string Representation of the differences.
41+
* @return string|false String representation of the differences or false when versions are identical.
4242
*/
43-
public function renderOutput(array $changes, object $subRenderer): string
43+
public function renderOutput(array $changes, object $subRenderer)
4444
{
4545
if (!$changes) {
46-
//No changes between "old" and "new"
47-
return 'No differences found.';
46+
//No changes between version1 and version2
47+
return false;
4848
}
4949

5050
$output = $subRenderer->generateDiffHeader();

lib/jblond/Diff/Renderer/SubRendererInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ interface SubRendererInterface
2121
/**
2222
* Render and return a diff-view with changes between two sequences.
2323
*
24-
* @return string The generated diff-view.
24+
* @return string|false The generated diff-view or false when there's no difference.
2525
*/
26-
public function render(): string;
26+
public function render();
2727

2828
/**
2929
* Generate a string representation of the start of a diff view.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class Context extends MainRendererAbstract
3737
*
3838
* @link https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Context.html#Detailed-Context
3939
*
40-
* @return string The generated context diff-view.
40+
* @return string|false The generated diff-view or false when there's no difference.
4141
*/
42-
public function render(): string
42+
public function render()
4343
{
44-
$diff = '';
44+
$diff = false;
4545
$opCodes = $this->diff->getGroupedOpCodes();
4646

4747
foreach ($opCodes as $group) {

0 commit comments

Comments
 (0)