8000 Add generics to ArgumentMetadata::getAttributes · symfony/symfony@7475c24 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7475c24

Browse files
committed
Add generics to ArgumentMetadata::getAttributes
1 parent 8e8207b commit 7475c24

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadata.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,27 @@ public function getDefaultValue(): mixed
101101
}
102102

103103
/**
104-
* @return object[]
104+
* @param class-string $name
105+
* @param self::IS_INSTANCEOF|0 $flags
106+
* @return array<object>
105107
*/
106108
public function getAttributes(string $name = null, int $flags = 0): array
107109
{
108110
if (!$name) {
109111
return $this->attributes;
110112
}
111113

114+
return $this->getAttributesOfType($name, $flags);
115+
}
116+
117+
/**
118+
* @template T of object
119+
* @param class-string<T> $name
120+
* @param self::IS_INSTANCEOF|0 $flags
121+
* @return array<T>
122+
*/
123+
public function getAttributesOfType(string $name, int $flags = 0): array
124+
{
112125
$attributes = [];
113126
if ($flags & self::IS_INSTANCEOF) {
114127
foreach ($this->attributes as $attribute) {

src/Symfony/Component/HttpKernel/Tests/ControllerMetadata/ArgumentMetadataTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ public function testGetAttributes()
4848
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
4949
$this->assertEquals([new Foo('bar')], $argument->getAttributes());
5050
}
51+
52+
public function testGetAttributesOfType()
53+
{
54+
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
55+
$this->assertEquals([new Foo('bar')], $argument->getAttributesOfType(Foo::class));
56+
}
5157
}

0 commit comments

Comments
 (0)
0