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

AttributeReader reads attributes from class traits #2835

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ a release.
---

## [Unreleased]
### Added
- AttributeReader reads attributes from class traits

## [3.16.1]
### Fixed
Expand Down
20 changes: 19 additions & 1 deletion src/Mapping/Driver/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class AttributeReader
*/
public function getClassAnnotations(\ReflectionClass $class): array
{
return $this->convertToAttributeInstances($class->getAttributes());
return $this->convertToAttributeInstances($this->getClassAndTraitsAttributes($class));
}

/**
Expand Down Expand Up @@ -94,6 +94,24 @@ private function convertToAttributeInstances(iterable $attributes): array
return $instances;
}

/**
* Get attributes from a class including those from traits.
*
* @phpstan-param \ReflectionClass<object> $class
*
* @return \ReflectionAttribute<object>[]
*/
private function getClassAndTraitsAttributes(\ReflectionClass $class): array
{
$traitAttributes = array_reduce(
$class->getTraits(),
static fn (array $carry, \ReflectionClass $trait) => array_merge($trait->getAttributes(), $carry),
[]
);

return array_merge($class->getAttributes(), $traitAttributes);
}

private function isRepeatable(string $attributeClassName): bool
{
if (isset($this->isRepeatableAttribute[$attributeClassName])) {
Expand Down