From 9cc668a34627aa334a3db029fee08be29c33455c Mon Sep 17 00:00:00 2001 From: pmaasz Date: Thu, 29 Aug 2024 19:05:14 +0200 Subject: [PATCH 1/9] add an option to only render the class diagram for one single class --- bin/php-class-diagram | 9 ++++++++- src/Config/Options.php | 4 ++++ src/Main.php | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/bin/php-class-diagram b/bin/php-class-diagram index 5ca31b3..f4e619b 100755 --- a/bin/php-class-diagram +++ b/bin/php-class-diagram @@ -21,6 +21,7 @@ $options = getopt('hv', [ 'help', 'version', 'class-diagram', + 'single-class-diagram', 'package-diagram', 'division-diagram', 'jig-diagram', @@ -51,6 +52,7 @@ OPTIONS -h, --help show this help page. -v, --version show version. --class-diagram output class diagram script. (default) + --single-class-diagram output class diagram script. (default) --package-diagram output package diagram script. --division-diagram output division diagram script. --jig-diagram output class diagram and package diagram and division diagram script. @@ -88,11 +90,16 @@ if (empty($directory)) { fwrite(STDERR, $usage); exit(-1); } -if (!is_dir($directory)) { +if (!isset($options['single-class-diagram']) && !is_dir($directory)) { fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); fwrite(STDERR, $usage); exit(-1); } +if(isset($options['single-class-diagram']) && !is_file($directory)) { + fwrite(STDERR, sprintf("ERROR: specified file dose not exists. file: %s\n", $directory)); + fwrite(STDERR, $usage); + exit(-1); +} $main = new Main($directory, new Options($options)); $main->run(); diff --git a/src/Config/Options.php b/src/Config/Options.php index 9d5ccd7..e3139bc 100644 --- a/src/Config/Options.php +++ b/src/Config/Options.php @@ -14,6 +14,7 @@ final class Options public const PHP8 = 'php8'; public const DIAGRAM_CLASS = 'class'; + public const DIAGRAM_CLASS_SINGLE = 'single-class'; public const DIAGRAM_PACKAGE = 'package'; public const DIAGRAM_JIG = 'jig'; public const DIAGRAM_DIVISION = 'division'; @@ -42,6 +43,9 @@ public function diagram(): string if (isset($this->opt['class-diagram'])) { return self::DIAGRAM_CLASS; } + if (isset($this->opt['single-class-diagram'])) { + return self::DIAGRAM_CLASS_SINGLE; + } if (isset($this->opt['package-diagram'])) { return self::DIAGRAM_PACKAGE; } diff --git a/src/Main.php b/src/Main.php index 6fd8907..1f2f2bf 100644 --- a/src/Main.php +++ b/src/Main.php @@ -22,12 +22,27 @@ public function __construct( } public function run(): void + { + match ($this->options->diagram()) { + Options::DIAGRAM_CLASS_SINGLE => $this->runSingleClass(), + default => $this->runDefault(), + }; + } + + private function runDefault(): void { $finder = $this->createFinder(); $entries = $this->findEntries($finder); $this->renderEntries($entries); } + private function runSingleClass(): void + { + $finder = $this->createSingleClassFinder(); + $entries = $this->findEntries($finder); + $this->renderEntries($entries); + } + private function createFinder(): Finder { $finder = new Finder(); @@ -42,6 +57,24 @@ private function createFinder(): Finder return $finder; } + private function createSingleClassFinder(): Finder + { + $fileDir = explode('/', $this->directory); + $fileName = array_pop($fileDir); + $fileDir = implode('/', $fileDir); + + $finder = new Finder(); + $finder->files()->in($fileDir); + $finder->files()->name([$fileName]); + + $excludes = $this->options->excludes(); + if (count($excludes) > 0) { + $finder->files()->notName($excludes)->notPath($excludes); + } + + return $finder; + } + /** * @return list */ @@ -62,6 +95,7 @@ private function findEntries(Finder $finder): array fwrite(STDERR, $e->getMessage() . "\r\n"); } } + return $entries; } @@ -74,6 +108,7 @@ private function renderEntries(array $entries): void match ($this->options->diagram()) { Options::DIAGRAM_CLASS => $this->renderDiagramClass($relation), + Options::DIAGRAM_CLASS_SINGLE => $this->renderDiagramSingleClass($relation), OPTIONS::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation), OPTIONS::DIAGRAM_JIG => $this->renderDiagramJig($relation), OPTIONS::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation), @@ -86,6 +121,11 @@ private function renderDiagramClass(Relation $relation): void echo implode("\r\n", $relation->dump()) . "\r\n"; } + private function renderDiagramSingleClass(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"; From 9bfdadf2dde58e4d503f8f398787e1646a4660af Mon Sep 17 00:00:00 2001 From: pmaasz Date: Thu, 29 Aug 2024 19:11:30 +0200 Subject: [PATCH 2/9] add minimal test --- test/OptionsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/OptionsTest.php b/test/OptionsTest.php index a39d676..c3a0444 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -171,6 +171,7 @@ public function provideDiagrams(): array [['package-diagram' => true], Options::DIAGRAM_PACKAGE], [['division-diagram' => true], Options::DIAGRAM_DIVISION], [['jig-diagram' => true], Options::DIAGRAM_JIG], + [['class-single-diagram' => true], Options::DIAGRAM_CLASS_SINGLE], ]; } public function testHeader(): void From ce64f743eb7dfcfdc8578c00baf12529f53bb620 Mon Sep 17 00:00:00 2001 From: pmaasz Date: Thu, 29 Aug 2024 19:48:27 +0200 Subject: [PATCH 3/9] directory or single file are accepted as parameter --- bin/php-class-diagram | 23 ++++++++++++----------- src/Config/Options.php | 4 ---- src/Main.php | 9 +++++---- test/OptionsTest.php | 1 - 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/bin/php-class-diagram b/bin/php-class-diagram index f4e619b..bb0e094 100755 --- a/bin/php-class-diagram +++ b/bin/php-class-diagram @@ -21,7 +21,6 @@ $options = getopt('hv', [ 'help', 'version', 'class-diagram', - 'single-class-diagram', 'package-diagram', 'division-diagram', 'jig-diagram', @@ -52,7 +51,6 @@ OPTIONS -h, --help show this help page. -v, --version show version. --class-diagram output class diagram script. (default) - --single-class-diagram output class diagram script. (default) --package-diagram output package diagram script. --division-diagram output division diagram script. --jig-diagram output class diagram and package diagram and division diagram script. @@ -90,15 +88,18 @@ if (empty($directory)) { fwrite(STDERR, $usage); exit(-1); } -if (!isset($options['single-class-diagram']) && !is_dir($directory)) { - fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); - fwrite(STDERR, $usage); - exit(-1); -} -if(isset($options['single-class-diagram']) && !is_file($directory)) { - fwrite(STDERR, sprintf("ERROR: specified file dose not exists. file: %s\n", $directory)); - fwrite(STDERR, $usage); - exit(-1); +if (!is_file($directory) && !is_dir($directory)) { + if(!is_file($directory)) { + fwrite(STDERR, sprintf("ERROR: specified file dose not exists. file: %s\n", $directory)); + fwrite(STDERR, $usage); + exit(-1); + } + + if (!is_dir($directory)) { + fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); + fwrite(STDERR, $usage); + exit(-1); + } } $main = new Main($directory, new Options($options)); diff --git a/src/Config/Options.php b/src/Config/Options.php index e3139bc..9d5ccd7 100644 --- a/src/Config/Options.php +++ b/src/Config/Options.php @@ -14,7 +14,6 @@ final class Options public const PHP8 = 'php8'; public const DIAGRAM_CLASS = 'class'; - public const DIAGRAM_CLASS_SINGLE = 'single-class'; public const DIAGRAM_PACKAGE = 'package'; public const DIAGRAM_JIG = 'jig'; public const DIAGRAM_DIVISION = 'division'; @@ -43,9 +42,6 @@ public function diagram(): string if (isset($this->opt['class-diagram'])) { return self::DIAGRAM_CLASS; } - if (isset($this->opt['single-class-diagram'])) { - return self::DIAGRAM_CLASS_SINGLE; - } if (isset($this->opt['package-diagram'])) { return self::DIAGRAM_PACKAGE; } diff --git a/src/Main.php b/src/Main.php index 1f2f2bf..5da3ff5 100644 --- a/src/Main.php +++ b/src/Main.php @@ -23,10 +23,11 @@ public function __construct( public function run(): void { - match ($this->options->diagram()) { - Options::DIAGRAM_CLASS_SINGLE => $this->runSingleClass(), - default => $this->runDefault(), - }; + if (!is_dir($this->directory)) { + $this->runSingleClass(); + } + + $this->runDefault(); } private function runDefault(): void diff --git a/test/OptionsTest.php b/test/OptionsTest.php index c3a0444..a39d676 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -171,7 +171,6 @@ public function provideDiagrams(): array [['package-diagram' => true], Options::DIAGRAM_PACKAGE], [['division-diagram' => true], Options::DIAGRAM_DIVISION], [['jig-diagram' => true], Options::DIAGRAM_JIG], - [['class-single-diagram' => true], Options::DIAGRAM_CLASS_SINGLE], ]; } public function testHeader(): void From 47b2368b894b129396d43797c1c4a7fba9097918 Mon Sep 17 00:00:00 2001 From: pmaasz Date: Thu, 29 Aug 2024 19:49:26 +0200 Subject: [PATCH 4/9] directory or single file are accepted as parameter --- src/Main.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Main.php b/src/Main.php index 5da3ff5..72d26b5 100644 --- a/src/Main.php +++ b/src/Main.php @@ -25,6 +25,8 @@ public function run(): void { if (!is_dir($this->directory)) { $this->runSingleClass(); + + return; } $this->runDefault(); From 3981207a44a8f0df73767b44b97e3c53efef9087 Mon Sep 17 00:00:00 2001 From: pmaasz Date: Thu, 29 Aug 2024 19:53:43 +0200 Subject: [PATCH 5/9] directory or single file are accepted as parameter --- src/Main.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Main.php b/src/Main.php index 72d26b5..44dcb85 100644 --- a/src/Main.php +++ b/src/Main.php @@ -68,7 +68,9 @@ private function createSingleClassFinder(): Finder $finder = new Finder(); $finder->files()->in($fileDir); - $finder->files()->name([$fileName]); + $finder->files()->name([ + $fileName, + ]); $excludes = $this->options->excludes(); if (count($excludes) > 0) { @@ -111,7 +113,6 @@ private function renderEntries(array $entries): void match ($this->options->diagram()) { Options::DIAGRAM_CLASS => $this->renderDiagramClass($relation), - Options::DIAGRAM_CLASS_SINGLE => $this->renderDiagramSingleClass($relation), OPTIONS::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation), OPTIONS::DIAGRAM_JIG => $this->renderDiagramJig($relation), OPTIONS::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation), @@ -124,11 +125,6 @@ private function renderDiagramClass(Relation $relation): void echo implode("\r\n", $relation->dump()) . "\r\n"; } - private function renderDiagramSingleClass(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"; From c84be99811a308d13c1faeaaf2172e0aeb42e864 Mon Sep 17 00:00:00 2001 From: pmaasz Date: Fri, 30 Aug 2024 15:27:11 +0200 Subject: [PATCH 6/9] cosmetic change --- src/Main.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Main.php b/src/Main.php index 44dcb85..c2e2f17 100644 --- a/src/Main.php +++ b/src/Main.php @@ -113,9 +113,9 @@ private function renderEntries(array $entries): void 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), + Options::DIAGRAM_PACKAGE => $this->renderDiagramPackage($relation), + Options::DIAGRAM_JIG => $this->renderDiagramJig($relation), + Options::DIAGRAM_DIVISION => $this->renderDiagramDivision($relation), default => throw new RuntimeException('invalid diagram.') }; } From 20f0a5d3ac32fd54e963ba7e85bc5509a0df021f Mon Sep 17 00:00:00 2001 From: pmaasz Date: Sat, 31 Aug 2024 11:30:53 +0200 Subject: [PATCH 7/9] changes if to check file exists and only put out error for directory --- bin/php-class-diagram | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/bin/php-class-diagram b/bin/php-class-diagram index bb0e094..9f539c1 100755 --- a/bin/php-class-diagram +++ b/bin/php-class-diagram @@ -88,18 +88,10 @@ if (empty($directory)) { fwrite(STDERR, $usage); exit(-1); } -if (!is_file($directory) && !is_dir($directory)) { - if(!is_file($directory)) { - fwrite(STDERR, sprintf("ERROR: specified file dose not exists. file: %s\n", $directory)); - fwrite(STDERR, $usage); - exit(-1); - } - - if (!is_dir($directory)) { - fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); - fwrite(STDERR, $usage); - exit(-1); - } +if (!file_exists($directory)) { + fwrite(STDERR, sprintf("ERROR: specified directory dose not exists. directory: %s\n", $directory)); + fwrite(STDERR, $usage); + exit(-1); } $main = new Main($directory, new Options($options)); From 35fce94c7b6c8e5981d60ee4a81718eab46f4c2c Mon Sep 17 00:00:00 2001 From: pmaasz Date: Sat, 31 Aug 2024 12:23:54 +0200 Subject: [PATCH 8/9] removes code because classes already work --- src/Main.php | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/src/Main.php b/src/Main.php index c2e2f17..ce7bd41 100644 --- a/src/Main.php +++ b/src/Main.php @@ -22,30 +22,12 @@ public function __construct( } public function run(): void - { - if (!is_dir($this->directory)) { - $this->runSingleClass(); - - return; - } - - $this->runDefault(); - } - - private function runDefault(): void { $finder = $this->createFinder(); $entries = $this->findEntries($finder); $this->renderEntries($entries); } - private function runSingleClass(): void - { - $finder = $this->createSingleClassFinder(); - $entries = $this->findEntries($finder); - $this->renderEntries($entries); - } - private function createFinder(): Finder { $finder = new Finder(); @@ -60,26 +42,6 @@ private function createFinder(): Finder return $finder; } - private function createSingleClassFinder(): Finder - { - $fileDir = explode('/', $this->directory); - $fileName = array_pop($fileDir); - $fileDir = implode('/', $fileDir); - - $finder = new Finder(); - $finder->files()->in($fileDir); - $finder->files()->name([ - $fileName, - ]); - - $excludes = $this->options->excludes(); - if (count($excludes) > 0) { - $finder->files()->notName($excludes)->notPath($excludes); - } - - return $finder; - } - /** * @return list */ From bc5340831ec38706581649cc3a1e4c2b821db528 Mon Sep 17 00:00:00 2001 From: pmaasz Date: Sat, 31 Aug 2024 12:27:27 +0200 Subject: [PATCH 9/9] removes code because classes already work --- src/Main.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Main.php b/src/Main.php index ce7bd41..d36e887 100644 --- a/src/Main.php +++ b/src/Main.php @@ -30,6 +30,14 @@ public function run(): void private function createFinder(): Finder { + if (is_file($this->directory)) { + $finder = new Finder(); + $finder->files()->in(dirname($this->directory)); + $finder->files()->name($this->options->includes()); + + return $finder; + } + $finder = new Finder(); $finder->files()->in($this->directory); $finder->files()->name($this->options->includes());