Skip to content

Commit 6f8e9dd

Browse files
committed
Uniform the term "$a, $b, from, to" into "old, new"
Related methods are deprecated and would be removed in the next major release. Signed-off-by: Jack Cherng <[email protected]>
1 parent f7679ff commit 6f8e9dd

File tree

13 files changed

+226
-149
lines changed

13 files changed

+226
-149
lines changed

src/Diff.php

Lines changed: 109 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ final class Diff
2525
/**
2626
* @var string[] the "old" sequence to use as the basis for the comparison
2727
*/
28-
private $a = [];
28+
private $old = [];
2929

3030
/**
3131
* @var string[] the "new" sequence to generate the changes for
3232
*/
33-
private $b = [];
33+
private $new = [];
3434

3535
/**
3636
* @var null|SequenceMatcher the sequence matcher
@@ -57,63 +57,63 @@ final class Diff
5757
/**
5858
* The constructor.
5959
*
60-
* @param string[] $a array containing the lines of the first string to compare
61-
* @param string[] $b array containing the lines for the second string to compare
60+
* @param string[] $old array containing the lines of the old string to compare
61+
* @param string[] $new array containing the lines for the new string to compare
6262
* @param array $options
6363
*/
64-
public function __construct(array $a, array $b, array $options = [])
64+
public function __construct(array $old, array $new, array $options = [])
6565
{
6666
$this->sequenceMatcher = new SequenceMatcher([], []);
6767

68-
$this->setAB($a, $b)->setOptions($options);
68+
$this->setOldNew($old, $new)->setOptions($options);
6969
}
7070

7171
/**
72-
* Set a and b.
72+
* Set old and new.
7373
*
74-
* @param string[] $a the a
75-
* @param string[] $b the b
74+
* @param string[] $old the old
75+
* @param string[] $new the new
7676
*
7777
* @return self
7878
*/
79-
public function setAB(array $a, array $b): self
79+
public function setOldNew(array $old, array $new): self
8080
{
81-
$this->setA($a)->setB($b);
81+
$this->setOld($old)->setNew($new);
8282

8383
return $this;
8484
}
8585

