Skip to content

Commit 16dea59

Browse files
committed
update formatters
1 parent 2442bbb commit 16dea59

File tree

7 files changed

+137
-38
lines changed

7 files changed

+137
-38
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ update:
77
validate:
88
composer validate
99

10+
analyze:
11+
./vendor/bin/phpstan analyze --level 3 src tests
12+
1013
lint:
1114
composer exec --verbose phpcs -- --standard=PSR12 src bin
1215

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"require-dev": {
1010
"squizlabs/php_codesniffer": "^3.7",
11-
"phpunit/phpunit": "^12.2.5"
11+
"phpunit/phpunit": "^12.2.5",
12+
"phpstan/phpstan": "^2.1.29"
1213
},
1314
"license": "MIT",
1415
"autoload": {

composer.lock

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

src/CommandFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ class CommandFactory implements CommandFactoryInterface
1717
{
1818
private mixed $parser;
1919
private FileReaderInterface $fileReader;
20+
/*
2021
private const array FORMAT_KEYS = [
2122
"stylish" => "stylish",
2223
"plain" => "plain",
2324
"json" => "json"
2425
];
25-
26+
*/
2627
public function __construct(
2728
mixed $parser,
2829
FileReaderInterface $fileReader
2930
) {
3031
$this->parser = $parser;
3132
$this->fileReader = $fileReader;
3233
}
33-
34+
/*
3435
public function getFormatKeys(): array
3536
{
3637
return self::FORMAT_KEYS;
3738
}
38-
39+
*/
3940
public function getCommand(string $commandType): ?CommandInterface
4041
{
4142
switch ($commandType) {
@@ -46,6 +47,7 @@ public function getCommand(string $commandType): ?CommandInterface
4647
$requestedCommand = (new FilesDiffCommand())
4748
->setFileReader($this->fileReader);
4849
break;
50+
/*
4951
case self::FORMAT_KEYS["stylish"]:
5052
$requestedCommand = new StylishCommand();
5153
break;
@@ -55,6 +57,7 @@ public function getCommand(string $commandType): ?CommandInterface
5557
case self::FORMAT_KEYS["json"]:
5658
$requestedCommand = new JSONCommand();
5759
break;
60+
*/
5861
case "format":
5962
$requestedCommand = new Formatters();
6063
break;

src/Formatters.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,32 @@
22

33
namespace Differ;
44

5+
use Differ\Formatters\StylishCommand;
6+
use Differ\Formatters\PlainCommand;
7+
use Differ\Formatters\JSONCommand;
8+
59
class Formatters implements CommandInterface
610
{
711
private CommandInterface $formatCommand;
12+
private const array FORMAT_KEYS = [
13+
"stylish" => "stylish",
14+
"plain" => "plain",
15+
"json" => "json"
16+
];
17+
18+
private function createCommand(string $commandKey): CommandInterface
19+
{
20+
switch ($commandKey) {
21+
case self::FORMAT_KEYS["stylish"]:
22+
return new StylishCommand();
23+
case self::FORMAT_KEYS["plain"]:
24+
return new PlainCommand();
25+
case self::FORMAT_KEYS["json"]:
26+
return new JSONCommand();
27+
default:
28+
return throw new DifferException("input error: unknown output format\nUse gendiff -h\n");
29+
}
30+
}
831

932
public function selectFormat(CommandInterface $command = null): CommandInterface
1033
{
@@ -13,14 +36,11 @@ public function selectFormat(CommandInterface $command = null): CommandInterface
1336
new FileReader()
1437
);
1538
$currentFormat = strtolower($command->getFormat());
16-
$formatKeys = $commandFactory->getFormatKeys();
17-
if (in_array($currentFormat, $formatKeys)) {
18-
$this->formatCommand = $commandFactory->getCommand(
19-
$currentFormat
20-
);
21-
} else {
22-
throw new DifferException("input error: unknown output format\nUse gendiff -h\n");
23-
}
39+
//$formatKeys = $commandFactory->getFormatKeys();
40+
41+
$this->formatCommand = $this->createCommand(
42+
$currentFormat
43+
);
2444

2545
return $this;
2646
}

tests/CommandFactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#[CoversClass(DifferException::class)]
2525
#[CoversClass(StylishCommand::class)]
2626
#[CoversClass(PlainCommand::class)]
27+
#[CoversMethod(CommandFactory::class, 'getCommand')]
2728
class CommandFactoryTest extends TestCase
2829
{
2930
private $commandFactory;
@@ -36,21 +37,21 @@ public function setUp(): void
3637
);
3738
}
3839

39-
#[CoversFunction(CommandFactory::class, 'getCommand')]
40+
4041
public function testGetCommand()
4142
{
4243
// Test for CommandLineParser
4344
$this->assertInstanceOf(CommandLineParser::class, $this->commandFactory->getCommand('parse'));
4445

4546
// Test for FilesDiffCommand
4647
$this->assertInstanceOf(FilesDiffCommand::class, $this->commandFactory->getCommand('difference'));
47-
48+
/*
4849
// Test for FilesDiffCommand
4950
$this->assertInstanceOf(StylishCommand::class, $this->commandFactory->getCommand('stylish'));
5051
5152
// Test for FilesDiffCommand
5253
$this->assertInstanceOf(PlainCommand::class, $this->commandFactory->getCommand('plain'));
53-
54+
*/
5455
// Test for DisplayCommand
5556
$this->assertInstanceOf(DisplayCommand::class, $this->commandFactory->getCommand('show'));
5657

tests/FormattersTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#[CoversClass(StylishCommand::class)]
2929
#[CoversClass(PlainCommand::class)]
3030
#[CoversClass(JSONCommand::class)]
31+
3132
class FormattersTest extends TestCase
3233
{
3334
public static function getParserArguments(): array

0 commit comments

Comments
 (0)