Skip to content

Commit 5c1556b

Browse files
author
Alxey Shapilov
committed
[feature/interface-attributes] Added read attributes on interface
1 parent aa45e59 commit 5c1556b

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/Internal/AttributeReader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function getClassMetadata(\ReflectionClass $class, string $name = null):
3535
yield $this->instantiator->instantiate($attribute, $arguments, $class);
3636
}
3737

38+
foreach ($class->getInterfaces() as $interface) {
39+
yield from $this->getClassMetadata($interface, $name);
40+
}
41+
3842
foreach ($class->getTraits() as $trait) {
3943
yield from $this->getClassMetadata($trait, $name);
4044
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Spiral Framework package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Spiral\Tests\Attributes\Reader\Fixture;
13+
14+
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\ClassAnnotation;
15+
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodAnnotation;
16+
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodParameterAnnotation;
17+
18+
/** @ClassAnnotation(field="value") */
19+
#[ClassAnnotation(field: 'value')]
20+
interface AnnotatedInterface
21+
{
22+
#[MethodAnnotation(field: 'value')]
23+
public function method(
24+
#[MethodParameterAnnotation(field: 'value')]
25+
string $parameter
26+
): void;
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Spiral Framework package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Spiral\Tests\Attributes\Reader\Fixture;
13+
14+
class ClassWithAnnotatedInterface implements AnnotatedInterface
15+
{
16+
public function method(string $parameter): void
17+
{
18+
}
19+
}

tests/Reader/ReaderTestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodAnnotation;
2222
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodParameterAnnotation;
2323
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\PropertyAnnotation;
24+
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedInterface;
2425
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedTrait;
2526
use Spiral\Tests\Attributes\Reader\Fixture\UndefinedMeta;
2627
use Spiral\Tests\Attributes\TestCase;
@@ -99,6 +100,10 @@ public function testClassMetadataCount(): void
99100
$this->assertCount($this->classMetadataCount,
100101
$this->getClassMetadata(ClassWithAnnotatedTrait::class)
101102
);
103+
104+
$this->assertCount($this->classMetadataCount,
105+
$this->getClassMetadata(ClassWithAnnotatedInterface::class)
106+
);
102107
}
103108

104109
public function testClassMetadataObjects(): void
@@ -118,6 +123,10 @@ public function testClassMetadataObjects(): void
118123
foreach ($this->getClassMetadata(ClassWithAnnotatedTrait::class) as $actual) {
119124
$this->assertEquals($expected, $actual);
120125
}
126+
127+
foreach ($this->getClassMetadata(ClassWithAnnotatedInterface::class) as $actual) {
128+
$this->assertEquals($expected, $actual);
129+
}
121130
}
122131

123132
public function testConstantMetadataCount(): void
@@ -169,6 +178,10 @@ public function testMethodMetadataCount(): void
169178
$this->assertCount($this->methodMetadataCount,
170179
$this->getMethodMetadata(AnnotatedClass::class, 'method')
171180
);
181+
182+
$this->assertCount($this->methodMetadataCount,
183+
$this->getMethodMetadata(ClassWithAnnotatedInterface::class, 'method')
184+
);
172185
}
173186

174187
public function testMethodMetadataObjects(): void
@@ -191,6 +204,10 @@ public function testMethodParameterMetadataCount(): void
191204
$this->assertCount($this->methodParameterMetadataCount,
192205
$this->getMethodParameterMetadata(AnnotatedClass::class, 'method', 'parameter')
193206
);
207+
208+
$this->assertCount($this->methodParameterMetadataCount,
209+
$this->getMethodParameterMetadata(ClassWithAnnotatedInterface::class, 'method', 'parameter')
210+
);
194211
}
195212

196213
public function testMethodParameterMetadataObjects(): void

0 commit comments

Comments
 (0)