Doctrine Common
  1. Doctrine Common
  2. DCOM-233

Support for class constant annotations

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Annotations
    • Labels:
      None

      Description

      I need to read annotations for class constants which currently is not possible. Obviously there is a good reason for that because PHP lacks a native ReflectionConstant class. Still sometimes this would be really useful and there are custom reflection implementation out there such as the PHP-Token-Reflection library (https://github.com/Andrewsville/PHP-Token-Reflection) which provide a class for that purpose.
      Now the problem is that there is no way to extend the annotation reader to add support for this purpose without having to duplicate a lot of code. This is due to the annotation parser being private in the annotation reader and Doctrine not providing a Target::TARGET_CONSTANT.
      If we could decide on supporting this, there are two options IMO:

      1. Provide a ReflectionConstant interface in the core and add getConstantAnnotation(ReflectionConstant $constant, $annotationName) and getConstantAnnotations(ReflectionConstant $constant) to the Reader interface in conjunction with an AbstractReader class to throw an exception when calling those methods (to preserve BC).

      2. Extend AnnotationReader to provide a final protected getParser() method for better extensibility so that one can subclass the annotation reader without heavy code duplication.

      In both cases an addition of Target::TARGET_CONSTANT would desirable.

      Currently I have to workaround this is in a very ugly manner by extending \ReflectionClass to access ReflectionConstant objects provided by PHP-Token-Reflection library and wrapping them into objects that emulate \ReflectionProperty, just to be able to use the annotation reader.

      Do you think there is any solution to this which we can provide in Doctrine annotations lib?

        Activity

        Hide
        Marco Pivetta added a comment -

        Guilherme Blanco was thinking of marking this as "won't fix". A constant, after all, is a constant, and it can be accessed no matter what.

        Steve Müller can you clarify on the use-case? This is too much work for too little benefit IMO...

        Show
        Marco Pivetta added a comment - Guilherme Blanco was thinking of marking this as "won't fix". A constant, after all, is a constant, and it can be accessed no matter what. Steve Müller can you clarify on the use-case? This is too much work for too little benefit IMO...
        Hide
        Steve Müller added a comment -

        Marco Pivetta This is not about access but about being able to put metadata on class constants. My exact use-case is an Enum implementation through class constants where I can put things like readable values, ordinal numbers and such on each enum constants. I haven't thought of any other special use-cases and it might not be as common to be able to enrich class constants with metadata but still it would perfectly fit the annotation reader.
        I see that this might cause too much work so if you decide on a "won't fix" I'll be fine with that.

        Show
        Steve Müller added a comment - Marco Pivetta This is not about access but about being able to put metadata on class constants. My exact use-case is an Enum implementation through class constants where I can put things like readable values, ordinal numbers and such on each enum constants. I haven't thought of any other special use-cases and it might not be as common to be able to enrich class constants with metadata but still it would perfectly fit the annotation reader. I see that this might cause too much work so if you decide on a "won't fix" I'll be fine with that.
        Hide
        Christophe Coevoet added a comment -

        Given that PHP does not expose the docComment of constants, I would vote for marking it as "won't fix", as it would force us to require a separate parsing of the code to find the docComment

        Show
        Christophe Coevoet added a comment - Given that PHP does not expose the docComment of constants, I would vote for marking it as "won't fix", as it would force us to require a separate parsing of the code to find the docComment
        Hide
        Guilherme Blanco added a comment -

        No magic please... PHP is guilty here. We should propose docComment support there, then we add this support here.

        Show
        Guilherme Blanco added a comment - No magic please... PHP is guilty here. We should propose docComment support there, then we add this support here.

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Steve Müller
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: