Skip to content

Commit 0df2fb6

Browse files
committed
using arguments array
1 parent 3e576ab commit 0df2fb6

File tree

6 files changed

+27
-56
lines changed

6 files changed

+27
-56
lines changed

src/CommandInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface CommandInterface
66
{
7-
public function execute(object $data): object | null;
7+
public function execute(array $data): ?array;
88
}

src/CommandLineParser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public function __construct(string $docopt)
1313
$this->docopt = $docopt;
1414
}
1515

16-
public function execute(): object | null
16+
public function execute(): ?array
1717
{
18-
return (new Docopt())->handle($this->docopt, array('version' => '1.0.6'));
18+
return (new Docopt())->handle($this->docopt, array('version' => '1.0.6'))
19+
->args;
1920
}
2021
}

src/CommandLineParserInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface CommandLineParserInterface
66
{
7-
public function execute(): object | null;
7+
public function execute(): ?array;
88
}

src/ConsoleApp.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ class ConsoleApp
77
private string $docopt;
88
private array $commandTypeList;
99
private $currentCommand;
10+
private array $cliData;
11+
private array $filesContent;
1012
private CommandFactoryInterface $commandFactory;
11-
private CommandLineParserInterface $commandLineParser;
12-
private CommandInterface $filesDiffCommand;
13-
private CommandInterface $displayCommand;
14-
private object $cliData;
15-
private object $filesContent;
1613

1714
public function __construct()
1815
{
@@ -46,37 +43,16 @@ public function run(): void
4643
$this->cliData = $this->currentCommand->execute();
4744
break;
4845
case "difference":
49-
$this->filesContent = $this->currentCommand->setFileReader(new FileReader())
50-
->execute($this->cliData);
46+
$this->currentCommand->setFileReader(new FileReader());
47+
$this->filesContent = $this->currentCommand->execute($this->cliData);
5148
break;
5249
case "show":
53-
$this->currentCommand->execute($this->filesContent)
54-
->showDiffsToConsole();
50+
$this->currentCommand->execute($this->filesContent);
51+
$this->currentCommand->showDiffsToConsole();
5552
break;
5653
default:
5754
throw new \Exception('unknown command type');
5855
}
5956
}
60-
/*
61-
if ($this->commandLineParser = $this->commandFactory->getCommand("parse")) {
62-
$this->cliData = $this->commandLineParser->execute();
63-
} else {
64-
throw new \Exception("can't create command line parser");
65-
}
66-
67-
if ($this->filesDiffCommand = $this->commandFactory->getCommand("difference")) {
68-
$this->filesContent = $this->filesDiffCommand->setFileReader(new FileReader())
69-
->execute($this->cliData);
70-
} else {
71-
throw new \Exception("can't create files difference command");
72-
}
73-
74-
if ($this->displayCommand = $this->commandFactory->getCommand("show")) {
75-
$this->displayCommand->execute($this->filesContent)
76-
->showDiffsToConsole();
77-
} else {
78-
throw new \Exception("can't create display command");
79-
}
80-
*/
8157
}
8258
}

src/DisplayCommand.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,33 @@ public function __construct()
1111
{
1212
$this->filesContent = [];
1313
$this->filesDiffContent = [];
14-
15-
return $this;
1614
}
1715

1816
private function constructContent($accum, $item)
1917
{
2018
return $accum .= "\n " . $item;
2119
}
2220

23-
public function execute(object $data): object
21+
public function execute(array $data): ?array
2422
{
2523

2624
$this->filesContent[] = "file1.json content:\n";
2725
$this->filesContent[] = array_reduce(
28-
$data->file1,
26+
$data['file1'],
2927
[$this, 'constructContent'],
3028
"{"
3129
) . "\r}\n";
3230

3331
$this->filesContent[] = "file2.json content:\n";
3432
$this->filesContent[] = array_reduce(
35-
$data->file2,
33+
$data['file2'],
3634
[$this, 'constructContent'],
3735
"{"
3836
) . "\n}\n";
3937

40-
$file1Array = $data->file1;
38+
$file1Array = $data['file1'];
4139
$file1Keys = array_keys($file1Array);
42-
$file2Array = $data->file2;
40+
$file2Array = $data['file2'];
4341
$this->filesDiffContent = array_map(
4442
function ($file1Key) use ($file1Array, $file2Array) {
4543
if (array_key_exists($file1Key, $file2Array)) {
@@ -55,20 +53,20 @@ function ($file1Key) use ($file1Array, $file2Array) {
5553
},
5654
$file1Keys
5755
);
58-
return $this;
56+
return
57+
[
58+
"content" => $this->filesContent,
59+
"diff" => $this->filesDiffContent
60+
];
5961
}
6062

6163
public function showContentToConsole()
6264
{
6365
echo implode("", $this->filesContent);
64-
65-
return $this;
6666
}
6767

6868
public function showDiffsToConsole()
6969
{
7070
echo "{\n" . implode("", $this->filesDiffContent) . "}\n";
71-
72-
return $this;
7371
}
7472
}

src/FilesDiffCommand.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,22 @@ public function __construct()
1313
{
1414
$this->file1Content = [];
1515
$this->file2Content = [];
16-
17-
return $this;
1816
}
1917

20-
public function setFileReader(FileReaderInterface $fileReader): object
18+
public function setFileReader(FileReaderInterface $fileReader)
2119
{
2220
$this->fileReader = $fileReader;
23-
24-
return $this;
2521
}
2622

27-
public function execute(object $cliData): object
23+
public function execute(array $cliData): ?array
2824
{
29-
if (isset($cliData->args['FILE1']) && isset($cliData->args['FILE2'])) {
30-
$this->file1Content = $this->fileReader->readFile($cliData->args['FILE1']);
31-
$this->file2Content = $this->fileReader->readFile($cliData->args['FILE2']);
25+
if (isset($cliData['FILE1']) && isset($cliData['FILE2'])) {
26+
$this->file1Content = $this->fileReader->readFile($cliData['FILE1']);
27+
$this->file2Content = $this->fileReader->readFile($cliData['FILE2']);
3228
}
3329

3430
return
35-
(object)[
31+
[
3632
'file1' => $this->file1Content,
3733
'file2' => $this->file2Content
3834
];

0 commit comments

Comments
 (0)