Repository navigation
Open
Description
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:
- reading annotations from
ParentClasswill:- discover 3 annotations
- result in exception () because only 1 is allowed
throw new AnnotationException("only one annotation of type {$type} may be applied to the same {$member}");
- reading annotations from
SubClasswill:- discover 5 annotations (3 inherited and 2 defined on inspected class)
- remove 3 inherited annotations because only single annotation instance allowed
- result in exception () because only 1 is allowed
throw new AnnotationException("only one annotation of type {$type} may be applied to the same {$member}");
What happens instead:
- no exception and I get single
@oneannotation back - no exception and I get single
@oneannotation 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]); |
Activity
mindplay-dk commentedon Aug 21, 2014
There was already a test-case for this which ought to fail?
aik099 commentedon Aug 21, 2014
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
aik099 commentedon Aug 24, 2014
@mindplay-dk do you see where the problem is now? Any ideas on fixing?
mindplay-dk commentedon Aug 24, 2014
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.
mindplay-dk commentedon Aug 24, 2014
Or maybe not, here's a stack trace:
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?mindplay-dk commentedon Aug 24, 2014
What I mean is, if the second usage-annotations replaces the first using the "old" approach, it will likely
unsetindex0, leaving only an item with index1for the second usage-annotation - since it's hard-coded to pick up the annotation with index0, that'll fail.Try merging the fix for sequential indices and see if the error still occurs?
mindplay-dk commentedon Aug 24, 2014
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 = trueusage 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.aik099 commentedon Aug 24, 2014
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.
Yes, you are correct.
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.
Yes 2 tests are needed:
mindplay-dk commentedon Aug 24, 2014
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.
aik099 commentedon Aug 24, 2014
I see change that we talked about in the
getAnnotationsmethod: annotations are now collected into local$annotationsvariable and only then assigned into$this->annotationsarray (this can give a performance boost because on each found annotation we don't need to access array key again).Also in
applyConstraintsmethod I see that you now return annotations. But I don't quite get the change in scanning algorithm.mindplay-dk commentedon Aug 24, 2014
It's simple - when I find an annotation with usage
$multiple=falseand$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? :-)
aik099 commentedon Aug 24, 2014
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.
mindplay-dk commentedon Aug 24, 2014
Something like that :-)
aik099 commentedon Aug 27, 2014
I've moved out scan forward changes made in #73 into separate #82 PR and merged it.
mindplay-dk commentedon Aug 27, 2014
Looking good 👍
aik099 commentedon Aug 27, 2014
I wonder how originally described problem can be solved. Right now the
getAnnotationsmethod doesn't keep class name in which annotation was used along with annotations. Because of this in theapplyConstrainsall annotations looks similar.There are several ways of solving:
Way 1
classNameproperty on the annotation (problem is that we can't enforce this through theIAnnotationinterface)->classNamepropertygetAnnotationsto theapplyConstraintsmethodapplyConstraintsremove only inherited annotations (ones where->classNameof annotation doesn't match to the class given to theapplyConstraintsmethod itself)Way 2
$classNamevariablegetAnnotationsto theapplyConstraintsmethodapplyConstraintsremove only inherited annotations (ones where$classNameof annotation doesn't match to the class given to theapplyConstraintsmethod itself)getAnnotationsmethodThe
Way 1might be easier, but annotation doesn't really care on which class it was defined. That's whyWay 2is there.Since the
getAnnotationsis called recursively for all parent classes theapplyConstrainswill only see inherited annotations from direct super-class. Maybe this can help in simplifying the way of removing inherited annotations.mindplay-dk commentedon Aug 27, 2014
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 = falseoccur 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?aik099 commentedon Aug 27, 2014
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.
aik099 commentedon Aug 27, 2014
The
getAnnotationsknows what is inherited from what, but we remove annotations inapplyConstraints, which doesn't know anything of that.