9
9
*/
10
10
namespace SebastianBergmann \Diff ;
11
11
12
+ use const PHP_INT_SIZE ;
13
+ use const PREG_SPLIT_DELIM_CAPTURE ;
14
+ use const PREG_SPLIT_NO_EMPTY ;
15
+ use function array_shift ;
16
+ use function array_unshift ;
17
+ use function array_values ;
18
+ use function count ;
19
+ use function current ;
20
+ use function end ;
21
+ use function get_class ;
22
+ use function gettype ;
23
+ use function is_array ;
24
+ use function is_object ;
25
+ use function is_string ;
26
+ use function key ;
27
+ use function min ;
28
+ use function preg_split ;
29
+ use function prev ;
30
+ use function reset ;
31
+ use function sprintf ;
32
+ use function substr ;
12
33
use SebastianBergmann \Diff \Output \DiffOutputBuilderInterface ;
13
34
use SebastianBergmann \Diff \Output \UnifiedDiffOutputBuilder ;
14
35
@@ -40,16 +61,16 @@ public function __construct($outputBuilder = null)
40
61
$ this ->outputBuilder = $ outputBuilder ;
41
62
} elseif (null === $ outputBuilder ) {
42
63
$ this ->outputBuilder = new UnifiedDiffOutputBuilder ;
43
- } elseif (\ is_string ($ outputBuilder )) {
64
+ } elseif (is_string ($ outputBuilder )) {
44
65
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
45
66
// @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
46
67
// @deprecated
47
68
$ this ->outputBuilder = new UnifiedDiffOutputBuilder ($ outputBuilder );
48
69
} else {
49
70
throw new InvalidArgumentException (
50
- \ sprintf (
71
+ sprintf (
51
72
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s. ' ,
52
- \ is_object ($ outputBuilder ) ? 'instance of " ' . \ get_class ($ outputBuilder ) . '" ' : \ gettype ($ outputBuilder ) . ' " ' . $ outputBuilder . '" '
73
+ is_object ($ outputBuilder ) ? 'instance of " ' . get_class ($ outputBuilder ) . '" ' : gettype ($ outputBuilder ) . ' " ' . $ outputBuilder . '" '
53
74
)
54
75
);
55
76
}
@@ -89,15 +110,15 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
89
110
*/
90
111
public function diffToArray ($ from , $ to , LongestCommonSubsequenceCalculator $ lcs = null ): array
91
112
{
92
- if (\ is_string ($ from )) {
113
+ if (is_string ($ from )) {
93
114
$ from = $ this ->splitStringByLines ($ from );
94
- } elseif (!\ is_array ($ from )) {
115
+ } elseif (!is_array ($ from )) {
95
116
throw new InvalidArgumentException ('"from" must be an array or string. ' );
96
117
}
97
118
98
- if (\ is_string ($ to )) {
119
+ if (is_string ($ to )) {
99
120
$ to = $ this ->splitStringByLines ($ to );
100
- } elseif (!\ is_array ($ to )) {
121
+ } elseif (!is_array ($ to )) {
101
122
throw new InvalidArgumentException ('"to" must be an array or string. ' );
102
123
}
103
124
@@ -107,36 +128,36 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
107
128
$ lcs = $ this ->selectLcsImplementation ($ from , $ to );
108
129
}
109
130
110
- $ common = $ lcs ->calculate (\ array_values ($ from ), \ array_values ($ to ));
131
+ $ common = $ lcs ->calculate (array_values ($ from ), array_values ($ to ));
111
132
$ diff = [];
112
133
113
134
foreach ($ start as $ token ) {
114
135
$ diff [] = [$ token , self ::OLD ];
115
136
}
116
137
117
- \ reset ($ from );
118
- \ reset ($ to );
138
+ reset ($ from );
139
+ reset ($ to );
119
140
120
141
foreach ($ common as $ token ) {
121
- while (($ fromToken = \ reset ($ from )) !== $ token ) {
122
- $ diff [] = [\ array_shift ($ from ), self ::REMOVED ];
142
+ while (($ fromToken = reset ($ from )) !== $ token ) {
143
+ $ diff [] = [array_shift ($ from ), self ::REMOVED ];
123
144
}
124
145
125
- while (($ toToken = \ reset ($ to )) !== $ token ) {
126
- $ diff [] = [\ array_shift ($ to ), self ::ADDED ];
146
+ while (($ toToken = reset ($ to )) !== $ token ) {
147
+ $ diff [] = [array_shift ($ to ), self ::ADDED ];
127
148
}
128
149
129
150
$ diff [] = [$ token , self ::OLD ];
130
151
131
- \ array_shift ($ from );
132
- \ array_shift ($ to );
152
+ array_shift ($ from );
153
+ array_shift ($ to );
133
154
}
134
155
135
- while (($ token = \ array_shift ($ from )) !== null ) {
156
+ while (($ token = array_shift ($ from )) !== null ) {
136
157
$ diff [] = [$ token , self ::REMOVED ];
137
158
}
138
159
139
- while (($ token = \ array_shift ($ to )) !== null ) {
160
+ while (($ token = array_shift ($ to )) !== null ) {
140
161
$ diff [] = [$ token , self ::ADDED ];
141
162
}
142
163
@@ -145,7 +166,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
145
166
}
146
167
147
168
if ($ this ->detectUnmatchedLineEndings ($ diff )) {
148
- \ array_unshift ($ diff , ["#Warning: Strings contain different line endings! \n" , self ::DIFF_LINE_END_WARNING ]);
169
+ array_unshift ($ diff , ["#Warning: Strings contain different line endings! \n" , self ::DIFF_LINE_END_WARNING ]);
149
170
}
150
171
151
172
return $ diff ;
@@ -158,7 +179,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
158
179
*/
159
180
private function normalizeDiffInput ($ input )
160
181
{
161
- if (!\ is_array ($ input ) && !\ is_string ($ input )) {
182
+ if (!is_array ($ input ) && !is_string ($ input )) {
162
183
return (string ) $ input ;
163
184
}
164
185
@@ -170,7 +191,7 @@ private function normalizeDiffInput($input)
170
191
*/
171
192
private function splitStringByLines (string $ input ): array
172
193
{
173
- return \ preg_split ('/(.*\R)/ ' , $ input , -1 , \ PREG_SPLIT_DELIM_CAPTURE | \ PREG_SPLIT_NO_EMPTY );
194
+ return preg_split ('/(.*\R)/ ' , $ input , -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
174
195
}
175
196
176
197
private function selectLcsImplementation (array $ from , array $ to ): LongestCommonSubsequenceCalculator
@@ -195,9 +216,9 @@ private function selectLcsImplementation(array $from, array $to): LongestCommonS
195
216
*/
196
217
private function calculateEstimatedFootprint (array $ from , array $ to )
197
218
{
198
- $ itemSize = \ PHP_INT_SIZE === 4 ? 76 : 144 ;
219
+ $ itemSize = PHP_INT_SIZE === 4 ? 76 : 144 ;
199
220
200
- return $ itemSize * \ min (\ count ($ from ), \ count ($ to )) ** 2 ;
221
+ return $ itemSize * min (count ($ from ), count ($ to )) ** 2 ;
201
222
}
202
223
203
224
/**
@@ -243,11 +264,11 @@ private function detectUnmatchedLineEndings(array $diff): bool
243
264
244
265
private function getLinebreak ($ line ): string
245
266
{
246
- if (!\ is_string ($ line )) {
267
+ if (!is_string ($ line )) {
247
268
return '' ;
248
269
}
249
270
250
- $ lc = \ substr ($ line , -1 );
271
+ $ lc = substr ($ line , -1 );
251
272
252
273
if ("\r" === $ lc ) {
253
274
return "\r" ;
@@ -257,7 +278,7 @@ private function getLinebreak($line): string
257
278
return '' ;
258
279
}
259
280
260
- if ("\r\n" === \ substr ($ line , -2 )) {
281
+ if ("\r\n" === substr ($ line , -2 )) {
261
282
return "\r\n" ;
262
283
}
263
284
@@ -269,10 +290,10 @@ private static function getArrayDiffParted(array &$from, array &$to): array
269
290
$ start = [];
270
291
$ end = [];
271
292
272
- \ reset ($ to );
293
+ reset ($ to );
273
294
274
295
foreach ($ from as $ k => $ v ) {
275
- $ toK = \ key ($ to );
296
+ $ toK = key ($ to );
276
297
277
298
if ($ toK === $ k && $ v === $ to [$ k ]) {
278
299
$ start [$ k ] = $ v ;
@@ -283,19 +304,19 @@ private static function getArrayDiffParted(array &$from, array &$to): array
283
304
}
284
305
}
285
306
286
- \ end ($ from );
287
- \ end ($ to );
307
+ end ($ from );
308
+ end ($ to );
288
309
289
310
do {
290
- $ fromK = \ key ($ from );
291
- $ toK = \ key ($ to );
311
+ $ fromK = key ($ from );
312
+ $ toK = key ($ to );
292
313
293
- if (null === $ fromK || null === $ toK || \ current ($ from ) !== \ current ($ to )) {
314
+ if (null === $ fromK || null === $ toK || current ($ from ) !== current ($ to )) {
294
315
break ;
295
316
}
296
317
297
- \ prev ($ from );
298
- \ prev ($ to );
318
+ prev ($ from );
319
+ prev ($ to );
299
320
300
321
$ end = [$ fromK => $ from [$ fromK ]] + $ end ;
301
322
unset($ from [$ fromK ], $ to [$ toK ]);
0 commit comments