Skip to content

Commit

Permalink
Merge pull request #883 from kukulich/promoted
Browse files Browse the repository at this point in the history
Support promoted properties docblocks and attributes
  • Loading branch information
Ocramius authored Nov 28, 2021
2 parents a37bda7 + 416ac1c commit 99c472c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,9 @@ public function getImmediateProperties(?int $filter = null): array
$propertyNode = new Node\Stmt\Property(
$parameterNode->flags,
[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
2 changes: 1 addition & 1 deletion test/unit/Fixture/ExampleClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ExampleClass

public static $publicStaticProperty;

public function __construct(private ?int $promotedProperty = 123, $noPromotedProperty = null)
public function __construct(/** Some doccomment */ private ?int $promotedProperty = 123, $noPromotedProperty = null)
{
}

Expand Down
36 changes: 29 additions & 7 deletions test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ public function testIsPromoted(): void
self::assertTrue($promotedProperty->hasType());
self::assertSame('?int', $promotedProperty->getType()->__toString());
self::assertTrue($promotedProperty->hasDefaultValue());
self:self::assertSame(123, $promotedProperty->getDefaultValue());
self::assertSame(123, $promotedProperty->getDefaultValue());
self::assertSame(46, $promotedProperty->getStartLine());
self::assertSame(46, $promotedProperty->getEndLine());
self::assertSame(60, $promotedProperty->getStartColumn());
self::assertSame(95, $promotedProperty->getEndColumn());
self::assertSame('/** Some doccomment */', $promotedProperty->getDocComment());
}

public function testIsDefault(): void
Expand Down Expand Up @@ -867,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 99c472c

Please sign in to comment.