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

ReflectionEnumProperty throws error on AttributeReader::getPropertyAnnotations() #9380

Closed
ezkimo opened this issue Jan 14, 2022 · 0 comments · Fixed by #9381
Closed

ReflectionEnumProperty throws error on AttributeReader::getPropertyAnnotations() #9380

ezkimo opened this issue Jan 14, 2022 · 0 comments · Fixed by #9381
Assignees
Labels
Milestone

Comments

@ezkimo
Copy link

ezkimo commented Jan 14, 2022

Bug Report

Q A
BC Break no
Version 2.11.0

Summary

An Error "Internal error: Failed to retrieve the reflection object" is thrown when calling AttributeReader::getPropertyAnotations because the inherited function ReflectionProperty::getAttributes() is not called on the original reflection property in the ReflectionEnumProperty class.
In my specific case I 'm using custom annotations I have to read for custom behaviour. When trying to retrieve the custom annotations the error occurs.

Current behavior

When trying to fetch all or a specific annotation of an entity property, the call fails with an error "Internal error: Failed to retrieve the reflection object".

How to reproduce

enum Suit: string {
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

#[Entity]
class Card
{
    /** ... */

    #[Column(type: 'string', enumType: Suit::class)]
    #[AnyCustomAnnotation(param: 'bla')]
    public $suit;
}

/** ... */

// @var \Doctrine\ORM\EntityManager
$metadata = $objectManager->getClassMetadata(Card::class);

foreach ($metadata->fieldMappings as $field => $mapping) {
    $property = $metadata->getReflectionProperty($field);
    // @var Doctrine\ORM\Mapping\Driver\AttributeReader
    $isCustom = $reader->getPropertyAnnotation($property, AnyCustomAnnotation::class);
    /** ... */
}

Expected behavior

The expected behaviour should return the property attributes like ReflectionProperty::getAttributes() is supposed to. A possible solution could be extending the ReflectionEnumProperty class with an inherited getAttributes method, that will return the attributes from the original reflection property.

class ReflectionEnumProperty extends ReflectionProperty
{
    ...
    /**
     * {@inheritDoc}
     */
     public function getAttributes(?string $name = null, int $flags = 0): array
     {
         return $this->originalReflectionProperty->getAttributes();
     }
}
@beberlei beberlei self-assigned this Jan 14, 2022
beberlei added a commit to beberlei/doctrine2 that referenced this issue Jan 14, 2022
beberlei added a commit that referenced this issue Jan 15, 2022
…9381)

* [GH-9380] Bugfix: Delegate ReflectionEnumProperty::getAttributes().

* [GH-9380] Add test for retrieving attributes via enum property.

* [GH-9380] Add test for retrieving attributes via enum property.

* [GH-9380] Call parent ReflectionProperty ctor for best behavior.

* Update tests/Doctrine/Tests/ORM/Functional/EnumTest.php

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
@beberlei beberlei added this to the 2.11.1 milestone Jan 16, 2022
derrabus added a commit to derrabus/orm that referenced this issue Jan 17, 2022
* 2.12.x:
  Allow using Enum from different namespace than Entity (doctrine#9384)
  Corrected ORM version and added missing dependency (doctrine#9386)
  PHPStan 1.4.0 (doctrine#9385)
  [doctrineGH-9380] Bugfix: Delegate ReflectionEnumProperty::getAttributes(). (doctrine#9381)
  Support enum cases as parameters (doctrine#9373)
  Add detach as of list cascade-all operations (doctrine#9357)
@derrabus derrabus added the Bug label Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants