Skip to content

Problem with "inherited=true" + "multiple=false" annotations #71

Open

Description

@aik099

According to https://github.com/php-annotations/php-annotations/wiki/CustomAnnotations only of annotations are allowed at a time.

Example code:

/**
 * @usage('class' => true, 'inherited' => true)
 */
class OneAnnotation extends Annotation
{

}

/**
 * @one
 * @one
 * @one
 */
class ParentClass {

}

/**
 * @one
 * @one
 */
class SubClass extends ParentClass {

}

Expected behavior:

  1. reading annotations from ParentClass will:
  2. reading annotations from SubClass will:
    • discover 5 annotations (3 inherited and 2 defined on inspected class)
    • remove 3 inherited annotations because only single annotation instance allowed
  3. result in exception (
    throw new AnnotationException("only one annotation of type {$type} may be applied to the same {$member}");
    ) because only 1 is allowed

What happens instead:

  1. no exception and I get single @one annotation back
  2. no exception and I get single @one annotation back

Why this happens:

List of annotations doesn't remember from which class each annotation came or at least was is defined explicitly on that class or was inherited from one of a parent classes. Without that knowledge

unset($annotations[$inner]);
code is removing all but last annotation. I think that code should only remove annotations are are in fact inherited from parent classes only and keep ones defined explicitly to catch that warning.

Activity

  1. mindplay-dk commented on Aug 21, 2014

    @mindplay-dk
    Member

    There was already a test-case for this which ought to fail?

  2. aik099 commented on Aug 21, 2014

    @aik099
    MemberAuthor

    That's the problem. Library makes no difference between inherited and not inherited annotations and removing all but last making test pass.

    This way if have 2+ same annotations on a class/property/method then you'll get 1 back instead of getting exception that couldn't use more then 1.

    The failing test case to prove this is here: #73

  3. added this to the 1.2.0 milestone on Aug 24, 2014
  4. aik099 commented on Aug 24, 2014

    @aik099
    MemberAuthor

    @mindplay-dk do you see where the problem is now? Any ideas on fixing?

  5. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    Looks like a bug in the test-framework? I wasn't able to pinpoint it. The error doesn't appear to be coming from the test-suite, and it appears proper checks for precisely one usage-annotation are in place.

  6. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    Or maybe not, here's a stack trace:

    \test\lib\xTestException: Undefined offset: 0 in C:\workspace\test\php-annotations\test\lib\xTestRunner.php on line 146
    
    Call Stack:
        0.0902     130488   1. {main}() C:\workspace\test\php-annotations\test\test.php:0
        0.1278     443176   2. mindplay\test\lib\xTestRunner->run(string(44), string(9)) C:\workspace\test\php-annotations\test\test.php:24
        0.1445     731912   3. mindplay\test\lib\xTest->run(class mindplay\test\lib\xTestRunner) C:\workspace\test\php-annotations\test\lib\xTestRunner.php:129
        0.2477     969832   4. AnnotationsTest->testAnnotationWithMultipleUsageAnnotations() C:\workspace\test\php-annotations\test\lib\xTest.php:110
       52.5464     978376   5. mindplay\annotations\Annotations::getUsage(string(20)) C:\workspace\test\php-annotations\test\suite\Annotations.test.php:191
       52.5466     978376   6. mindplay\annotations\AnnotationManager->getUsage(string(20)) C:\workspace\test\php-annotations\src\annotations\Annotations.php:60
       52.5473     979608   7. mindplay\test\lib\xTestRunner->handlePHPErrors(long, string(19), string(71), long, array(2)) C:\workspace\test\php-annotations\src\annotations\Annotations.php:419
    

    Could this have something to do with the previous problem of non-sequential indices in the returned list of annotations, when overriden annotations were previously removed using unset, before you fixed that?

  7. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    What I mean is, if the second usage-annotations replaces the first using the "old" approach, it will likely unset index 0, leaving only an item with index 1 for the second usage-annotation - since it's hard-coded to pick up the annotation with index 0, that'll fail.

    Try merging the fix for sequential indices and see if the error still occurs?

  8. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    Even if that fixes this issue, there is still something wonky though... there's a check and an exception missing for the case where two annotations with $single = true usage are picked up from the same member - that needs to be added either way, and perhaps a test-case to prove that this is a general problem, to avoid future regressions.

  9. aik099 commented on Aug 24, 2014

    @aik099
    MemberAuthor

    Or maybe not, here's a stack trace:

    This is my version of convertor for each warning/notice that happens into exception. The stack trace represents the path from my exception handler inside test suite, but not the original stack trace of notice/warning, that was wrapped within the exception.

    Could this have something to do with the previous problem of non-sequential indices in the returned list of annotations, when overriden annotations were previously removed using unset, before you fixed that?

    Yes, you are correct.

    Try merging the fix for sequential indices and see if the error still occurs?

    Nope. Another one does, but that is due bug in test suite framework, where with 1 expected exception 0 are thrown and that marks test as incomplete instead of failed.

    Even if that fixes this issue, there is still something wonky though... there's a check and an exception missing for the case where two annotations with $single = true usage are picked up from the same member - that needs to be added either way, and perhaps a test-case to prove that this is a general problem, to avoid future regressions.

    Yes 2 tests are needed:

    1. class, that has 2 annotations of "inherited=true" + "multiple=false": we should get an exception that only 1 annotation is allowed
    2. class that extends class from above and has only 1 such annotation: we should get no exception from inspected class, but we might get one when reading annotations from parent class
  10. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    Take a look at this branch - attempting to scan forward to apply the inheritance rule instead, I think this may be the way forward... have a look at the updated getAnnotations and applyConstraints methods.

  11. aik099 commented on Aug 24, 2014

    @aik099
    MemberAuthor

    I see change that we talked about in the getAnnotations method: annotations are now collected into local $annotations variable and only then assigned into $this->annotations array (this can give a performance boost because on each found annotation we don't need to access array key again).

    Also in applyConstraints method I see that you now return annotations. But I don't quite get the change in scanning algorithm.

  12. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    But I don't quite get the change in scanning algorithm.

    It's simple - when I find an annotation with usage $multiple=false and $inherited=true, I look through the remaining annotations to see if it's going to get overriden - if so, I don't add it in the first place.

    As opposed to the previous approach: looking backwards to see if an annotation should have been overriden.

    Make sense? :-)

  13. aik099 commented on Aug 24, 2014

    @aik099
    MemberAuthor

    Don't add if there will be more in contrast to remove already added if there will be more. Makes sense if I put it this way.

  14. mindplay-dk commented on Aug 24, 2014

    @mindplay-dk
    Member

    Something like that :-)

  15. aik099 commented on Aug 27, 2014

    @aik099
    MemberAuthor

    I've moved out scan forward changes made in #73 into separate #82 PR and merged it.

  16. mindplay-dk commented on Aug 27, 2014

    @mindplay-dk
    Member

    Looking good 👍

  17. aik099 commented on Aug 27, 2014

    @aik099
    MemberAuthor

    I wonder how originally described problem can be solved. Right now the getAnnotations method doesn't keep class name in which annotation was used along with annotations. Because of this in the applyConstrains all annotations looks similar.

    There are several ways of solving:

    Way 1

    1. introduce className property on the annotation (problem is that we can't enforce this through the IAnnotation interface)
    2. when annotation is discovered on a property/method/class we put owner class into ->className property
    3. given class name being scanned in getAnnotations to the applyConstraints method
    4. make applyConstraints remove only inherited annotations (ones where ->className of annotation doesn't match to the class given to the applyConstraints method itself)

    Way 2

    1. instead of storing annotation directly in the annotations array we store array($annotation, $className) instead
    2. when annotation is discovered on a property/method/class we put owner class into $className variable
    3. given class name being scanned in getAnnotations to the applyConstraints method
    4. make applyConstraints remove only inherited annotations (ones where $className of annotation doesn't match to the class given to the applyConstraints method itself)
    5. somehow filter out $className from the annotation array to return array in expected format from the getAnnotations method

    The Way 1 might be easier, but annotation doesn't really care on which class it was defined. That's why Way 2 is there.

    Since the getAnnotations is called recursively for all parent classes the applyConstrains will only see inherited annotations from direct super-class. Maybe this can help in simplifying the way of removing inherited annotations.

  18. mindplay-dk commented on Aug 27, 2014

    @mindplay-dk
    Member

    I think it's probably a much simpler matter of just checking the annotations being picked up from a single class (before picking up annotations from parent classes) to make sure that those with $multiple = false occur only once.

    I'm unclear as to why applyConstraints() isn't catching the multiple usage-annotations in the test-case - applyConstraints() is being called after all of the annotations have been collected into a flat list, so why is it not detecting the two usage-annotations and throwing an error?

  19. aik099 commented on Aug 27, 2014

    @aik099
    MemberAuthor

    I'm unclear as to why applyConstraints() isn't catching the multiple usage-annotations in the test-case - applyConstraints() is being called after all of the annotations have been collected into a flat list, so why is it not detecting the two usage-annotations and throwing an error?

    That I know why: there are 2 usage annotations and same class and only 1 usage is allowed the 1st usage is discarded (was unset before) and only 2nd usage is kept. Since 1st usage is discarded without consulting if annotation is inherited from parent class or not we don't get any exception back.

  20. aik099 commented on Aug 27, 2014

    @aik099
    MemberAuthor

    I think it's probably a much simpler matter of just checking the annotations being picked up from a single class (before picking up annotations from parent classes) to make sure that those with $multiple = false occur only once.

    The getAnnotations knows what is inherited from what, but we remove annotations in applyConstraints, which doesn't know anything of that.

  21. self-assigned this
    on Aug 27, 2014
  22. modified the milestones: 1.2.0, 1.3.0 on Jun 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions