Skip to content

Commit 281579a

Browse files
committed
Merge branch 'master' into inlineCli
# Conflicts: # lib/jblond/Diff/Renderer/Html/Inline.php # lib/jblond/Diff/Renderer/Html/SideBySide.php # lib/jblond/Diff/Renderer/Html/Unified.php # lib/jblond/Diff/Renderer/MainRenderer.php # lib/jblond/Diff/Renderer/RendererAbstract.php # lib/jblond/Diff/Renderer/Text/UnifiedCli.php # tests/Diff/Renderer/Text/TextRenderersTest.php
2 parents 0ef6def + eda111b commit 281579a

File tree

7 files changed

+93
-59
lines changed

7 files changed

+93
-59
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ modification, are permitted provided that the following conditions are met:
1212
- Redistributions in binary form must reproduce the above copyright notice,
1313
this list of conditions and the following disclaimer in the documentation
1414
and/or other materials provided with the distribution.
15-
- Neither the name of the Chris Boulton nor the names of its contributors
15+
- Neither the name of the Mario Brandt nor the names of its contributors
1616
may be used to endorse or promote products derived from this software
1717
without specific prior written permission.
1818

lib/jblond/Diff.php

Lines changed: 13 additions & 12 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]>
28-
* @copyright (c) 2009 Chris Boulton
29-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
30-
* @version 2.0.0
31-
* @link https://github.com/JBlond/php-diff
24+
* @package jblond
25+
* @author Chris Boulton <[email protected]>
26+
* @author Mario Brandt <[email protected]>
27+
* @author Ferry Cools <[email protected]>
28+
* @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
3232
*/
3333
class Diff
3434
{
@@ -50,8 +50,9 @@ class Diff
5050
private $groupedCodes;
5151

5252
/**
53-
* @var array Associative array containing the default options available for the diff class and their default
54-
* value.
53+
* @var array<string, string> Associative array containing the default options available
54+
* for the diff class and their default value.
55+
*
5556
* - context The amount of lines to include around blocks that differ.
5657
* - trimEqual Strip blocks of equal lines from the start and end of the text.
5758
* - ignoreWhitespace When true, tabs and spaces are ignored while comparing.
@@ -111,8 +112,8 @@ public function __construct($version1, $version2, array $options = [])
111112
*
112113
* @param mixed $var Variable to get type from.
113114
*
114-
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
115-
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
115+
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
116+
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
116117
*
117118
*/
118119
public function getArgumentType($var): int

lib/jblond/Diff/DiffUtils.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*
88
* PHP version 7.2 or greater
99
*
10-
* @package jblond\Diff
11-
* @author Mario Brandt <[email protected]>
12-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
13-
* @version 2.0.0
14-
* @link https://github.com/JBlond/php-diff
10+
* @package jblond\Diff
11+
* @author Mario Brandt <[email protected]>
12+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
13+
* @version 2.1.1
14+
* @link https://github.com/JBlond/php-diff
1515
*/
1616
class DiffUtils
1717
{

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*
1212
* PHP version 7.2 or greater
1313
*
14-
* @package jblond\Diff\Renderer\Text
15-
* @author Chris Boulton <[email protected]>
16-
* @author Mario Brandt <[email protected]>
17-
* @author Ferry Cools <[email protected]>
18-
* @copyright (c) 2009 Chris Boulton
19-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20-
* @version 2.0.0
21-
* @link https://github.com/JBlond/php-diff
14+
* @package jblond\Diff\Renderer\Text
15+
* @author Chris Boulton <[email protected]>
16+
* @author Mario Brandt <[email protected]>
17+
* @author Ferry Cools <[email protected]>
18+
* @copyright (c) 2009 Chris Boulton
19+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20+
* @version 2.1.1
21+
* @link https://github.com/JBlond/php-diff
2222
*/
2323
class Context extends MainRendererAbstract
2424
{

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
*
1212
* PHP version 7.2 or greater
1313
*
14-
* @package jblond\Diff\Renderer\Text
15-
* @author Chris Boulton <[email protected]>
16-
* @author Mario Brandt <[email protected]>
17-
* @copyright (c) 2009 Chris Boulton
18-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
19-
* @version 2.0.0
20-
* @link https://github.com/JBlond/php-diff
14+
* @package jblond\Diff\Renderer\Text
15+
* @author Chris Boulton <[email protected]>
16+
* @author Mario Brandt <[email protected]>
17+
* @copyright (c) 2020 Mario Brandt
18+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
19+
* @version 2.1.1
20+
* @link https://github.com/JBlond/php-diff
2121
*/
2222

2323
/**

lib/jblond/Diff/SequenceMatcher.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*
1212
* PHP version 7.2 or greater
1313
*
14-
* @package jblond\Diff
15-
* @author Chris Boulton <[email protected]>
16-
* @author Mario Brandt <[email protected]>
17-
* @author Ferry Cools <[email protected]>
18-
* @copyright (c) 2009 Chris Boulton
19-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20-
* @version 2.0.0
21-
* @link https://github.com/JBlond/php-diff
14+
* @package jblond\Diff
15+
* @author Chris Boulton <[email protected]>
16+
* @author Mario Brandt <[email protected]>
17+
* @author Ferry Cools <[email protected]>
18+
* @copyright (c) 2020 Mario Brandt
19+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20+
* @version 2.1.1
21+
* @link https://github.com/JBlond/php-diff
2222
*/
2323
class SequenceMatcher
2424
{
@@ -79,9 +79,9 @@ class SequenceMatcher
7979
* sequence matcher and it will perform a basic cleanup & calculate junk
8080
* elements.
8181
*
82-
* @param string|array $old A string or array containing the lines to compare against.
83-
* @param string|array $new A string or array containing the lines to compare.
84-
* @param array $options
82+
* @param string|array $old A string or array containing the lines to compare against.
83+
* @param string|array $new A string or array containing the lines to compare.
84+
* @param array $options
8585
* @param string|array|null $junkCallback Either an array or string that references a callback function
8686
* (if there is one) to determine 'junk' characters.
8787
*/
@@ -212,7 +212,7 @@ private function chainB()
212212
* for the list of junk characters.
213213
*
214214
* @param string $bString
215-
* @return bool $b True if the character is considered junk. False if not.
215+
* @return bool True if the character is considered junk. False if not.
216216
*/
217217
private function isBJunk(string $bString): bool
218218
{
@@ -555,7 +555,7 @@ public function getGroupedOpCodes(): array
555555

556556
if ($this->options['trimEqual']) {
557557
if ($opCodes['0']['0'] == 'equal') {
558-
// Remove sequences at the start which are out of context.
558+
// Remove sequences at the start of the text, but keep the context lines.
559559
$opCodes['0'] = [
560560
$opCodes['0']['0'],
561561
max($opCodes['0']['1'], $opCodes['0']['2'] - $this->options['context']),
@@ -568,7 +568,7 @@ public function getGroupedOpCodes(): array
568568
$lastItem = count($opCodes) - 1;
569569
if ($opCodes[$lastItem]['0'] == 'equal') {
570570
[$tag, $item1, $item2, $item3, $item4] = $opCodes[$lastItem];
571-
// Remove sequences at the end which are out of context.
571+
// Remove sequences at the end of the text, but keep the context lines.
572572
$opCodes[$lastItem] = [
573573
$tag,
574574
$item1,
@@ -607,8 +607,8 @@ public function getGroupedOpCodes(): array
607607
];
608608
}
609609

610-
if ($this->options['trimEqual'] || (!empty($group) && !(count($group) == 1 && $group[0][0] == 'equal'))) {
611-
//Do not add the last sequences. They're out of context.
610+
if (!$this->options['trimEqual'] || (!empty($group) && !(count($group) == 1 && $group[0][0] == 'equal'))) {
611+
// Add the last sequences when !trimEqual || When there are no differences between both versions.
612612
$groups[] = $group;
613613
}
614614

tests/Diff/SequenceMatcherTest.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,72 @@ public function __construct($name = null, array $data = [], $dataName = '')
3434
parent::__construct($name, $data, $dataName);
3535
}
3636

37-
public function testGetGroupedOpCodes()
37+
public function testGetGroupedOpCodesDefault()
3838
{
3939
// Test with default options.
40-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345');
40+
$sequenceMatcher = new SequenceMatcher(
41+
'54321ABXDE12345',
42+
'54321ABxDE12345'
43+
);
44+
4145
$this->assertEquals(
42-
[[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
46+
[
47+
[
48+
['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]
49+
]
50+
],
4351
$sequenceMatcher->getGroupedOpCodes()
4452
);
53+
}
4554

55+
public function testGetGroupedOpCodesTrimEqualFalse()
56+
{
4657
// Test with trimEqual disabled.
47-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['trimEqual' => false]);
58+
// First and last context lines of the sequences are included.
59+
$sequenceMatcher = new SequenceMatcher(
60+
'54321ABXDE12345',
61+
'54321ABxDE12345',
62+
['trimEqual' => false]
63+
);
64+
4865
$this->assertEquals(
49-
[[['equal', 0, 3, 0, 3]], [['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
66+
[
67+
[['equal', 0, 3, 0, 3]],
68+
[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]],
69+
[['equal', 12, 15, 12, 15]],
70+
],
5071
$sequenceMatcher->getGroupedOpCodes()
5172
);
73+
}
5274

53-
// Test with ignoreWhitespace enabled.
75+
public function testGetGroupedOpCodesIgnoreWhitespaceTrue()
76+
{
77+
// Test with ignoreWhitespace enabled. Both sequences are considered to be the same.
5478
// Note: The sequenceMatcher evaluates the string character by character. Option ignoreWhitespace will ignore
5579
// if the difference if the character is a tab in one sequence and a space in the other.
5680
$sequenceMatcher = new SequenceMatcher(
5781
"\t54321ABXDE12345 ",
5882
" 54321ABXDE12345\t",
5983
['ignoreWhitespace' => true]
6084
);
85+
6186
$this->assertEquals(
62-
[[['equal', 14, 17, 14, 17]]],
87+
[],
6388
$sequenceMatcher->getGroupedOpCodes()
6489
);
90+
}
91+
92+
public function testGetGroupedOpCodesIgnoreCaseTrue()
93+
{
94+
// Test with ignoreCase enabled. Both sequences are considered to be the same.
95+
$sequenceMatcher = new SequenceMatcher(
96+
'54321ABXDE12345',
97+
'54321ABxDE12345',
98+
['ignoreCase' => true]
99+
);
65100

66-
// Test with ignoreCase enabled.
67-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['ignoreCase' => true]);
68101
$this->assertEquals(
69-
[[['equal', 12, 15, 12, 15]]],
102+
[],
70103
$sequenceMatcher->getGroupedOpCodes()
71104
);
72105
}

0 commit comments

Comments
 (0)