Skip to content

Commit

Permalink
Fix promoted properties sharing context (#1619)
Browse files Browse the repository at this point in the history
Co-authored-by: Rasmus Christoffer Nielsen <rniels@norlys.dk>
  • Loading branch information
rasmuscnielsen and Rasmus Christoffer Nielsen authored Jul 3, 2024
1 parent 7bd9029 commit 4756339
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function build(\Reflector $reflector, Context $context): array
$instance->nullable = $nullable ?: Generator::UNDEFINED;

if ($rp->isPromoted()) {
// ensure each property has its own context
$instance->_context = new Context([], $instance->_context);

// promoted parameter - docblock is available via class/property
if ($comment = $rp->getDeclaringClass()->getProperty($rp->getName())->getDocComment()) {
$instance->_context->comment = $comment;
Expand Down
15 changes: 12 additions & 3 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,17 @@ public function testPhp8PromotedProperties(): void

/** @var OA\Property[] $properties */
$properties = $analysis->getAnnotationsOfType(OA\Property::class);
$this->assertCount(2, $properties);
$this->assertEquals('id', $properties[0]->property);
$this->assertEquals('labels', $properties[1]->property);

[$tags, $id, $labels] = $properties;

$this->assertCount(3, $properties);
$this->assertEquals('tags', $tags->property);
$this->assertEquals('id', $id->property);
$this->assertEquals('labels', $labels->property);

// regression: check doc blocks
$this->assertStringContainsString('Label List', $labels->_context->comment);
$this->assertStringContainsString('Tag List', $tags->_context->comment);
$this->assertEmpty($id->_context->comment);
}
}
2 changes: 1 addition & 1 deletion tests/Analysers/TokenScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static function scanCases(): iterable
'traits' => [],
'enums' => [],
'methods' => ['__construct'],
'properties' => ['labels', 'id'],
'properties' => ['labels', 'tags', 'id'],
],
],
];
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/PHP/Php8PromotedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function __construct(
* @OA\Property()
*/
public ?array $labels,
/**
* Tag List.
*
* @var array<int,string>
*/
#[OAT\Property()]
public array $tags,
#[OAT\Property()]
public int $id,
) {
Expand Down

0 comments on commit 4756339

Please sign in to comment.