Skip to content

Commit c5f6d72

Browse files
committed
Fixes #64 - maxLineMarkerWidth only calculated for input format plain.
- Calculation of maxLineMarkerWidth independent of input format. - Second parameter of string repeat function minimizes to 0. - Code cleanup.
1 parent 836b128 commit c5f6d72

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

lib/jblond/Diff/Renderer/MainRenderer.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class MainRenderer extends MainRendererAbstract
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 version1 and version2.
39-
* @param object $subRenderer Renderer which is subClass of this class.
38+
* @param array $changes Contains the op-codes about the differences between version1 and version2.
39+
* @param object $subRenderer Renderer which is subClass of this class.
4040
*
4141
* @return string|false String representation of the differences or false when versions are identical.
4242
*/
@@ -56,15 +56,12 @@ public function renderOutput(array $changes, object $subRenderer)
5656
$output .= $subRenderer->generateSkippedLines();
5757
}
5858

59-
if ($this->options['format'] == 'plain') {
60-
$this->maxLineMarkerWidth =
61-
max(
62-
strlen($this->options['insertMarkers'][0]),
63-
strlen($this->options['deleteMarkers'][0]),
64-
strlen($this->options['equalityMarkers'][0]),
65-
strlen($this->options['equalityMarkers'][1])
66-
);
67-
}
59+
$this->maxLineMarkerWidth = max(
60+
strlen($this->options['insertMarkers'][0]),
61+
strlen($this->options['deleteMarkers'][0]),
62+
strlen($this->options['equalityMarkers'][0]),
63+
strlen($this->options['equalityMarkers'][1])
64+
);
6865

6966
foreach ($blocks as $change) {
7067
$output .= $subRenderer->generateBlockHeader($change);
@@ -184,11 +181,11 @@ protected function renderSequences(): array
184181
* New => "ab123fg" End marker inserted at position 6
185182
* </pre>
186183
*
187-
* @param array $oldText Collection of lines of old text.
188-
* @param array $newText Collection of lines of new text.
189-
* @param int $startOld First line of the block in old to replace.
190-
* @param int $endOld last line of the block in old to replace.
191-
* @param int $startNew First line of the block in new to replace.
184+
* @param array $oldText Collection of lines of old text.
185+
* @param array $newText Collection of lines of new text.
186+
* @param int $startOld First line of the block in old to replace.
187+
* @param int $endOld last line of the block in old to replace.
188+
* @param int $startNew First line of the block in new to replace.
192189
*/
193190
private function markInlineChange(array &$oldText, array &$newText, int $startOld, int $endOld, int $startNew)
194191
{
@@ -231,8 +228,8 @@ private function markInlineChange(array &$oldText, array &$newText, int $startOl
231228
* The second element defines the last (starting at -0) character from the end of the old string which is different.
232229
*
233230
*
234-
* @param string $oldString The first string to compare.
235-
* @param string $newString The second string to compare.
231+
* @param string $oldString The first string to compare.
232+
* @param string $newString The second string to compare.
236233
*
237234
* @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
238235
*/
@@ -270,10 +267,10 @@ private function getInlineChange(string $oldString, string $newString): array
270267
*
271268
* The index of the last element of the array is always returned.
272269
*
273-
* @param array $blocks The array which keeps the changes for the HTML renderer.
274-
* @param string $tag Kind of difference.
275-
* @param integer $lineInOld Start of block in "old".
276-
* @param integer $lineInNew Start of block in "new".
270+
* @param array $blocks The array which keeps the changes for the HTML renderer.
271+
* @param string $tag Kind of difference.
272+
* @param integer $lineInOld Start of block in "old".
273+
* @param integer $lineInNew Start of block in "new".
277274
*
278275
* @return int The index of the last element.
279276
*/
@@ -306,7 +303,7 @@ private function appendChangesArray(array &$blocks, string $tag, int $lineInOld,
306303
* This involves replacing tab characters with spaces, making the HTML safe for output by ensuring that double
307304
* spaces are replaced with &nbsp; etc.
308305
*
309-
* @param array $strings Array of strings to format.
306+
* @param array $strings Array of strings to format.
310307
*
311308
* @return array Array of formatted strings.
312309
*/

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*
1414
* PHP version 7.2 or greater
1515
*
16-
* @package jblond\Diff\Renderer\Text
17-
* @author Ferry Cools <[email protected]>
16+
* @package jblond\Diff\Renderer\Text
17+
* @author Ferry Cools <[email protected]>
1818
* @copyright (c) 2020 Ferry Cools
19-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20-
* @version 2.2.1
21-
* @link https://github.com/JBlond/php-diff
19+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
20+
* @version 2.2.1
21+
* @link https://github.com/JBlond/php-diff
2222
*/
2323
class InlineCli extends MainRenderer implements SubRendererInterface
2424
{
@@ -31,7 +31,7 @@ class InlineCli extends MainRenderer implements SubRendererInterface
3131
/**
3232
* InlineCli constructor.
3333
*
34-
* @param array $options Custom defined options for the inline diff renderer.
34+
* @param array $options Custom defined options for the inline diff renderer.
3535
*
3636
* @see Inline::$subOptions
3737
*/
@@ -67,7 +67,7 @@ public function generateDiffHeader(): string
6767
/**
6868
* Generate a string representation of the start of a block.
6969
*
70-
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
70+
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
7171
*
7272
* @return string Start of the diff view.
7373
*/
@@ -89,14 +89,17 @@ public function generateSkippedLines(): string
8989
/**
9090
* Generate a string representation lines without differences between the two versions.
9191
*
92-
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
92+
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
9393
*
9494
* @return string Text with no difference.
9595
*/
9696
public function generateLinesEqual(array $changes): string
9797
{
9898
$returnValue = '';
99-
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][0]));
99+
$padding = str_repeat(
100+
' ',
101+
max($this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][0]), 0)
102+
);
100103

101104
foreach ($changes['base']['lines'] as $line) {
102105
$returnValue .= $this->options['equalityMarkers'][0] . $padding . '|' . $line . "\n";
@@ -108,15 +111,18 @@ public function generateLinesEqual(array $changes): string
108111
/**
109112
* Generate a string representation of lines that are added to the 2nd version.
110113
*
111-
* @param array $changes Contains the op-codes about the changes between two blocks of text.
114+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
112115
*
113116
* @return string Added text.
114117
*/
115118
public function generateLinesInsert(array $changes): string
116119
{
117120
$colorize = new CliColors();
118121
$returnValue = '';
119-
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['insertMarkers'][0]));
122+
$padding = str_repeat(
123+
' ',
124+
max($this->maxLineMarkerWidth - strlen($this->options['insertMarkers'][0]), 0)
125+
);
120126

121127
foreach ($changes['changed']['lines'] as $line) {
122128
if ($this->options['cliColor']) {
@@ -132,15 +138,18 @@ public function generateLinesInsert(array $changes): string
132138
/**
133139
* Generate a string representation of lines that are removed from the 2nd version.
134140
*
135-
* @param array $changes Contains the op-codes about the changes between two blocks of text.
141+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
136142
*
137143
* @return string Removed text.
138144
*/
139145
public function generateLinesDelete(array $changes): string
140146
{
141147
$colorize = new CliColors();
142148
$returnValue = '';
143-
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['deleteMarkers'][0]));
149+
$padding = str_repeat(
150+
' ',
151+
max($this->maxLineMarkerWidth - strlen($this->options['deleteMarkers'][0]), 0)
152+
);
144153

145154
foreach ($changes['base']['lines'] as $line) {
146155
if ($this->options['cliColor']) {
@@ -156,7 +165,7 @@ public function generateLinesDelete(array $changes): string
156165
/**
157166
* Generate a string representation of lines that are partially modified.
158167
*
159-
* @param array $changes Contains the op-codes about the changes between two blocks of text.
168+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
160169
*
161170
* @return string Modified text.
162171
*/
@@ -179,10 +188,10 @@ public function generateLinesReplace(array $changes): string
179188
/**
180189
* Merge the changes between two lines together and mark these changes.
181190
*
182-
* @param array $baseLines Lines of version 1.
183-
* @param array $changedLines Lines of version 2.
184-
* @param array|null[] $deleteColors Fore- and background colors of part that is removed from the 2nd version.
185-
* @param array|null[] $insertColors Fore- and background colors of part that is added to the 2nd version.
191+
* @param array $baseLines Lines of version 1.
192+
* @param array $changedLines Lines of version 2.
193+
* @param array|null[] $deleteColors Fore- and background colors of part that is removed from the 2nd version.
194+
* @param array|null[] $insertColors Fore- and background colors of part that is added to the 2nd version.
186195
*
187196
* Option $deleteColors and $insertColors only have affect when this class's cliColors option is set to true.
188197
*
@@ -194,7 +203,11 @@ private function mergeChanges(
194203
array $deleteColors = [null, null],
195204
array $insertColors = [null, null]
196205
): array {
197-
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][1]));
206+
$padding = str_repeat(
207+
' ',
208+
max($this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][1]), 0)
209+
);
210+
198211
if ($this->options['cliColor']) {
199212
$colorize = new CliColors();
200213
}
@@ -233,7 +246,7 @@ private function mergeChanges(
233246
/**
234247
* Generate a string representation of the end of a block.
235248
*
236-
* @param array $changes Contains the op-codes about the changes between two blocks of text.
249+
* @param array $changes Contains the op-codes about the changes between two blocks of text.
237250
*
238251
* @return string End of the block
239252
*/

0 commit comments

Comments
 (0)