Skip to content

Commit badcf56

Browse files
committed
Fixed an error when loading traits.
1 parent e25c93e commit badcf56

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
### Bug fix
4+
5+
* Fixed an error when loading traits.
6+
7+
38
## v0.3.0 (2023-02-23)
49

510
### Features

src/Php/PhpClass.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@
1010
use PhpParser\Node\Name\FullyQualified;
1111
use PhpParser\Node\Stmt;
1212
use PhpParser\Node\Stmt\ {
13-
Class_,
1413
Namespace_,
1514
ClassLike,
1615
ClassMethod,
1716
Property,
1817
GroupUse,
19-
Interface_,
2018
Use_,
2119
};
2220

2321
class PhpClass {
2422
/** @var string[] directory parts */
2523
protected array $dirs;
26-
protected Class_|Interface_ $syntax;
24+
protected ClassLike $syntax;
2725
protected array $full;
2826

2927
public function __construct(string $filename, Stmt $syntax, array $full) {

test/PhpReflectionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,19 @@ public function testClassesInAFile(): void {
283283
$this->assertSame('Price', $data->getClassType()->getName(), '3rd class type name.');
284284
$this->assertSame(['hoge', 'fuga', 'product'], $data->getClassType()->getNamespace(), '3rd namespace name.');
285285
}
286+
public function testTrait(): void {
287+
$options = new Options([]);
288+
$directory = sprintf('%s/trait', $this->fixtureDir);
289+
$filename = sprintf('%s/trait/TestTrait.php', $this->fixtureDir);
290+
$parsed = PhpReader::parseFile($directory, $filename, $options);
291+
292+
$data = $parsed[0]->getInfo();
293+
$this->assertSame('TestTrait', $data->getClassType()->getName(), '1st class type name.');
294+
$this->assertSame(['Foo', 'Traits'], $data->getClassType()->getNamespace(), '1st namespace name.');
295+
$this->assertSame([], $data->getMethods()[0]->getType()->getTypes()[0]->getNamespace(), 'return type namespace.');
296+
$this->assertSame('bool', $data->getMethods()[0]->getType()->getTypes()[0]->getName(), 'return type name.');
297+
$this->assertSame('name', $data->getMethods()[0]->getParams()[0]->getName(), 'parameter name.');
298+
$this->assertSame('string', $data->getMethods()[0]->getParams()[0]->getType()->getName(), 'parameter type.');
299+
$this->assertSame([], $data->getMethods()[0]->getParams()[0]->getType()->getTypes()[0]->getNamespace(), 'parameter type namespace.');
300+
}
286301
}

test/fixtures/trait/TestTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Foo\Traits;
4+
5+
trait TestTrait
6+
{
7+
private function something(string $name): bool
8+
{
9+
return false;
10+
}
11+
}
12+

0 commit comments

Comments
 (0)