Skip to content

Commit 7016891

Browse files
SpacePossumsebastianbergmann
authored andcommitted
Add type hinting.
1 parent 68703a3 commit 7016891

9 files changed

+47
-118
lines changed

src/Chunk.php

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,65 +37,40 @@ final class Chunk
3737
*/
3838
private $lines;
3939

40-
/**
41-
* @param int $start
42-
* @param int $startRange
43-
* @param int $end
44-
* @param int $endRange
45-
* @param array $lines
46-
*/
47-
public function __construct($start = 0, $startRange = 1, $end = 0, $endRange = 1, array $lines = [])
40+
public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = [])
4841
{
49-
$this->start = (int) $start;
50-
$this->startRange = (int) $startRange;
51-
$this->end = (int) $end;
52-
$this->endRange = (int) $endRange;
42+
$this->start = $start;
43+
$this->startRange = $startRange;
44+
$this->end = $end;
45+
$this->endRange = $endRange;
5346
$this->lines = $lines;
5447
}
5548

56-
/**
57-
* @return int
58-
*/
59-
public function getStart()
49+
public function getStart(): int
6050
{
6151
return $this->start;
6252
}
6353

64-
/**
65-
* @return int
66-
*/
67-
public function getStartRange()
54+
public function getStartRange(): int
6855
{
6956
return $this->startRange;
7057
}
7158

72-
/**
73-
* @return int
74-
*/
75-
public function getEnd()
59+
public function getEnd(): int
7660
{
7761
return $this->end;
7862
}
7963

80-
/**
81-
* @return int
82-
*/
83-
public function getEndRange()
64+
public function getEndRange(): int
8465
{
8566
return $this->endRange;
8667
}
8768

88-
/**
89-
* @return array
90-
*/
91-
public function getLines()
69+
public function getLines(): array
9270
{
9371
return $this->lines;
9472
}
9573

