Skip to content

Commit

Permalink
Merge pull request #68 from Chemaclass/refactor/improve-main-class
Browse files Browse the repository at this point in the history
Improve Main class
  • Loading branch information
smeghead authored Jun 10, 2024
2 parents 30765a6 + fc1261c commit 218633f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 49 deletions.
34 changes: 20 additions & 14 deletions bin/php-class-diagram
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

namespace Smeghead\PhpClassDiagram;

foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
use Smeghead\PhpClassDiagram\Config\Options;

foreach (
[
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../vendor/autoload.php'
] as $file
) {
if (file_exists($file)) {
require $file;
break;
}
}

$options = getopt('hv',[
$options = getopt('hv', [
'help',
'version',
'class-diagram',
Expand All @@ -31,7 +39,7 @@ $options = getopt('hv',[
], $rest_index);
$arguments = array_slice($argv, $rest_index);

$usage =<<<EOS
$usage = <<<EOS
usage: php-class-diagram [OPTIONS] <target php source directory>
A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output.
Expand Down Expand Up @@ -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();
4 changes: 2 additions & 2 deletions src/Config/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $opt Option array
Expand Down Expand Up @@ -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;
Expand Down
110 changes: 78 additions & 32 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry>
*/
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<Entry> $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";
}
}
2 changes: 1 addition & 1 deletion test/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 218633f

Please sign in to comment.