Skip to content

Commit

Permalink
Promoted properties should copy parameter attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 28, 2021
1 parent b10e903 commit 416ac1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ public function getImmediateProperties(?int $filter = null): array
[new Node\Stmt\PropertyProperty($parameterNameNode->name, $parameterNode->default)],
$parameterNode->getAttributes(),
$parameterNode->type,
$parameterNode->attrGroups,
);
$property = ReflectionProperty::createFromNode(
$this->reflector,
Expand Down
8 changes: 8 additions & 0 deletions test/unit/Fixture/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function methodWithAttributes(
{

}

public function __construct(
#[Attr]
#[AnotherAttr]
private $promotedPropertyWithAttributes
)
{
}
}

#[Attr]
Expand Down
29 changes: 23 additions & 6 deletions test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,31 +872,48 @@ public function testGetAttributesWithoutAttributes(): void
self::assertCount(0, $attributes);
}

public function testGetAttributesWithAttributes(): void
public function dataGetAttributes(): array
{
return [
['propertyWithAttributes'],
['promotedPropertyWithAttributes'],
];
}

/**
* @dataProvider dataGetAttributes
*/
public function testGetAttributesWithAttributes(string $propertyName): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Attributes.php', $this->astLocator));
$classReflection = $reflector->reflectClass(ClassWithAttributes::class);
$propertyReflection = $classReflection->getProperty('propertyWithAttributes');
$propertyReflection = $classReflection->getProperty($propertyName);
$attributes = $propertyReflection->getAttributes();

self::assertCount(2, $attributes);
}

public function testGetAttributesByName(): void
/**
* @dataProvider dataGetAttributes
*/
public function testGetAttributesByName(string $propertyName): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Attributes.php', $this->astLocator));
$classReflection = $reflector->reflectClass(ClassWithAttributes::class);
$propertyReflection = $classReflection->getProperty('propertyWithAttributes');
$propertyReflection = $classReflection->getProperty($propertyName);
$attributes = $propertyReflection->getAttributesByName(Attr::class);

self::assertCount(1, $attributes);
}

public function testGetAttributesByInstance(): void
/**
* @dataProvider dataGetAttributes
*/
public function testGetAttributesByInstance(string $propertyName): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Attributes.php', $this->astLocator));
$classReflection = $reflector->reflectClass(ClassWithAttributes::class);
$propertyReflection = $classReflection->getProperty('propertyWithAttributes');
$propertyReflection = $classReflection->getProperty($propertyName);
$attributes = $propertyReflection->getAttributesByInstance(Attr::class);

self::assertCount(2, $attributes);
Expand Down

0 comments on commit 416ac1c

Please sign in to comment.