Skip to content

Commit 6344e5f

Browse files
authored
Merge pull request #12 from JBlond/development
Development
2 parents 70fd0e3 + 1c537ff commit 6344e5f

File tree

8 files changed

+103
-131
lines changed

8 files changed

+103
-131
lines changed

lib/jblond/Diff.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @author Chris Boulton <[email protected]>
4545
* @copyright (c) 2009 Chris Boulton
4646
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
47-
* @version 1.8
47+
* @version 1.9
4848
* @link https://github.com/JBlond/php-diff
4949
*/
5050
class Diff
@@ -87,7 +87,7 @@ class Diff
8787
* @param array $b Array containing the lines for the second string to compare.
8888
* @param array $options Array for the options
8989
*/
90-
public function __construct($a, $b, $options = array())
90+
public function __construct(array $a, array $b, array $options = array())
9191
{
9292
$this->a = $a;
9393
$this->b = $b;
@@ -119,10 +119,10 @@ public function render($renderer)
119119
* that line.
120120
*
121121
* @param int $start The starting number.
122-
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
122+
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
123123
* @return array Array of all of the lines between the specified range.
124124
*/
125-
public function getA($start = 0, $end = null) : array
125+
public function getA(int $start = 0, $end = null) : array
126126
{
127127
if ($start == 0 && $end === null) {
128128
return $this->a;
@@ -144,10 +144,10 @@ public function getA($start = 0, $end = null) : array
144144
* that line.
145145
*
146146
* @param int $start The starting number.
147-
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
147+
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
148148
* @return array Array of all of the lines between the specified range.
149149
*/
150-
public function getB($start = 0, $end = null) : array
150+
public function getB(int $start = 0, $end = null) : array
151151
{
152152
if ($start == 0 && $end === null) {
153153
return $this->b;

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

+60-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @author Chris Boulton <[email protected]>
4242
* @copyright (c) 2009 Chris Boulton
4343
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
44-
* @version 1.8
44+
* @version 1.9
4545
* @link https://github.com/JBlond/php-diff
4646
*/
4747

@@ -61,18 +61,18 @@ class HtmlArray extends RendererAbstract
6161

6262
/**
6363
* From https://gist.github.com/stemar/8287074
64-
* @param mixed $string The input string.
65-
* @param mixed $replacement The replacement string.
66-
* @param mixed $start If start is positive, the replacing will begin at the start'th offset into string.
64+
* @param string $string The input string.
65+
* @param string $replacement The replacement string.
66+
* @param int $start If start is positive, the replacing will begin at the start'th offset into string.
6767
* If start is negative, the replacing will begin at the start'th character from the end of string.
68-
* @param mixed $length If given and is positive, it represents the length of the portion of string which is to
68+
* @param int|null $length If given and is positive, it represents the length of the portion of string which is to
6969
* be replaced. If it is negative, it represents the number of characters from the end of string at which to
7070
* stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the
7171
* end of string. Of course, if length is zero then this function will have the effect of inserting replacement
7272
* into string at the given start offset.
7373
* @return string|array The result string is returned. If string is an array then array is returned.
7474
*/
75-
public function mbSubstrReplace($string, $replacement, $start, $length = null)
75+
public function mbSubstrReplace(string $string, string $replacement, int $start, $length = null)
7676
{
7777
if (is_array($string)) {
7878
$num = count($string);
@@ -115,6 +115,54 @@ public function mbSubstrReplace($string, $replacement, $start, $length = null)
115115
return join($smatches['0']);
116116
}
117117

118+
/**
119+
* @param string|array $changes
120+
* @param SideBySide|Inline $object
121+
* @return string
122+
*/
123+
public function renderHtml($changes, $object)
124+
{
125+
$html = '';
126+
if (empty($changes)) {
127+
return $html;
128+
}
129+
130+
$html .= $object->generateTableHeader();
131+
132+
foreach ($changes as $i => $blocks) {
133+
// If this is a separate block, we're condensing code so output ...,
134+
// indicating a significant portion of the code has been collapsed as
135+
// it is the same
136+
if ($i > 0) {
137+
$html .= $object->generateSkippedTable();
138+
}
139+
140+
foreach ($blocks as $change) {
141+
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
142+
switch ($change['tag']) {
143+
// Equal changes should be shown on both sides of the diff
144+
case 'equal':
145+
$html .= $object->generateTableRowsEqual($change);
146+
break;
147+
// Added lines only on the right side
148+
case 'insert':
149+
$html .= $object->generateTableRowsInsert($change);
150+
break;
151+
// Show deleted lines only on the left side
152+
case 'delete':
153+
$html .= $object->generateTableRowsDelete($change);
154+
break;
155+
// Show modified lines on both sides
156+
case 'replace':
157+
$html .= $object->generateTableRowsReplace($change);
158+
break;
159+
}
160+
$html .= '</tbody>';
161+
}
162+
}
163+
$html .= '</table>';
164+
return $html;
165+
}
118166
/**
119167
* Render and return an array structure suitable for generating HTML
120168
* based differences. Generally called by subclasses that generate a
@@ -204,7 +252,7 @@ public function render()
204252
* @param string $toLine The second string.
205253
* @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
206254
*/
207-
private function getChangeExtent($fromLine, $toLine)
255+
private function getChangeExtent(string $fromLine, string $toLine)
208256
{
209257
$start = 0;
210258
$limit = min(mb_strlen($fromLine), mb_strlen($toLine));
@@ -230,7 +278,7 @@ private function getChangeExtent($fromLine, $toLine)
230278
* @param array $lines Array of lines to format.
231279
* @return array Array of the formatted lines.
232280
*/
233-
protected function formatLines($lines)
281+
protected function formatLines(array $lines) : array
234282
{
235283
if ($this->options['tabSize'] !== false) {
236284
$lines = array_map(array($this, 'ExpandTabs'), $lines);
@@ -248,7 +296,7 @@ protected function formatLines($lines)
248296
* @param array $matches The string of spaces.
249297
* @return string The HTML representation of the string.
250298
*/
251-
protected function fixSpaces($matches)
299+
protected function fixSpaces(array $matches) : string
252300
{
253301
$buffer = '';
254302
$count = 0;
@@ -273,7 +321,7 @@ protected function fixSpaces($matches)
273321
* @param string $line The containing tabs to convert.
274322
* @return string The line with the tabs converted to spaces.
275323
*/
276-
private function expandTabs($line)
324+
private function expandTabs(string $line) : string
277325
{
278326
$tabSize = $this->options['tabSize'];
279327
while (($pos = strpos($line, "\t")) !== false) {
@@ -292,7 +340,7 @@ private function expandTabs($line)
292340
* @param string $string The string.
293341
* @return string The string with the HTML characters replaced by entities.
294342
*/
295-
private function htmlSafe($string)
343+
private function htmlSafe(string $string) : string
296344
{
297345
return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
298346
}
@@ -303,7 +351,7 @@ private function htmlSafe($string)
303351
* @param integer $j1
304352
* @return array
305353
*/
306-
private function getDefaultArray($tag, $i1, $j1)
354+
private function getDefaultArray(string $tag, int $i1, int $j1) : array
307355
{
308356
return array
309357
(

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

+8-47
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author Chris Boulton <[email protected]>
4040
* @copyright (c) 2009 Chris Boulton
4141
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42-
* @version 1.8
42+
* @version 1.9
4343
* @link https://github.com/JBlond/php-diff
4444
*/
4545

@@ -57,46 +57,7 @@ class Inline extends HtmlArray
5757
public function render() : string
5858
{
5959
$changes = parent::render();
60-
$html = '';
61-
if (empty($changes)) {
62-
return $html;
63-
}
64-
65-
$html .= $this->generateTableHeader();
66-
67-
foreach ($changes as $i => $blocks) {
68-
// If this is a separate block, we're condensing code so output ...,
69-
// indicating a significant portion of the code has been collapsed as
70-
// it is the same
71-
if ($i > 0) {
72-
$html .= $this->generateSkippedTable();
73-
}
74-
75-
foreach ($blocks as $change) {
76-
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
77-
switch ($change['tag']) {
78-
// Equal changes should be shown on both sides of the diff
79-
case 'equal':
80-
$html .= $this->generateTableRowsEqual($change);
81-
break;
82-
// Added lines only on the right side
83-
case 'insert':
84-
$html .= $this->generateTableRowsInsert($change);
85-
break;
86-
// Show deleted lines only on the left side
87-
case 'delete':
88-
$html .= $this->generateTableRowsDelete($change);
89-
break;
90-
// Show modified lines on both sides
91-
case 'replace':
92-
$html .= $this->generateTableRowsReplace($change);
93-
break;
94-
}
95-
$html .= '</tbody>';
96-
}
97-
}
98-
$html .= '</table>';
99-
return $html;
60+
return parent::renderHtml($changes, $this);
10061
}
10162

10263

@@ -106,7 +67,7 @@ public function render() : string
10667
*
10768
* @return string Html code representation of the table's header.
10869
*/
109-
private function generateTableHeader() : string
70+
public function generateTableHeader() : string
11071
{
11172
$html = '<table class="Differences DifferencesInline">';
11273
$html .= '<thead>';
@@ -124,7 +85,7 @@ private function generateTableHeader() : string
12485
*
12586
* @return string Html code representing empty table body.
12687
*/
127-
private function generateSkippedTable() : string
88+
public function generateSkippedTable() : string
12889
{
12990
$html = '<tbody class="Skipped">';
13091
$html .= '<th>&hellip;</th>';
@@ -140,7 +101,7 @@ private function generateSkippedTable() : string
140101
* @param array &$change Array with data about changes.
141102
* @return string Html code representing one or more rows of text with no difference.
142103
*/
143-
private function generateTableRowsEqual(&$change) : string
104+
public function generateTableRowsEqual(array &$change) : string
144105
{
145106
$html = "";
146107
foreach ($change['base']['lines'] as $no => $line) {
@@ -161,7 +122,7 @@ private function generateTableRowsEqual(&$change) : string
161122
* @param array &$change Array with data about changes.
162123
* @return string Html code representing one or more rows of added text.
163124
*/
164-
private function generateTableRowsInsert(&$change) : string
125+
public function generateTableRowsInsert(array &$change) : string
165126
{
166127
$html = "";
167128
foreach ($change['changed']['lines'] as $no => $line) {
@@ -181,7 +142,7 @@ private function generateTableRowsInsert(&$change) : string
181142
* @param array &$change Array with data about changes.
182143
* @return string Html code representing one or more rows of removed text.
183144
*/
184-
private function generateTableRowsDelete(&$change) : string
145+
public function generateTableRowsDelete(array &$change) : string
185146
{
186147
$html = "";
187148
foreach ($change['base']['lines'] as $no => $line) {
@@ -201,7 +162,7 @@ private function generateTableRowsDelete(&$change) : string
201162
* @param array &$change Array with data about changes.
202163
* @return string Html code representing one or more rows of modified.
203164
*/
204-
private function generateTableRowsReplace(&$change) : string
165+
public function generateTableRowsReplace(array &$change) : string
205166
{
206167
$html = "";
207168

0 commit comments

Comments
 (0)