96-
/**
97-
* @param array $lines
98-
*/
9974
public function setLines(array $lines)
10075
{
10176
$this->lines = $lines;

src/Diff.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,27 @@ final class Diff
3232
* @param string $to
3333
* @param Chunk[] $chunks
3434
*/
35-
public function __construct($from, $to, array $chunks = [])
35+
public function __construct(string $from, string $to, array $chunks = [])
3636
{
3737
$this->from = $from;
3838
$this->to = $to;
3939
$this->chunks = $chunks;
4040
}
4141

42-
/**
43-
* @return string
44-
*/
45-
public function getFrom()
42+
public function getFrom(): string
4643
{
4744
return $this->from;
4845
}
4946

50-
/**
51-
* @return string
52-
*/
53-
public function getTo()
47+
public function getTo(): string
5448
{
5549
return $this->to;
5650
}
5751

5852
/**
5953
* @return Chunk[]
6054
*/
61-
public function getChunks()
55+
public function getChunks(): array
6256
{
6357
return $this->chunks;
6458
}

src/Differ.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ final class Differ
2525
*/
2626
private $showNonDiffLines;
2727

28-
/**
29-
* @param string $header
30-
* @param bool $showNonDiffLines
31-
*/
32-
public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLines = true)
28+
public function __construct(string $header = "--- Original\n+++ New\n", bool $showNonDiffLines = true)
3329
{
3430
$this->header = $header;
3531
$this->showNonDiffLines = $showNonDiffLines;
@@ -38,13 +34,13 @@ public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLin
3834
/**
3935
* Returns the diff between two arrays or strings as string.
4036
*
41-
* @param array|string $from
42-
* @param array|string $to
43-
* @param LongestCommonSubsequenceCalculator $lcs
37+
* @param array|string $from
38+
* @param array|string $to
39+
* @param LongestCommonSubsequenceCalculator|null $lcs
4440
*
4541
* @return string
4642
*/
47-
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
43+
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
4844
{
4945
$from = $this->validateDiffInput($from);
5046
$to = $this->validateDiffInput($to);
@@ -63,7 +59,7 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
6359
*
6460
* @return string
6561
*/
66-
private function validateDiffInput($input)
62+
private function validateDiffInput($input): string
6763
{
6864
if (!\is_array($input) && !\is_string($input)) {
6965
return (string) $input;
@@ -80,7 +76,7 @@ private function validateDiffInput($input)
8076
*
8177
* @return array
8278
*/
83-
private function checkIfDiffInOld(array $diff)
79+
private function checkIfDiffInOld(array $diff): array
8480
{
8581
$inOld = false;
8682
$i = 0;
@@ -115,7 +111,7 @@ private function checkIfDiffInOld(array $diff)
115111
*
116112
* @return string
117113
*/
118-
private function getBuffer(array $diff, array $old, $start, $end)
114+
private function getBuffer(array $diff, array $old, int $start, int $end): string
119115
{
120116
$buffer = $this->header;
121117

@@ -145,7 +141,7 @@ private function getBuffer(array $diff, array $old, $start, $end)
145141
*
146142
* @return string
147143
*/
148-
private function getDiffBufferElement(array $diff, $buffer, $diffIndex)
144+
private function getDiffBufferElement(array $diff, string $buffer, int $diffIndex): string
149145
{
150146
if ($diff[$diffIndex][1] === 1 /* ADDED */) {
151147
$buffer .= '+' . $diff[$diffIndex][0] . "\n";
@@ -167,7 +163,7 @@ private function getDiffBufferElement(array $diff, $buffer, $diffIndex)
167163
*
168164
* @return string
169165
*/
170-
private function getDiffBufferElementNew(array $diff, $buffer, $diffIndex)
166+
private function getDiffBufferElementNew(array $diff, string $buffer, int $diffIndex): string
171167
{
172168
if ($this->showNonDiffLines === true) {
173169
$buffer .= "@@ @@\n";
@@ -193,7 +189,7 @@ private function getDiffBufferElementNew(array $diff, $buffer, $diffIndex)
193189
*
194190
* @return array
195191
*/
196-
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
192+
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
197193
{
198194
if (\is_string($from)) {
199195
$fromMatches = $this->getNewLineMatches($from);
@@ -273,7 +269,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
273269
*
274270
* @return array
275271
*/
276-
private function getNewLineMatches($string)
272+
private function getNewLineMatches(string $string): array
277273
{
278274
\preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
279275

@@ -287,7 +283,7 @@ private function getNewLineMatches($string)
287283
*
288284
* @return array
289285
*/
290-
private function splitStringByLines($input)
286+
private function splitStringByLines(string $input): array
291287
{
292288
return \preg_split('(\r\n|\r|\n)', $input);
293289
}
@@ -298,7 +294,7 @@ private function splitStringByLines($input)
298294
*
299295
* @return LongestCommonSubsequenceCalculator
300296
*/
301-
private function selectLcsImplementation(array $from, array $to)
297+
private function selectLcsImplementation(array $from, array $to): LongestCommonSubsequenceCalculator
302298
{
303299
// We do not want to use the time-efficient implementation if its memory
304300
// footprint will probably exceed this value. Note that the footprint
@@ -336,20 +332,14 @@ private function calculateEstimatedFootprint(array $from, array $to)
336332
*
337333
* @return bool
338334
*/
339-
private function detectUnmatchedLineEndings(array $fromMatches, array $toMatches)
335+
private function detectUnmatchedLineEndings(array $fromMatches, array $toMatches): bool
340336
{
341337
return isset($fromMatches[0], $toMatches[0]) &&
342338
\count($fromMatches[0]) === \count($toMatches[0]) &&
343339
$fromMatches[0] !== $toMatches[0];
344340
}
345341

346-
/**
347-
* @param array $from
348-
* @param array $to
349-
*
350-
* @return array
351-
*/
352-
private static function getArrayDiffParted(array &$from, array &$to)
342+
private static function getArrayDiffParted(array &$from, array &$to): array
353343
{
354344
$start = [];
355345
$end = [];

src/Line.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,18 @@ final class Line
2626
*/
2727
private $content;
2828

29-
/**
30-
* @param int $type
31-
* @param string $content
32-
*/
33-
public function __construct($type = self::UNCHANGED, $content = '')
29+
public function __construct(int $type = self::UNCHANGED, string $content = '')
3430
{
3531
$this->type = $type;
3632
$this->content = $content;
3733
}
3834

39-
/**
40-
* @return string
41-
*/
42-
public function getContent()
35+
public function getContent(): string
4336
{
4437
return $this->content;
4538
}
4639

47-
/**
48-
* @return int
49-
*/
50-
public function getType()
40+
public function getType(): int
5141
{
5242
return $this->type;
5343
}

src/LongestCommonSubsequenceCalculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ interface LongestCommonSubsequenceCalculator
2020
*
2121
* @return array
2222
*/
23-
public function calculate(array $from, array $to);
23+
public function calculate(array $from, array $to): array;
2424
}

src/MemoryEfficientLongestCommonSubsequenceCalculator.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
1414
{
1515
/**
16-
* Calculates the longest common subsequence of two arrays.
17-
*
18-
* @param array $from
19-
* @param array $to
20-
*
21-
* @return array
16+
* {@inheritdoc}
2217
*/
23-
public function calculate(array $from, array $to)
18+
public function calculate(array $from, array $to): array
2419
{
2520
$cFrom = \count($from);
2621
$cTo = \count($to);
@@ -63,13 +58,7 @@ public function calculate(array $from, array $to)
6358
);
6459
}
6560

66-
/**
67-
* @param array $from
68-
* @param array $to
69-
*
70-
* @return array
71-
*/
72-
private function length(array $from, array $to)
61+
private function length(array $from, array $to): array
7362
{
7463
$current = \array_fill(0, \count($to) + 1, 0);
7564
$cFrom = \count($from);

src/Parser.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class Parser
2020
*
2121
* @return Diff[]
2222
*/
23-
public function parse($string)
23+
public function parse(string $string): array
2424
{
2525
$lines = \preg_split('(\r\n|\r|\n)', $string);
2626

@@ -64,10 +64,6 @@ public function parse($string)
6464
return $diffs;
6565
}
6666

67-
/**
68-
* @param Diff $diff
69-
* @param array $lines
70-
*/
7167
private function parseFileDiff(Diff $diff, array $lines)
7268
{
7369
$chunks = [];

src/TimeEfficientLongestCommonSubsequenceCalculator.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
1414
{
1515
/**
16-
* Calculates the longest common subsequence of two arrays.
17-
*
18-
* @param array $from
19-
* @param array $to
20-
*
21-
* @return array
16+
* {@inheritdoc}
2217
*/
23-
public function calculate(array $from, array $to)
18+
public function calculate(array $from, array $to): array
2419
{
2520
$common = [];
2621
$fromLength = \count($from);

0 commit comments

Comments
 (0)