Skip to content

Commit 773fe7e

Browse files
Cleanup
1 parent 2d1114d commit 773fe7e

15 files changed

+232
-156
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea
22
/composer.lock
33
/vendor
4+
/.php_cs.cache

.php_cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
$header = <<<'EOF'
3+
This file is part of sebastian/diff.
4+
5+
(c) Sebastian Bergmann <[email protected]>
6+
7+
For the full copyright and license information, please view the LICENSE
8+
file that was distributed with this source code.
9+
EOF;
10+
11+
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
13+
->setRules(
14+
[
15+
'array_syntax' => ['syntax' => 'long'],
16+
'binary_operator_spaces' => [
17+
'align_double_arrow' => true,
18+
'align_equals' => true
19+
],
20+
'blank_line_after_namespace' => true,
21+
'blank_line_before_return' => true,
22+
'braces' => true,
23+
'cast_spaces' => true,
24+
'concat_space' => ['spacing' => 'one'],
25+
'elseif' => true,
26+
'encoding' => true,
27+
'full_opening_tag' => true,
28+
'function_declaration' => true,
29+
'header_comment' => ['header' => $header, 'separate' => 'none'],
30+
'indentation_type' => true,
31+
'line_ending' => true,
32+
'lowercase_constants' => true,
33+
'lowercase_keywords' => true,
34+
'method_argument_space' => true,
35+
'native_function_invocation' => true,
36+
'no_alias_functions' => true,
37+
'no_blank_lines_after_class_opening' => true,
38+
'no_blank_lines_after_phpdoc' => true,
39+
'no_closing_tag' => true,
40+
'no_empty_phpdoc' => true,
41+
'no_empty_statement' => true,
42+
'no_extra_consecutive_blank_lines' => true,
43+
'no_leading_namespace_whitespace' => true,
44+
'no_singleline_whitespace_before_semicolons' => true,
45+
'no_spaces_after_function_name' => true,
46+
'no_spaces_inside_parenthesis' => true,
47+
'no_trailing_comma_in_list_call' => true,
48+
'no_trailing_whitespace' => true,
49+
'no_unused_imports' => true,
50+
'no_whitespace_in_blank_line' => true,
51+
'phpdoc_align' => true,
52+
'phpdoc_indent' => true,
53+
'phpdoc_no_access' => true,
54+
'phpdoc_no_empty_return' => true,
55+
'phpdoc_no_package' => true,
56+
'phpdoc_scalar' => true,
57+
'phpdoc_separation' => true,
58+
'phpdoc_to_comment' => true,
59+
'phpdoc_trim' => true,
60+
'phpdoc_types' => true,
61+
'phpdoc_var_without_name' => true,
62+
'self_accessor' => true,
63+
'simplified_null_return' => true,
64+
'single_blank_line_at_eof' => true,
65+
'single_import_per_statement' => true,
66+
'single_line_after_imports' => true,
67+
'single_quote' => true,
68+
'ternary_operator_spaces' => true,
69+
'trim_array_spaces' => true,
70+
'visibility_required' => true,
71+
]
72+
)
73+
->setFinder(
74+
PhpCsFixer\Finder::create()
75+
->files()
76+
->in(__DIR__ . '/src')
77+
->in(__DIR__ . '/tests')
78+
->name('*.php')
79+
);

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Diff
1+
sebastian/diff
22

33
Copyright (c) 2002-2017, Sebastian Bergmann <[email protected]>.
44
All rights reserved.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Diff
1+
# sebastian/diff
22

33
Diff implementation for PHP, factored out of PHPUnit into a stand-alone component.
44

src/Chunk.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* This file is part of the Diff package.
3+
* This file is part of sebastian/diff.
44
*
55
* (c) Sebastian Bergmann <[email protected]>
66
*
@@ -10,8 +10,6 @@
1010

1111
namespace SebastianBergmann\Diff;
1212

13-
/**
14-
*/
1513
class Chunk
1614
{
1715
/**

src/Diff.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* This file is part of the Diff package.
3+
* This file is part of sebastian/diff.
44
*
55
* (c) Sebastian Bergmann <[email protected]>
66
*
@@ -10,8 +10,6 @@
1010

1111
namespace SebastianBergmann\Diff;
1212

13-
/**
14-
*/
1513
class Diff
1614
{
1715
/**

src/Differ.php

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* This file is part of the Diff package.
3+
* This file is part of sebastian/diff.
44
*
55
* (c) Sebastian Bergmann <[email protected]>
66
*
@@ -41,32 +41,26 @@ public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLin
4141
/**
4242
* Returns the diff between two arrays or strings as string.
4343
*
44-
* @param array|string $from
45-
* @param array|string $to
44+
* @param array|string $from
45+
* @param array|string $to
4646
* @param LongestCommonSubsequence $lcs
4747
*
4848
* @return string
4949
*/
5050
public function diff($from, $to, LongestCommonSubsequence $lcs = null)
5151
{
52-
$from = $this->validateDiffInput($from);
53-
54-
$to = $this->validateDiffInput($to);
55-
56-
$diff = $this->diffToArray($from, $to, $lcs);
57-
58-
$old = $this->checkIfDiffInOld($diff);
59-
52+
$from = $this->validateDiffInput($from);
53+
$to = $this->validateDiffInput($to);
54+
$diff = $this->diffToArray($from, $to, $lcs);
55+
$old = $this->checkIfDiffInOld($diff);
6056
$start = isset($old[0]) ? $old[0] : 0;
61-
$end = count($diff);
57+
$end = \count($diff);
6258

63-
if ($tmp = array_search($end, $old)) {
59+
if ($tmp = \array_search($end, $old)) {
6460
$end = $tmp;
6561
}
6662

67-
$buffer = $this->getBuffer($diff, $old, $start, $end);
68-
69-
return $buffer;
63+
return $this->getBuffer($diff, $old, $start, $end);
7064
}
7165

7266
/**
@@ -78,11 +72,11 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
7872
*/
7973
private function validateDiffInput($input)
8074
{
81-
if ( ! is_array($input) && ! is_string($input)) {
82-
return (string)$input;
83-
} else {
84-
return $input;
75+
if (!\is_array($input) && !\is_string($input)) {
76+
return (string) $input;
8577
}
78+
79+
return $input;
8680
}
8781

8882
/**
@@ -93,7 +87,7 @@ private function validateDiffInput($input)
9387
*
9488
* @return array
9589
*/
96-
private function checkIfDiffInOld(Array $diff)
90+
private function checkIfDiffInOld(array $diff)
9791
{
9892
$inOld = false;
9993
$i = 0;
@@ -188,8 +182,8 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
188182
* - 1: ADDED: $token was added to $from
189183
* - 0: OLD: $token is not changed in $to
190184
*
191-
* @param array|string $from
192-
* @param array|string $to
185+
* @param array|string $from
186+
* @param array|string $to
193187
* @param LongestCommonSubsequence $lcs
194188
*
195189
* @return array
@@ -198,15 +192,13 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
198192
{
199193
$fromMatches = $this->getNewLineMatches($from);
200194
$toMatches = $this->getNewLineMatches($to);
201-
202-
$from = $this->splitStringByLines($from);
203-
$to = $this->splitStringByLines($to);
204-
205-
$start = array();
206-
$end = array();
207-
$fromLength = count($from);
208-
$toLength = count($to);
209-
$length = min($fromLength, $toLength);
195+
$from = $this->splitStringByLines($from);
196+
$to = $this->splitStringByLines($to);
197+
$start = array();
198+
$end = array();
199+
$fromLength = \count($from);
200+
$toLength = \count($to);
201+
$length = \min($fromLength, $toLength);
210202

211203
$this->adjustDiffStartPoint($length, $from, $to);
212204
$this->adjustDiffEndPoint($length, $from, $to, $end, $fromLength, $toLength);
@@ -215,7 +207,7 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
215207
$lcs = $this->selectLcsImplementation($from, $to);
216208
}
217209

218-
$common = $lcs->calculate(array_values($from), array_values($to));
210+
$common = $lcs->calculate(\array_values($from), \array_values($to));
219211
$diff = array();
220212

221213
if ($this->detectUnmatchedLineEndings($fromMatches, $toMatches)) {
@@ -229,29 +221,29 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
229221
$diff[] = array($token, 0 /* OLD */);
230222
}
231223

232-
reset($from);
233-
reset($to);
224+
\reset($from);
225+
\reset($to);
234226

235227
foreach ($common as $token) {
236-
while ((($fromToken = reset($from)) !== $token)) {
237-
$diff[] = array(array_shift($from), 2 /* REMOVED */);
228+
while (($fromToken = \reset($from)) !== $token) {
229+
$diff[] = array(\array_shift($from), 2 /* REMOVED */);
238230
}
239231

240-
while ((($toToken = reset($to)) !== $token)) {
241-
$diff[] = array(array_shift($to), 1 /* ADDED */);
232+
while (($toToken = \reset($to)) !== $token) {
233+
$diff[] = array(\array_shift($to), 1 /* ADDED */);
242234
}
243235

244236
$diff[] = array($token, 0 /* OLD */);
245237

246-
array_shift($from);
247-
array_shift($to);
238+
\array_shift($from);
239+
\array_shift($to);
248240
}
249241

250-
while (($token = array_shift($from)) !== null) {
242+
while (($token = \array_shift($from)) !== null) {
251243
$diff[] = array($token, 2 /* REMOVED */);
252244
}
253245

254-
while (($token = array_shift($to)) !== null) {
246+
while (($token = \array_shift($to)) !== null) {
255247
$diff[] = array($token, 1 /* ADDED */);
256248
}
257249

@@ -271,10 +263,9 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
271263
*/
272264
private function getNewLineMatches($string)
273265
{
274-
preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
266+
\preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
275267

276268
return $stringMatches;
277-
278269
}
279270

280271
/**
@@ -286,8 +277,8 @@ private function getNewLineMatches($string)
286277
*/
287278
private function splitStringByLines($input)
288279
{
289-
if (is_string($input)) {
290-
return preg_split('(\r\n|\r|\n)', $input);
280+
if (\is_string($input)) {
281+
return \preg_split('(\r\n|\r|\n)', $input);
291282
}
292283

293284
return $input;
@@ -324,23 +315,24 @@ private function selectLcsImplementation(array $from, array $to)
324315
*/
325316
private function calculateEstimatedFootprint(array $from, array $to)
326317
{
327-
$itemSize = PHP_INT_SIZE == 4 ? 76 : 144;
318+
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
328319

329-
return $itemSize * pow(min(count($from), count($to)), 2);
320+
return $itemSize * \pow(\min(\count($from), \count($to)), 2);
330321
}
331322

332323
/**
333324
* Adjust start point and removes common from/to lines.
334325
*
335-
* @param $length
336-
* @param $from
337-
* @param $to
326+
* @param int $length
327+
* @param array $from
328+
* @param array $to
338329
*/
339-
private function adjustDiffStartPoint(&$length, &$from, &$to)
330+
private function adjustDiffStartPoint(&$length, array &$from, array &$to)
340331
{
341332
for ($i = 0; $i < $length; ++$i) {
342333
if ($from[$i] === $to[$i]) {
343334
$start[] = $from[$i];
335+
344336
unset($from[$i], $to[$i]);
345337
} else {
346338
break;
@@ -353,18 +345,18 @@ private function adjustDiffStartPoint(&$length, &$from, &$to)
353345
/**
354346
* Adjusts end point and removes common from/to lines.
355347
*
356-
* @param $length
357-
* @param $from
358-
* @param $to
359-
* @param $end
360-
* @param $fromLength
361-
* @param $toLength
348+
* @param int $length
349+
* @param array $from
350+
* @param array $to
351+
* @param array $end
352+
* @param int $fromLength
353+
* @param int $toLength
362354
*/
363-
private function adjustDiffEndPoint(&$length, &$from, &$to, $end, $fromLength, $toLength)
355+
private function adjustDiffEndPoint(&$length, array &$from, array &$to, array $end, $fromLength, $toLength)
364356
{
365357
for ($i = 1; $i < $length; ++$i) {
366358
if ($from[$fromLength - $i] === $to[$toLength - $i]) {
367-
array_unshift($end, $from[$fromLength - $i]);
359+
\array_unshift($end, $from[$fromLength - $i]);
368360
unset($from[$fromLength - $i], $to[$toLength - $i]);
369361
} else {
370362
break;
@@ -375,15 +367,16 @@ private function adjustDiffEndPoint(&$length, &$from, &$to, $end, $fromLength, $
375367
/**
376368
* Returns true if line ends don't match on fromMatches and toMatches.
377369
*
378-
* @param $fromMatches
379-
* @param $toMatches
370+
* @param array $fromMatches
371+
* @param array $toMatches
380372
*
381373
* @return bool
382374
*/
383-
private function detectUnmatchedLineEndings($fromMatches, $toMatches)
375+
private function detectUnmatchedLineEndings(array $fromMatches, array $toMatches)
384376
{
385-
return isset($fromMatches[0]) && $toMatches[0] &&
386-
count($fromMatches[0]) === count($toMatches[0]) &&
377+
return isset($fromMatches[0]) &&
378+
$toMatches[0] &&
379+
\count($fromMatches[0]) === \count($toMatches[0]) &&
387380
$fromMatches[0] !== $toMatches[0];
388381
}
389382
}

0 commit comments

Comments
 (0)