Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promoted properties sharing context #1619

Merged
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
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