Skip to content

Commit c6e7408

Browse files
authored
Merge pull request #69 from Chemaclass/refactor/php-reader
Improve PhpReader
2 parents 218633f + fc9472b commit c6e7408

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

src/DiagramElement/Package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getLogicalName(): string
3636
}
3737

3838
/**
39-
* @param string[] $paths Paths
39+
* @param list<string> $paths
4040
*/
4141
public function addEntry(array $paths, Entry $entry): string
4242
{

src/DiagramElement/Relation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public function __construct(array $entries, Options $options)
1919
$this->options = $options;
2020
$this->package = new Package([], 'ROOT', $options);
2121
foreach ($entries as $e) {
22-
$this->package->addEntry(preg_split('/[\\\\\/]/', $e->getDirectory()), $e);
22+
/** @var list<string> $paths */
23+
$paths = preg_split('/[\\\\\/]/', $e->getDirectory());
24+
$this->package->addEntry($paths, $e);
2325
}
2426
}
2527

src/Php/PhpReader.php

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,41 @@
55
namespace Smeghead\PhpClassDiagram\Php;
66

77
use PhpParser\Error;
8-
use PhpParser\ParserFactory;
9-
use PhpParser\Node\Stmt\{
10-
Namespace_,
11-
ClassLike,
12-
};
8+
use PhpParser\Node;
9+
use PhpParser\Node\Stmt\ClassLike;
10+
use PhpParser\Node\Stmt\Namespace_;
1311
use PhpParser\NodeTraverser;
1412
use PhpParser\NodeVisitor\NameResolver;
13+
use PhpParser\ParserFactory;
14+
use RuntimeException;
1515
use Smeghead\PhpClassDiagram\Config\Options;
1616

1717
final class PhpReader
1818
{
19-
private PhpClass $class;
19+
private function __construct(
20+
private PhpClass $class,
21+
) {
22+
}
2023

21-
private function __construct(PhpClass $class)
24+
public function getInfo(): PhpClass
2225
{
23-
$this->class = $class;
26+
return $this->class;
2427
}
2528

2629
/**
27-
* @return PhpReader[]
30+
* @return list<PhpReader>
2831
*/
2932
public static function parseFile(string $directory, string $filename, Options $options): array
3033
{
3134
$code = file_get_contents($filename);
3235

33-
$targetVesion = ParserFactory::PREFER_PHP7;
34-
switch ($options->phpVersion()) {
35-
case 'php5':
36-
$targetVesion = ParserFactory::PREFER_PHP5;
37-
break;
38-
case 'php7':
39-
$targetVesion = ParserFactory::PREFER_PHP7;
40-
break;
41-
case 'php8':
42-
$targetVesion = ParserFactory::PREFER_PHP7; // php-parser でまだ php8 がサポートされていない。
43-
break;
44-
default:
45-
throw new \Exception("invalid php version. {$targetVesion}\n");
46-
}
47-
$parser = (new ParserFactory)->create($targetVesion);
36+
$targetVersion = match ($options->phpVersion()) {
37+
'php5' => ParserFactory::PREFER_PHP5,
38+
'php7', 'php8' => ParserFactory::PREFER_PHP7, // php-parser でまだ php8 がサポートされていない。
39+
default => throw new RuntimeException(sprintf("invalid php version %s\n", ParserFactory::PREFER_PHP7)),
40+
};
41+
42+
$parser = (new ParserFactory)->create($targetVersion);
4843
try {
4944
$ast = $parser->parse($code);
5045
$nameResolver = new NameResolver();
@@ -53,43 +48,45 @@ public static function parseFile(string $directory, string $filename, Options $o
5348
// Resolve names
5449
$ast = $nodeTraverser->traverse($ast);
5550
} catch (Error $error) {
56-
throw new \Exception("Parse error: {$error->getMessage()} file: {$filename}\n");
51+
throw new RuntimeException(sprintf("Parse error: %s file: %s\n", $error->getMessage(), $filename));
5752
}
5853

5954
$relativePath = mb_substr($filename, mb_strlen($directory) + 1);
6055
$classes = [];
6156
foreach (self::getClasses($relativePath, $ast) as $class) {
6257
$classes[] = new self($class);
6358
}
59+
6460
return $classes;
6561
}
6662

6763
/**
68-
* @param \PhpParser\Node[] $ast
69-
* @return PhpClass[]|null
64+
* @param list<Node> $ast
65+
*
66+
* @return list<PhpClass>
7067
*/
71-
private static function getClasses(string $relativePath, array $ast): ?array
68+
private static function getClasses(string $relativePath, array $ast): array
7269
{
7370
if (count($ast) === 0) {
74-
return null;
71+
return [];
7572
}
73+
7674
$classes = [];
7775
foreach ($ast as $element) {
7876
if ($element instanceof ClassLike) {
7977
$classes[] = new PhpClass($relativePath, $element, $ast);
80-
} else if ($element instanceof Namespace_) {
78+
continue;
79+
}
80+
81+
if ($element instanceof Namespace_) {
8182
foreach ($element->stmts as $e) {
8283
if ($e instanceof ClassLike) {
8384
$classes[] = new PhpClass($relativePath, $e, $ast);
8485
}
8586
}
8687
}
8788
}
88-
return $classes;
89-
}
9089

91-
public function getInfo(): PhpClass
92-
{
93-
return $this->class;
90+
return $classes;
9491
}
9592
}

0 commit comments

Comments
 (0)