Skip to content

Commit 12221dc

Browse files
authored
Merge pull request #39 from smeghead/support-enum
support enum.
2 parents 11e1205 + 0fce594 commit 12221dc

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# CHANGELOG
22

3+
### Features
4+
5+
* support enum.
6+
37
### Bug fix
48

5-
* Fixed an error when loading traits.
9+
* Fixed an error when loading traits. #37
610

711

812
## v0.3.0 (2023-02-23)

src/DiagramElement/Entry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function getClass(): PhpClass {
3434
public function dump($level = 0): array {
3535
$indent = str_repeat(' ', $level);
3636
$lines = [];
37-
$meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class';
37+
// $meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class';
38+
$meta = $this->class->getClassType()->getMetaName();
3839
if ($this->options->classProperties() || $this->options->classMethods()) {
3940
$lines[] = sprintf('%s%s %s {', $indent, $meta, $this->class->getLogicalName());
4041
if ($this->options->classProperties()) {

src/Php/PhpType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public function getMeta(): string {
2424
return $this->meta;
2525
}
2626

27+
public function getMetaName(): string {
28+
switch ($this->meta) {
29+
case 'Stmt_Interface':
30+
return 'interface';
31+
case 'Stmt_Enum':
32+
return 'enum';
33+
case 'Stmt_Trait':
34+
return 'class';
35+
default:
36+
return 'class';
37+
}
38+
}
39+
2740
/**
2841
* @return string[] namespace
2942
*/

test/PhpReflectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,18 @@ public function testTrait(): void {
298298
$this->assertSame('string', $data->getMethods()[0]->getParams()[0]->getType()->getName(), 'parameter type.');
299299
$this->assertSame([], $data->getMethods()[0]->getParams()[0]->getType()->getTypes()[0]->getNamespace(), 'parameter type namespace.');
300300
}
301+
/**
302+
* @requires PHP >= 8.1
303+
* PHP8.0 dose not have `enum`.
304+
*/
305+
public function testEnum(): void {
306+
$options = new Options([]);
307+
$directory = sprintf('%s/enum', $this->fixtureDir);
308+
$filename = sprintf('%s/enum/TestEnum.php', $this->fixtureDir);
309+
$parsed = PhpReader::parseFile($directory, $filename, $options);
310+
311+
$data = $parsed[0]->getInfo();
312+
$this->assertSame('Suit', $data->getClassType()->getName(), '1st class type name.');
313+
$this->assertSame(['Hoge', 'TestEnum'], $data->getClassType()->getNamespace(), '1st namespace name.');
314+
}
301315
}

test/fixtures/enum/TestEnum.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Hoge\TestEnum;
3+
4+
enum Suit
5+
{
6+
case Hearts;
7+
case Diamonds;
8+
case Clubs;
9+
case Spades;
10+
}
11+
12+
function do_stuff(Suit $s)
13+
{
14+
// ...
15+
}
16+
17+
do_stuff(Suit::Spades);

0 commit comments

Comments
 (0)