diff --git a/bin/php-class-diagram b/bin/php-class-diagram index 505bf1c..6bbe0b5 100755 --- a/bin/php-class-diagram +++ b/bin/php-class-diagram @@ -1,15 +1,23 @@ #!/usr/bin/env php A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output. @@ -60,27 +68,25 @@ OPTIONS EOS; if (isset($options['v']) || isset($options['version'])) { - fputs(STDERR, sprintf('php-class-diagram %s%s', \Smeghead\PhpClassDiagram\Main::VERSION, PHP_EOL)); + fwrite(STDERR, sprintf('php-class-diagram %s%s', Main::VERSION, PHP_EOL)); exit(-1); } if (isset($options['h']) || isset($options['help'])) { - fputs(STDERR, $usage); + fwrite(STDERR, $usage); exit(-1); } $directory = array_shift($arguments); if (empty($directory)) { - fputs(STDERR, "ERROR: not specified php source file.\n"); - fputs(STDERR, $usage); + fwrite(STDERR, "ERROR: not specified php source file.\n"); + fwrite(STDERR, $usage); exit(-1); } -if ( ! is_dir($directory)) { - fputs(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); - fputs(STDERR, $usage); +if (!is_dir($directory)) { + fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); + fwrite(STDERR, $usage); exit(-1); } -use Smeghead\PhpClassDiagram\Config\Options; -use Smeghead\PhpClassDiagram\Main; - -new Main($directory, new Options($options)); +$main = new Main($directory, new Options($options)); +$main->run(); diff --git a/src/Config/Options.php b/src/Config/Options.php index a26e963..86a9d4a 100644 --- a/src/Config/Options.php +++ b/src/Config/Options.php @@ -16,7 +16,7 @@ final class Options public const DIAGRAM_CLASS = 'class'; public const DIAGRAM_PACKAGE = 'package'; public const DIAGRAM_JIG = 'jig'; - public const DIAGRAM_DIVSION = 'division'; + public const DIAGRAM_DIVISION = 'division'; /** * @param array $opt Option array @@ -49,7 +49,7 @@ public function diagram(): string return self::DIAGRAM_JIG; } if (isset($this->opt['division-diagram'])) { - return self::DIAGRAM_DIVSION; + return self::DIAGRAM_DIVISION; } // default return self::DIAGRAM_CLASS; diff --git a/src/Main.php b/src/Main.php index fb7ea5f..305a2ad 100644 --- a/src/Main.php +++ b/src/Main.php @@ -4,56 +4,102 @@ namespace Smeghead\PhpClassDiagram; -use Symfony\Component\Finder\Finder; +use RuntimeException; use Smeghead\PhpClassDiagram\Config\Options; -use Smeghead\PhpClassDiagram\DiagramElement\{ - Entry, - Relation, -}; +use Smeghead\PhpClassDiagram\DiagramElement\Entry; +use Smeghead\PhpClassDiagram\DiagramElement\Relation; use Smeghead\PhpClassDiagram\Php\PhpReader; +use Symfony\Component\Finder\Finder; final class Main { - const VERSION = 'v1.3.0'; + public const VERSION = 'v1.3.0'; + + public function __construct( + private string $directory, + private Options $options, + ) { + } - public function __construct(string $directory, Options $options) + public function run(): void + { + $finder = $this->createFinder(); + $entries = $this->findEntries($finder); + $this->renderEntries($entries); + } + + private function createFinder(): Finder { $finder = new Finder(); - $finder->files()->in($directory); - $finder->files()->name($options->includes()); - $excludes = $options->excludes(); + $finder->files()->in($this->directory); + $finder->files()->name($this->options->includes()); + $excludes = $this->options->excludes(); + if (count($excludes) > 0) { $finder->files()->notName($excludes)->notPath($excludes); } + + return $finder; + } + + /** + * @return list + */ + private function findEntries(Finder $finder): array + { $entries = []; foreach ($finder as $file) { try { - $reflections = PhpReader::parseFile(realpath($directory), $file->getRealPath(), $options); + $reflections = PhpReader::parseFile( + realpath($this->directory), + $file->getRealPath(), + $this->options + ); foreach ($reflections as $reflection) { - $entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $options); + $entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $this->options); } } catch (\Exception $e) { - fputs(STDERR, $e->getMessage() . "\r\n"); + fwrite(STDERR, $e->getMessage() . "\r\n"); } } - $relation = new Relation($entries, $options); - switch ($options->diagram()) { - case Options::DIAGRAM_CLASS: - echo implode("\r\n", $relation->dump()) . "\r\n"; - break; - case OPTIONS::DIAGRAM_PACKAGE: - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; - break; - case OPTIONS::DIAGRAM_JIG: - echo implode("\r\n", $relation->dump()) . "\r\n"; - echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; - echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; - break; - case OPTIONS::DIAGRAM_DIVSION: - echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; - break; - default: - throw new \Exception('invalid diagram.'); - } + return $entries; + } + + /** + * @param list $entries + */ + private function renderEntries(array $entries): void + { + $relation = new Relation($entries, $this->options); + + match ($this->options->diagram()) { + Options::DIAGRAM_CLASS => $this->renderDiagramClass($relation), + OPTIONS::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation), + OPTIONS::DIAGRAM_JIG => $this->renderDiagramJig($relation), + OPTIONS::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation), + default => throw new RuntimeException('invalid diagram.') + }; + } + + private function renderDiagramClass(Relation $relation): void + { + echo implode("\r\n", $relation->dump()) . "\r\n"; + } + + private function renderDiagramPackage(Relation $relation): void + { + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; + } + + private function renderDiagramJig(Relation $relation): void + { + echo implode("\r\n", $relation->dump()) . "\r\n"; + echo implode("\r\n", $relation->dumpPackages()) . "\r\n"; + echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; + } + + private function renderDiagramDivision(Relation $relation): void + { + echo implode("\r\n", $relation->dumpDivisions()) . "\r\n"; } } diff --git a/test/OptionsTest.php b/test/OptionsTest.php index f7b6ad4..f2770e1 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -178,7 +178,7 @@ public function testDiagram_division(): void $options = new Options($opt); - $this->assertSame(Options::DIAGRAM_DIVSION, $options->diagram(), 'diagram is division.'); + $this->assertSame(Options::DIAGRAM_DIVISION, $options->diagram(), 'diagram is division.'); } public function testDiagram3(): void {