Validate return type using reflection #78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change aims to use declared method return type value, if defined, over doc block
@return
tag annotation, for\Hyva\Admin\Model\GridSourceType\RepositorySourceType\RepositorySourceFactory::validateReturnType
validation.It has been tested against the following PHP versions:
7.3 - 7.4 - 8.0 - 8.1 - 8.2 - 8.3
Given the following, simplified, class:
Current code execution relies in
\Magento\Framework\Reflection\MethodsMap::getMethodReturnType
method, which in turn uses only the@return
tag, to retrieve the return type. In the case above,SearchResultsInterface
is found as the@return
type declared in the doc block, but it fails to return its FQDN, so the\is_subclass_of
check fails.Simplifying and importing classes instead of using its FQDN every time is a common practice (PhpStorm, php-cs-fixer...), and cannot be applied in a standard way if this
@return
tag needs to be fully qualified in order to work.This change proposes to use reflection to get method declared return type, which is enforced to return, and is more reliable than a doc block annotation for validation. Approach can be discussed, as the suggested changes have been implemented quickly for testing purposes (although they work) and may be improved regarding caching, performance, or another criteria.
I hope this PR proposal is interesting to be added to source code, write me for any aclaration if needed, thank you!