Skip to content

Commit 7e0a55e

Browse files
committed
added type checking to gendiff user function
1 parent f804aae commit 7e0a55e

13 files changed

+115
-92
lines changed

composer.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CommandFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Differ\DifferException;
1515
use Differ\CommandLineParserInterface as CLP;
1616
use Differ\FilesDiffCommandInterface as FDCI;
17-
use Differ\CommandInterface as CI;
1817
use Differ\FormattersInterface as FI;
1918
use Differ\DisplayCommandInterface as DCI;
2019

@@ -48,7 +47,7 @@ public function getFormatKeys(): array
4847
return self::FORMAT_KEYS;
4948
}
5049

51-
public function createCommand(string $commandType): CLP|FDCI|CI|FI|DCI
50+
public function createCommand(string $commandType): CLP|FDCI|FI|DCI
5251
{
5352
switch ($commandType) {
5453
case "parse":

src/CommandFactoryInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use Differ\CommandLineParserInterface as CLPI;
66
use Differ\FilesDiffCommandInterface as FDCI;
7-
use Differ\CommandInterface as CI;
87
use Differ\FormattersInterface as FI;
98
use Differ\DisplayCommandInterface as DCI;
109

1110
interface CommandFactoryInterface
1211
{
13-
public function createCommand(string $commandType): CLPI|FDCI|CI|FI|DCI;
12+
public function createCommand(string $commandType): CLPI|FDCI|FI|DCI;
1413
}

src/CommandInterface.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/ConsoleApp.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,61 @@
22

33
namespace Differ;
44

5-
use Differ\CommandLineParser;
6-
use Differ\CommandInterface as CI;
5+
use Differ\CommandFactoryInterface as CFI;
76
use Differ\CommandLineParserInterface as CLPI;
87
use Differ\FilesDiffCommandInterface as FDCI;
98
use Differ\FormattersInterface as FI;
109
use Differ\DisplayCommandInterface as DCI;
1110

1211
class ConsoleApp
1312
{
14-
private CLPI $parseCommand;
15-
private CI|CLPI|FDCI|FI|DCI $nextCommand;
16-
private CommandFactoryInterface $commandFactory;
17-
18-
/**
19-
* @var array<string> $flowSteps
20-
*/
21-
private array $flowSteps;
22-
private CI|CLPI|FDCI|FI|DCI $initCommand;
13+
private CLPI $parseCommand;
14+
private FDCI $nextFDCICommand;
15+
private FI $nextFICommand;
16+
private DCI $nextDCICommand;
17+
private CFI $commandFactory;
18+
private CLPI $initCLPICommand;
19+
private FDCI $initFDCICommand;
20+
private FI $initFICommand;
2321

2422
public function __construct(
2523
CommandFactoryInterface $commandFactory
2624
) {
2725
$this->commandFactory = $commandFactory;
2826

29-
$this->parseCommand = $this->commandFactory->createCommand("parse");
30-
$this->initCommand = $this->parseCommand->execute($this->parseCommand);
31-
$this->flowSteps = [
32-
"difference",
33-
strtolower($this->parseCommand->getFormat()),
34-
"show"
35-
];
27+
$parser = $this->commandFactory->createCommand("parse");
28+
if ($parser instanceof CLPI) {
29+
$this->parseCommand = $parser;
30+
} else {
31+
throw new DifferException("internal error: invalid type for \"parse\" command");
32+
}
33+
$this->initCLPICommand = $this->parseCommand->execute($this->parseCommand);
3634
}
3735

3836
public function run(): void
3937
{
40-
$commandFactory = $this->commandFactory;
41-
foreach ($this->flowSteps as $step) {
42-
$currentCommand = $commandFactory->createCommand($step);
43-
$this->nextCommand = $currentCommand->execute($this->initCommand);
44-
$this->initCommand = $this->nextCommand;
38+
$differ = $this->commandFactory->createCommand("difference");
39+
if ($differ instanceof FDCI) {
40+
$this->nextFDCICommand = $differ;
41+
} else {
42+
throw new DifferException("internal error: invalid type for \"difference\" command");
43+
}
44+
$this->initFDCICommand = $this->nextFDCICommand->execute($this->initCLPICommand);
45+
46+
$formatter = $this->commandFactory->createCommand(strtolower($this->parseCommand->getFormat()));
47+
if ($formatter instanceof FI) {
48+
$this->nextFICommand = $formatter;
49+
} else {
50+
throw new DifferException("internal error: invalid type for \"format\" command");
51+
}
52+
$this->initFICommand = $this->nextFICommand->execute($this->initFDCICommand);
53+
54+
$showCommand = $this->commandFactory->createCommand("show");
55+
if ($showCommand instanceof DCI) {
56+
$this->nextDCICommand = $showCommand;
57+
} else {
58+
throw new DifferException("internal error: invalid type for \"show\" command");
4559
}
60+
$this->nextDCICommand->execute($this->initFICommand);
4661
}
4762
}

src/Differ.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use Differ\FileReader;
99
use Differ\DisplayCommand;
1010
use Differ\Formatters;
11+
use Differ\DifferException;
12+
use Differ\CommandLineParserInterface as CLPI;
13+
use Differ\FilesDiffCommandInterface as FDCI;
14+
use Differ\FormattersInterface as FI;
15+
use Differ\DisplayCommandInterface as DCI;
1116

1217
function genDiff(
1318
string $pathToFile1,
@@ -19,25 +24,45 @@ function genDiff(
1924
new FileReader(),
2025
new Formatters()
2126
);
27+
28+
/**
29+
* @var CLPI $parseCommand
30+
*/
2231
$parseCommand = $commandFactory->createCommand('parse');
2332
$fileNames = [
2433
"FILE1" => $pathToFile1,
2534
"FILE2" => $pathToFile2
2635
];
2736

28-
$initCommand = $parseCommand->setFileNames($fileNames)
37+
/**
38+
* @var CLPI $initCLPICommand
39+
*/
40+
$initCLPICommand = $parseCommand->setFileNames($fileNames)
2941
->setFormat($format);
30-
$flowSteps = [
31-
"difference",
32-
strtolower($parseCommand->getFormat())
33-
];
34-
foreach ($flowSteps as $step) {
35-
$currentCommand = $commandFactory->createCommand($step);
36-
$nextCommand = $currentCommand->execute($initCommand);
37-
$initCommand = $nextCommand;
42+
43+
$differ = $commandFactory->createCommand("difference");
44+
if ($differ instanceof FDCI) {
45+
$nextFDCICommand = $differ;
46+
} else {
47+
throw new DifferException("internal error: invalid type for \"difference\" command");
48+
}
49+
$initFDCICommand = $nextFDCICommand->execute($initCLPICommand);
50+
51+
$formatter = $commandFactory->createCommand(strtolower($parseCommand->getFormat()));
52+
if ($formatter instanceof FI) {
53+
$nextFICommand = $formatter;
54+
} else {
55+
throw new DifferException("internal error: invalid type for \"format\" command");
56+
}
57+
$initFICommand = $nextFICommand->execute($initFDCICommand);
58+
59+
$showCommand = $commandFactory->createCommand("show");
60+
if ($showCommand instanceof DCI) {
61+
$nextDCICommand = $showCommand;
62+
} else {
63+
throw new DifferException("internal error: invalid type for \"show\" command");
3864
}
39-
$displayCommand = $commandFactory->createCommand("show");
4065

41-
return $displayCommand->setFormatter($initCommand)
66+
return $nextDCICommand->setFormatter($initFICommand)
4267
->getFilesDiffs();
4368
}

src/DisplayCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Differ\CommandLineParserInterface as CLP;
66
use Differ\FilesDiffCommandInterface as FDCI;
7-
use Differ\CommandInterface as CI;
87
use Differ\FormattersInterface as FI;
98
use Differ\DisplayCommandInterface as DCI;
109

src/DisplayCommandInterface.php

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

33
namespace Differ;
44

5+
use Differ\DisplayCommandInterface as DCI;
6+
use Differ\FormattersInterface as FI;
7+
58
interface DisplayCommandInterface
69
{
7-
public function execute(FormattersInterface $command): DisplayCommandInterface;
10+
public function execute(FormattersInterface $command): DCI;
811
public function getFilesContent(): string;
912
public function getFilesDiffs(): string;
10-
}
13+
public function setFormatter(FI $formatter): DCI;
14+
}

0 commit comments

Comments
 (0)