8686
/**
87-
* Set a.
87+
* Set old.
8888
*
89-
* @param string[] $a the a
89+
* @param string[] $old the old
9090
*
9191
* @return self
9292
*/
93-
public function setA(array $a): self
93+
public function setOld(array $old): self
9494
{
95-
if ($this->a !== $a) {
96-
$this->a = $a;
95+
if ($this->old !== $old) {
96+
$this->old = $old;
9797
$this->groupedCodes = null;
98-
$this->sequenceMatcher->setSeq1($a);
98+
$this->sequenceMatcher->setSeq1($old);
9999
}
100100

101101
return $this;
102102
}
103103

104104
/**
105-
* Set b.
105+
* Set new.
106106
*
107-
* @param string[] $b the b
107+
* @param string[] $new the new
108108
*
109109
* @return self
110110
*/
111-
public function setB(array $b): self
111+
public function setNew(array $new): self
112112
{
113-
if ($this->b !== $b) {
114-
$this->b = $b;
113+
if ($this->new !== $new) {
114+
$this->new = $new;
115115
$this->groupedCodes = null;
116-
$this->sequenceMatcher->setSeq2($b);
116+
$this->sequenceMatcher->setSeq2($new);
117117
}
118118

119119
return $this;
@@ -136,8 +136,7 @@ public function setOptions(array $options): self
136136
}
137137

138138
/**
139-
* Get a range of lines from $start to $end from the first comparison string
140-
* and return them as an array.
139+
* Get a range of lines from $start to $end from the old string and return them as an array.
141140
*
142141
* If $end is null, it returns array sliced from the $start to the end.
143142
*
@@ -146,14 +145,13 @@ public function setOptions(array $options): self
146145
*
147146
* @return string[] array of all of the lines between the specified range
148147
*/
149-
public function getA(int $start = 0, ?int $end = null): array
148+
public function getOld(int $start = 0, ?int $end = null): array
150149
{
151-
return $this->getText($this->a, $start, $end);
150+
return $this->getText($this->old, $start, $end);
152151
}
153152

154153
/**
155-
* Get a range of lines from $start to $end from the second comparison string
156-
* and return them as an array.
154+
* Get a range of lines from $start to $end from the new string and return them as an array.
157155
*
158156
* If $end is null, it returns array sliced from the $start to the end.
159157
*
@@ -162,9 +160,9 @@ public function getA(int $start = 0, ?int $end = null): array
162160
*
163161
* @return string[] array of all of the lines between the specified range
164162
*/
165-
public function getB(int $start = 0, ?int $end = null): array
163+
public function getNew(int $start = 0, ?int $end = null): array
166164
{
167-
return $this->getText($this->b, $start, $end);
165+
return $this->getText($this->new, $start, $end);
168166
}
169167

170168
/**
@@ -216,11 +214,90 @@ public function render(AbstractRenderer $renderer): string
216214

217215
// the "no difference" situation may happen frequently
218216
// let's save some calculation if possible
219-
return $this->a === $this->b
217+
return $this->old === $this->new
220218
? $renderer::getIdenticalResult()
221219
: $renderer->render();
222220
}
223221

222+
/**
223+
* Set a and b.
224+
*
225+
* @deprecated 5.0.0
226+
*
227+
* @param string[] $a the a
228+
* @param string[] $b the b
229+
*
230+
* @return self
231+
*/
232+
public function setAB(array $a, array $b): self
233+
{
234+
return $this->setOldNew($a, $b);
235+
}
236+
237+
/**
238+
* Set a.
239+
*
240+
* @deprecated 5.0.0
241+
*
242+
* @param string[] $a the a
243+
*
244+
* @return self
245+
*/
246+
public function setA(array $a): self
247+
{
248+
return $this->setOld($a);
249+
}
250+
251+
/**
252+
* Set b.
253+
*
254+
* @deprecated 5.0.0
255+
*
256+
* @param string[] $b the b
257+
*
258+
* @return self
259+
*/
260+
public function setB(array $b): self
261+
{
262+
return $this->setNew($b);
263+
}
264+
265+
/**
266+
* Get a range of lines from $start to $end from the first comparison string
267+
* and return them as an array.
268+
*
269+
* If $end is null, it returns array sliced from the $start to the end.
270+
*
271+
* @deprecated 5.0.0
272+
*
273+
* @param int $start the starting number. If null, the whole array will be returned.
274+
* @param null|int $end the ending number. If null, only the item in $start will be returned.
275+
*
276+
* @return string[] array of all of the lines between the specified range
277+
*/
278+
public function getA(int $start = 0, ?int $end = null): array
279+
{
280+
return $this->getOld($start, $end);
281+
}
282+
283+
/**
284+
* Get a range of lines from $start to $end from the second comparison string
285+
* and return them as an array.
286+
*
287+
* If $end is null, it returns array sliced from the $start to the end.
288+
*
289+
* @deprecated 5.0.0
290+
*
291+
* @param int $start the starting number
292+
* @param null|int $end the ending number
293+
*
294+
* @return string[] array of all of the lines between the specified range
295+
*/
296+
public function getB(int $start = 0, ?int $end = null): array
297+
{
298+
return $this->getNew($start, $end);
299+
}
300+
224301
/**
225302
* The work horse of getA() and getB().
226303
*

src/DiffHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function calculate(
9696
\is_string($new) && ($new = \explode("\n", $new));
9797

9898
return Diff::getInstance()
99-
->setAB($old, $new)
99+
->setOldNew($old, $new)
100100
->setOptions($diffOptions)
101101
->render(
102102
RendererFactory::getInstance($template)

src/Renderer/Html/AbstractHtml.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function getChanges(): array
4646
$this->options
4747
);
4848

49-
// As we'll be modifying a & b to include our change markers,
49+
// As we'll be modifying old & new to include our change markers,
5050
// we need to get the contents and store them here. That way
5151
// we're not going to destroy the original data
52-
$a = $this->diff->getA();
53-
$b = $this->diff->getB();
52+
$old = $this->diff->getOld();
53+
$new = $this->diff->getNew();
5454

5555
$changes = [];
5656

@@ -65,7 +65,7 @@ public function getChanges(): array
6565
$i2 - $i1 === $j2 - $j1
6666
) {
6767
for ($i = 0; $i < $i2 - $i1; ++$i) {
68-
$this->renderChangedExtent($lineRenderer, $a[$i1 + $i], $b[$j1 + $i]);
68+
$this->renderChangedExtent($lineRenderer, $old[$i1 + $i], $new[$j1 + $i]);
6969
}
7070
}
7171

@@ -77,7 +77,7 @@ public function getChanges(): array
7777
$lastTag = $tag;
7878

7979
if ($tag === SequenceMatcher::OP_EQ) {
80-
if (!empty($lines = \array_slice($a, $i1, ($i2 - $i1)))) {
80+
if (!empty($lines = \array_slice($old, $i1, ($i2 - $i1)))) {
8181
$formattedLines = $this->formatLines($lines);
8282

8383
$blocks[$lastBlock]['base']['lines'] += $formattedLines;
@@ -101,7 +101,7 @@ public function getChanges(): array
101101
$tag === SequenceMatcher::OP_REP ||
102102
$tag === SequenceMatcher::OP_DEL
103103
) {
104-
$lines = \array_slice($a, $i1, ($i2 - $i1));
104+
$lines = \array_slice($old, $i1, ($i2 - $i1));
105105
$lines = $this->formatLines($lines);
106106
$lines = \str_replace(
107107
RendererConstant::HTML_CLOSURES,
@@ -116,7 +116,7 @@ public function getChanges(): array
116116
$tag === SequenceMatcher::OP_REP ||
117117
$tag === SequenceMatcher::OP_INS
118118
) {
119-
$lines = \array_slice($b, $j1, ($j2 - $j1));
119+
$lines = \array_slice($new, $j1, ($j2 - $j1));
120120
$lines = $this->formatLines($lines);
121121
$lines = \str_replace(
122122
RendererConstant::HTML_CLOSURES,
@@ -138,27 +138,27 @@ public function getChanges(): array
138138
* Renderer the changed extent.
139139
*
140140
* @param AbstractLineRenderer $lineRenderer the line renderer
141-
* @param string $from the from line
142-
* @param string $to the to line
141+
* @param string $old the old line
142+
* @param string $new the new line
143143
*
144144
* @throws \InvalidArgumentException
145145
*
146146
* @return self
147147
*/
148-
protected function renderChangedExtent(AbstractLineRenderer $lineRenderer, string &$from, string &$to): self
148+
protected function renderChangedExtent(AbstractLineRenderer $lineRenderer, string &$old, string &$new): self
149149
{
150-
static $mbFrom, $mbTo;
150+
static $mbOld, $mbNew;
151151

152-
$mbFrom = $mbFrom ?? new MbString();
153-
$mbTo = $mbTo ?? new MbString();
152+
$mbOld = $mbOld ?? new MbString();
153+
$mbNew = $mbNew ?? new MbString();
154154

155-
$mbFrom->set($from);
156-
$mbTo->set($to);
155+
$mbOld->set($old);
156+
$mbNew->set($new);
157157

158-
$lineRenderer->render($mbFrom, $mbTo);
158+
$lineRenderer->render($mbOld, $mbNew);
159159

160-
$from = $mbFrom->get();
161-
$to = $mbTo->get();
160+
$old = $mbOld->get();
161+
$new = $mbNew->get();
162162

163163
return $this;
164164
}

0 commit comments

Comments
 (0)