Skip to content

Commit 7484087

Browse files
committed
add --include and --exclude options.
1 parent 6a9ecb3 commit 7484087

File tree

6 files changed

+105
-3
lines changed

6 files changed

+105
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
### Features
44

55
* add package diagram description to README.
6+
* Updated the PHP version of the development environment to 8.1.
7+
* add `--include` option. Enabled to specify the search pattern of the PHP source code to be processed.
8+
* add `--exclude` option. Enabled to specify the exclusion pattern of the PHP source code to be processed.
69

710
## v0.0.8 (2022-11-21)
811

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7.4-cli
1+
FROM php:8.1-cli
22

33
RUN apt-get update && apt-get install -y zip git plantuml && apt-get clean && rm -rf /var/lib/apt/lists/*
44

bin/php-class-diagram

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ $options = getopt('hv',[
2222
'php7',
2323
'php8',
2424
'header::',
25+
'include::',
26+
'exclude::',
2527
], $rest_index);
2628
$arguments = array_slice($argv, $rest_index);
2729

@@ -32,6 +34,7 @@ A CLI tool that parses the PHP source directory and outputs PlantUML class diagr
3234
3335
OPTIONS
3436
-h, --help show this help page.
37+
-v, --version show version.
3538
--class-diagram output class diagram script. (default)
3639
--package-diagram output package diagram script.
3740
--jig-diagram output class diagram and package diagram script.
@@ -43,6 +46,8 @@ OPTIONS
4346
--php7 parse php source file as php7.
4447
--php8 parse php source file as php8. (not suppoted)
4548
--header='header string' additional header string. You can specify multiple header values.
49+
--include='wildcard' include target file pattern. (default: `*.php`) You can specify multiple include patterns.
50+
--exclude='wildcard' exclude target file pattern. You can specify multiple exclude patterns.
4651
4752
EOS;
4853

src/Config/Options.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,34 @@ public function headers(): array {
8989
}
9090
return $headers;
9191
}
92+
93+
/**
94+
* @return string[] specified includes
95+
*/
96+
public function includes(): array {
97+
$includes = [];
98+
if (isset($this->opt['include'])) {
99+
$includes = $this->opt['include'];
100+
if ( ! is_array($includes)) {
101+
$includes = [$includes];
102+
}
103+
} else {
104+
$includes[] = '*.php';
105+
}
106+
return $includes;
107+
}
108+
109+
/**
110+
* @return string[] specified excludes
111+
*/
112+
public function excludes(): array {
113+
$excludes = [];
114+
if (isset($this->opt['exclude'])) {
115+
$excludes = $this->opt['exclude'];
116+
if ( ! is_array($excludes)) {
117+
$excludes = [$excludes];
118+
}
119+
}
120+
return $excludes;
121+
}
92122
}

src/Main.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ class Main {
1515
public function __construct(string $directory, Options $options) {
1616
$finder = new Finder();
1717
$finder->files()->in($directory);
18-
$finder->files()->name('*.php');
18+
$finder->files()->name($options->includes());
19+
$excludes = $options->excludes();
20+
if (count($excludes) > 0) {
21+
$finder->files()->notName($excludes);
22+
}
1923
$entries = [];
2024
foreach ($finder as $file) {
2125
try {
2226
$reflections = PhpReader::parseFile(realpath($directory), $file->getRealPath(), $options);
2327
foreach ($reflections as $reflection) {
24-
$entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $options);
28+
$entries[] = new Entry($file->getRelativePath(), $reflection->getInfo(), $options);
2529
}
2630
} catch (\Exception $e) {
2731
fputs(STDERR, $e->getMessage() . "\r\n");

test/OptionsTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,65 @@ public function testMultipleHeaders(): void {
163163
$this->assertSame('title PHP Class Diagram', $options->headers()[0], 'specified header. title');
164164
$this->assertSame('skinparam pageMargin 10', $options->headers()[1], 'specified header. pageMargin');
165165
}
166+
public function testInclude(): void {
167+
$opt = [
168+
'include' => '*.php8',
169+
];
170+
171+
$options = new Options($opt);
172+
173+
$this->assertSame('*.php8', $options->includes()[0], 'specified include.');
174+
}
175+
public function testMultipleInclude(): void {
176+
$opt = [
177+
'include' => [
178+
'*.php7',
179+
'*.php8',
180+
],
181+
];
182+
183+
$options = new Options($opt);
184+
185+
$this->assertSame('*.php7', $options->includes()[0], 'specified include. php7');
186+
$this->assertSame('*.php8', $options->includes()[1], 'specified include. php8');
187+
}
188+
public function testIncludeDefault(): void {
189+
$opt = [
190+
];
191+
192+
$options = new Options($opt);
193+
194+
$this->assertSame('*.php', $options->includes()[0], 'default include.');
195+
}
196+
public function testexclude(): void {
197+
$opt = [
198+
'exclude' => '*Exception.php',
199+
];
200+
201+
$options = new Options($opt);
202+
203+
$this->assertSame('*Exception.php', $options->excludes()[0], 'specified exclude.');
204+
}
205+
public function testMultipleExclude(): void {
206+
$opt = [
207+
'exclude' => [
208+
'*Exception.php',
209+
'config.php',
210+
],
211+
];
212+
213+
$options = new Options($opt);
214+
215+
$this->assertSame('*Exception.php', $options->excludes()[0], 'specified exclude. *Exception.php');
216+
$this->assertSame('config.php', $options->excludes()[1], 'specified exclude. config.php');
217+
}
218+
public function testExcludeDefault(): void {
219+
$opt = [
220+
];
221+
222+
$options = new Options($opt);
223+
224+
$this->assertSame(0, count($options->excludes()), 'default exclude is empty.');
225+
}
166226

167227
}

0 commit comments

Comments
 (0)