Skip to content

Commit

Permalink
Add generics to ArgumentMetadata::getAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 16, 2022
1 parent 8e8207b commit 2a0de9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,27 @@ public function getDefaultValue(): mixed
}

/**
* @return object[]
* @param class-string<T> $name
* @param self::IS_INSTANCEOF|0 $flags
* @return array<object>
*/
public function getAttributes(string $name = null, int $flags = 0): array
{
if (!$name) {
return $this->attributes;
}

return $this->getAttributesOfType($name, $flags);
}

/**
* @template T of object
* @param class-string<T> $name
* @param self::IS_INSTANCEOF|0 $flags
* @return array<T>
*/
public function getAttributesOfType(string $name, int $flags = 0): array
{
$attributes = [];
if ($flags & self::IS_INSTANCEOF) {
foreach ($this->attributes as $attribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public function testGetAttributes()
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
$this->assertEquals([new Foo('bar')], $argument->getAttributes());
}

public function testGetAttributesOfType()
{
$argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
$this->assertEquals([new Foo('bar')], $argument->getAttributesOfType(Foo::class));
}
}

0 comments on commit 2a0de9d

Please sign in to comment.