Skip to content

Commit 218633f

Browse files
authored
Merge pull request #68 from Chemaclass/refactor/improve-main-class
Improve Main class
2 parents 30765a6 + fc1261c commit 218633f

File tree

4 files changed

+101
-49
lines changed

4 files changed

+101
-49
lines changed

bin/php-class-diagram

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#!/usr/bin/env php
22
<?php declare(strict_types=1);
3+
34
namespace Smeghead\PhpClassDiagram;
45

5-
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
6+
use Smeghead\PhpClassDiagram\Config\Options;
7+
8+
foreach (
9+
[
10+
__DIR__ . '/../../../autoload.php',
11+
__DIR__ . '/../vendor/autoload.php'
12+
] as $file
13+
) {
614
if (file_exists($file)) {
715
require $file;
816
break;
917
}
1018
}
1119

12-
$options = getopt('hv',[
20+
$options = getopt('hv', [
1321
'help',
1422
'version',
1523
'class-diagram',
@@ -31,7 +39,7 @@ $options = getopt('hv',[
3139
], $rest_index);
3240
$arguments = array_slice($argv, $rest_index);
3341

34-
$usage =<<<EOS
42+
$usage = <<<EOS
3543
usage: php-class-diagram [OPTIONS] <target php source directory>
3644
3745
A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output.
@@ -60,27 +68,25 @@ OPTIONS
6068
EOS;
6169

6270
if (isset($options['v']) || isset($options['version'])) {
63-
fputs(STDERR, sprintf('php-class-diagram %s%s', \Smeghead\PhpClassDiagram\Main::VERSION, PHP_EOL));
71+
fwrite(STDERR, sprintf('php-class-diagram %s%s', Main::VERSION, PHP_EOL));
6472
exit(-1);
6573
}
6674
if (isset($options['h']) || isset($options['help'])) {
67-
fputs(STDERR, $usage);
75+
fwrite(STDERR, $usage);
6876
exit(-1);
6977
}
7078

7179
$directory = array_shift($arguments);
7280
if (empty($directory)) {
73-
fputs(STDERR, "ERROR: not specified php source file.\n");
74-
fputs(STDERR, $usage);
81+
fwrite(STDERR, "ERROR: not specified php source file.\n");
82+
fwrite(STDERR, $usage);
7583
exit(-1);
7684
}
77-
if ( ! is_dir($directory)) {
78-
fputs(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory));
79-
fputs(STDERR, $usage);
85+
if (!is_dir($directory)) {
86+
fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory));
87+
fwrite(STDERR, $usage);
8088
exit(-1);
8189
}
8290

83-
use Smeghead\PhpClassDiagram\Config\Options;
84-
use Smeghead\PhpClassDiagram\Main;
85-
86-
new Main($directory, new Options($options));
91+
$main = new Main($directory, new Options($options));
92+
$main->run();

src/Config/Options.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class Options
1616
public const DIAGRAM_CLASS = 'class';
1717
public const DIAGRAM_PACKAGE = 'package';
1818
public const DIAGRAM_JIG = 'jig';
19-
public const DIAGRAM_DIVSION = 'division';
19+
public const DIAGRAM_DIVISION = 'division';
2020

2121
/**
2222
* @param array<string, mixed> $opt Option array
@@ -49,7 +49,7 @@ public function diagram(): string
4949
return self::DIAGRAM_JIG;
5050
}
5151
if (isset($this->opt['division-diagram'])) {
52-
return self::DIAGRAM_DIVSION;
52+
return self::DIAGRAM_DIVISION;
5353
}
5454
// default
5555
return self::DIAGRAM_CLASS;

src/Main.php

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,102 @@
44

55
namespace Smeghead\PhpClassDiagram;
66

7-
use Symfony\Component\Finder\Finder;
7+
use RuntimeException;
88
use Smeghead\PhpClassDiagram\Config\Options;
9-
use Smeghead\PhpClassDiagram\DiagramElement\{
10-
Entry,
11-
Relation,
12-
};
9+
use Smeghead\PhpClassDiagram\DiagramElement\Entry;
10+
use Smeghead\PhpClassDiagram\DiagramElement\Relation;
1311
use Smeghead\PhpClassDiagram\Php\PhpReader;
12+
use Symfony\Component\Finder\Finder;
1413

1514
final class Main
1615
{
17-
const VERSION = 'v1.3.0';
16+
public const VERSION = 'v1.3.0';
17+
18+
public function __construct(
19+
private string $directory,
20+
private Options $options,
21+
) {
22+
}
1823

19-
public function __construct(string $directory, Options $options)
24+
public function run(): void
25+
{
26+
$finder = $this->createFinder();
27+
$entries = $this->findEntries($finder);
28+
$this->renderEntries($entries);
29+
}
30+
31+
private function createFinder(): Finder
2032
{
2133
$finder = new Finder();
22-
$finder->files()->in($directory);
23-
$finder->files()->name($options->includes());
24-
$excludes = $options->excludes();
34+
$finder->files()->in($this->directory);
35+
$finder->files()->name($this->options->includes());
36+
$excludes = $this->options->excludes();
37+
2538
if (count($excludes) > 0) {
2639
$finder->files()->notName($excludes)->notPath($excludes);
2740
}
41+
42+
return $finder;
43+
}
44+
45+
/**
46+
* @return list<Entry>
47+
*/
48+
private function findEntries(Finder $finder): array
49+
{
2850
$entries = [];
2951
foreach ($finder as $file) {
3052
try {
31-
$reflections = PhpReader::parseFile(realpath($directory), $file->getRealPath(), $options);
53+
$reflections = PhpReader::parseFile(
54+
realpath($this->directory),
55+
$file->getRealPath(),
56+
$this->options
57+
);
3258
foreach ($reflections as $reflection) {
33-
$entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $options);
59+
$entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $this->options);
3460
}
3561
} catch (\Exception $e) {
36-
fputs(STDERR, $e->getMessage() . "\r\n");
62+
fwrite(STDERR, $e->getMessage() . "\r\n");
3763
}
3864
}
39-
$relation = new Relation($entries, $options);
40-
switch ($options->diagram()) {
41-
case Options::DIAGRAM_CLASS:
42-
echo implode("\r\n", $relation->dump()) . "\r\n";
43-
break;
44-
case OPTIONS::DIAGRAM_PACKAGE:
45-
echo implode("\r\n", $relation->dumpPackages()) . "\r\n";
46-
break;
47-
case OPTIONS::DIAGRAM_JIG:
48-
echo implode("\r\n", $relation->dump()) . "\r\n";
49-
echo implode("\r\n", $relation->dumpPackages()) . "\r\n";
50-
echo implode("\r\n", $relation->dumpDivisions()) . "\r\n";
51-
break;
52-
case OPTIONS::DIAGRAM_DIVSION:
53-
echo implode("\r\n", $relation->dumpDivisions()) . "\r\n";
54-
break;
55-
default:
56-
throw new \Exception('invalid diagram.');
57-
}
65+
return $entries;
66+
}
67+
68+
/**
69+
* @param list<Entry> $entries
70+
*/
71+
private function renderEntries(array $entries): void
72+
{
73+
$relation = new Relation($entries, $this->options);
74+
75+
match ($this->options->diagram()) {
76+
Options::DIAGRAM_CLASS => $this->renderDiagramClass($relation),
77+
OPTIONS::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation),
78+
OPTIONS::DIAGRAM_JIG => $this->renderDiagramJig($relation),
79+
OPTIONS::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation),
80+
default => throw new RuntimeException('invalid diagram.')
81+
};
82+
}
83+
84+
private function renderDiagramClass(Relation $relation): void
85+
{
86+
echo implode("\r\n", $relation->dump()) . "\r\n";
87+
}
88+
89+
private function renderDiagramPackage(Relation $relation): void
90+
{
91+
echo implode("\r\n", $relation->dumpPackages()) . "\r\n";
92+
}
93+
94+
private function renderDiagramJig(Relation $relation): void
95+
{
96+
echo implode("\r\n", $relation->dump()) . "\r\n";
97+
echo implode("\r\n", $relation->dumpPackages()) . "\r\n";
98+
echo implode("\r\n", $relation->dumpDivisions()) . "\r\n";
99+
}
100+
101+
private function renderDiagramDivision(Relation $relation): void
102+
{
103+
echo implode("\r\n", $relation->dumpDivisions()) . "\r\n";
58104
}
59105
}

test/OptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function testDiagram_division(): void
178178

179179
$options = new Options($opt);
180180

181-
$this->assertSame(Options::DIAGRAM_DIVSION, $options->diagram(), 'diagram is division.');
181+
$this->assertSame(Options::DIAGRAM_DIVISION, $options->diagram(), 'diagram is division.');
182182
}
183183
public function testDiagram3(): void
184184
{

0 commit comments

Comments
 (0)