Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6be0d9

Browse files
committed
feature #45094 Add generics to ArgumentMetadata::getAttributes (Seldaek)
This PR was merged into the 6.1 branch. Discussion ---------- Add generics to ArgumentMetadata::getAttributes | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This is a bit of an unfortunate situation because the function does too many things. If stronger typing was applied earlier on, it would have been evident that typing this correctly is pretty much impossible, and that would have helped guide the design to use a separate function to retrieve all attributes and some attributes. e.g. the ideal outcome would be for `getAttributes(Foo::class)` to be typed as such: ``` /** * @template T * @param class-string<T> $name * @return array<T> */ ``` So that the result is guaranteed to be an array of Foo objects. The fact that $name is nullable throws all this off though.. So I guess the only good way forward would be to deprecate null, or deprecate passing `$name` and add a `getAttributesByClassName(string $class, int $flags = 0)` ? I'm not sure if merging this as is is valuable or not, or if you'd rather get the deprecation and new method targetting 6.1? Commits ------- aad19dd Add generics to ArgumentMetadata::getAttributes
2 parents b9eace8 + aad19dd commit d6be0d9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

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

103103
/**
104-
* @return object[]
104+
* @param class-string $name
105+
* @param self::IS_INSTANCEOF|0 $flags
106+
*
107+
* @return array<object>
105108
*/
106109
public function getAttributes(string $name = null, int $flags = 0): array
107110
{
108111
if (!$name) {
109112
return $this->attributes;
110113
}
111114

115+
return $this->getAttributesOfType($name, $flags);
116+
}
117+
118+
/**
119+
* @template T of object
120+
*
121+
* @param class-string<T> $name
122+
* @param self::IS_INSTANCEOF|0 $flags
123+
*
124+
* @return array<T>
125+
*/
126+
public function getAttributesOfType(string $name, int $flags = 0): array
127+
{
112128
$attributes = [];
113129
if ($flags & self::IS_INSTANCEOF) {
114130
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)