-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Skip AnnotationScanner if class name information can't be found. #4874
Conversation
Update: Also done this for |
Please add unit tests, @tomphp -- and thanks! |
…to fix/reflection-with-traits
@weierophinney Apologies, unit tests now included. |
I've just seen the 5.3.x travis builds failed to you the traits in the test suite. Is there a suggested method to deal with this in the ZF2 test suite? Is it just a matter of check the php version in the test and return if it's pre 5.4? |
Yes -- do something like this in the individual test methods and/or the if (version_compare(PHP_VERSION, '5.4', 'lt')) {
$this->markTestSkipped('Skipping; PHP 5.4 or greater is needed');
} |
I've added pre 5.4 version skip tests but I've also just noticed PR #4989 which should stop there being a problem with using traits. However still I think |
@tomphp I'm attempting to merge this to develop, as it implements new functionality for
Can you look into it, please? |
Hi @weierophinney, I've taken a look and as I expected PR #4989 has been merged into the develop branch, this has resulted in the After looking through the code it would appear that so long as However null and false are both valid return values for Therefore my question is; should this be taken as a situation which can't arise because of the tight dependency on Or should we say that because The only way I can think to test it would be to allow the injection of a Any thoughts? |
I'd say if they can return something else, we should handle that. We often put in such defensive code even without testing it as a precautionary measure when testing it would be to roundabout. |
Ooops, very sorry about the delay here, I got distracted and forgot to respond. Shall I just remove the tests then and submit without them since they are just a guard or shall we just close the PR? |
@tomphp Let's remove the tests, but keep the defensive code. |
@weierophinney I have removed the tests. However I have been reading this rather fantastic book http://pragprog.com/book/lotdd/modern-c-programming-with-test-driven-development and discovered the override factory method pattern which might be a way to deal with this sort of situation. This would involve refactoring protected function createFileScanner($fileName)
{
return new FileScanner($fileName);
}
public function getAnnotations(AnnotationManager $annotationManager)
{
$docComment = $this->getDocComment();
if ($docComment == '') {
return false;
}
if ($this->annotations) {
return $this->annotations;
}
$fileScanner = $this->createFileScanner($this->getFileName());
$nameInformation = $fileScanner->getClassNameInformation($this->getName());
if (!$nameInformation) {
return false;
}
$this->annotations = new AnnotationScanner($annotationManager, $docComment, $nameInformation);
return $this->annotations;
} Then the use Zend\Code\Reflection\ClassReflection;
class ClassReflectionExposedFileScanner extends ClassReflection
{
protected $fileScanner;
public function setFileScanner(FileScanner $fileScanner)
{
$this->fileScanner = $fileScanner;
}
public function createFileScanner($filename)
{
return $this->fileScanner;
}
} This sub class could then be used in the test with an injected FileScanner mock. Just a thought? |
@tomphp The override-factory-pattern usage you show looks great! Want to include it in this one, or no? |
May as well do things the best as well as possible so I'll get this done tomorrow ;) |
Finally got there but made a bit of a mess of the commits... do you want me to squash and resubmit the PR? |
Skip AnnotationScanner if class name information can't be found.
Forward port #4874 Conflicts: tests/ZendTest/Code/Reflection/MethodReflectionTest.php
Currently when
ClassReflection::getAnnotations()
is called on class which uses a trait and error occurs when theAnnotationScanner
constructor gets called withfalse
for the$nameInformation
parameter.In this PR I have simply changed it so that
getAnnotations()
returnsfalse
instead of creating a newAnnotationScanner
ifFileScanner::getClassNameInformation()
returnsfalse
.This simple causes traits to be skipped, maybe instead more needs to be done here to scan for traits in the scanner, if so let me know.