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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add generics to ArgumentMetadata::getAttributes
  • Loading branch information
Seldaek committed Mar 16, 2022
commit aad19dd45aede3b1321e76511cf968bfc33ba709
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,30 @@ public function getDefaultValue(): mixed
}

/**
* @return object[]
* @param class-string $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));
}
}