Skip to content

Commit 150ab98

Browse files
committed
added type checking to files diff command
1 parent 234d3e4 commit 150ab98

12 files changed

+159
-84
lines changed

src/CommandFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class CommandFactory implements CommandFactoryInterface
1717
{
18-
private \Docopt $parser;
18+
private \Docopt|DocoptDoubleInterface $parser;
1919
private FileReaderInterface $fileReader;
2020
private CommandFactoryInterface $formatters;
2121

@@ -26,7 +26,7 @@ class CommandFactory implements CommandFactoryInterface
2626
];
2727

2828
public function __construct(
29-
\Docopt $parser,
29+
\Docopt|DocoptDoubleInterface $parser,
3030
FileReaderInterface $fileReader,
3131
CommandFactoryInterface $formatters
3232
) {

src/CommandLineParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private function getDocoptArgs(mixed $args): array
4545
/**
4646
* @var array<string,string>
4747
*/
48+
4849
$result = array_reduce(
4950
$keys,
5051
function ($accum, $key) use ($keys, $values): array {

src/DocoptDouble.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Differ;
44

5-
class DocoptDouble implements \Docopt
5+
class DocoptDouble implements DocoptDoubleInterface
66
{
77
/**
8-
* @var array<string,string>
8+
* @var array
99
*/
1010
public array $args;
1111

@@ -20,10 +20,7 @@ public function __construct(string $format = "stylish")
2020
];
2121
}
2222

23-
/**
24-
* @return mixed
25-
*/
26-
public function handle()
23+
public function handle(): DocoptDoubleInterface
2724
{
2825
return $this;
2926
}

src/DocoptDoubleInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Differ;
4+
5+
interface DocoptDoubleInterface
6+
{
7+
public function handle(): \Docopt\Response|DocoptDoubleInterface;
8+
}

src/FilesDiffCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ private function normalizeData(mixed $data): mixed
7272
* @return array<mixed,mixed>
7373
*/
7474
private function getContent(
75-
$fileContentKeys,
76-
$initContentDescriptor
75+
array $fileContentKeys,
76+
array $initContentDescriptor
7777
): array {
7878
$result = array_reduce(
7979
$fileContentKeys,
@@ -222,12 +222,14 @@ function ($differenceDescriptor, $fileKey) {
222222
if (is_array($file1Item)) {
223223
$file1Content = in_array($fileKey, $file1Item) ? $file1Item[$fileKey] : null;
224224
}
225+
225226
$file2Content = null;
226227
if (is_array($file2Item)) {
227228
$file2Content = in_array($fileKey, $file2Item) ? $file2Item[$fileKey] : null;
228229
}
229230

230231
$nextItemIsNotArray = !(is_array($file1Item) && is_array($file2Item));
232+
231233
$currentStatus = "not changed";
232234
if (is_array($differenceDescriptor)) {
233235
$tmpStatus = $differenceDescriptor['status'];
@@ -281,7 +283,7 @@ function ($differenceDescriptor, $fileKey) {
281283
);
282284
}
283285

284-
public function execute(CommandLineParserInterface $command): FilesDiffCommandInterface
286+
public function execute(CommandLineParserInterface $command): FilesDiffCommandInterface
285287
{
286288
$fileNames = $command->getFileNames();
287289
if (is_array($fileNames)) {

src/FilesDiffCommandInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function getContent2Descriptor(): array;
2222
* @return array<mixed,mixed>
2323
*/
2424
public function getDifferenceDescriptor(): array;
25+
26+
/**
27+
* @return array<int,string>
28+
*/
29+
public function getStatusKeys(): array;
2530
}

src/Formatters/JSONCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Differ\Formatters;
44

55
use Differ\CommandInterface;
6+
use Differ\FilesDiffCommandInterface;
67
use Differ\FormattersInterface;
78

89
class JSONCommand implements FormattersInterface
@@ -12,7 +13,7 @@ class JSONCommand implements FormattersInterface
1213
public string $filesContentString;
1314
public string $filesDiffsString;
1415

15-
public function execute(CommandInterface $command): FormattersInterface
16+
public function execute(FilesDiffCommandInterface $command): FormattersInterface
1617
{
1718
$file1Name = $command->getFile1Name();
1819
$file2Name = $command->getFile2Name();

0 commit comments

Comments
 (0)