Skip to content

Instantly share code, notes, and snippets.

@LeeSaferite
Created May 21, 2018 22:35
Show Gist options
  • Save LeeSaferite/42fdd365f5eb00e6eb7042ccbd73d66f to your computer and use it in GitHub Desktop.
Save LeeSaferite/42fdd365f5eb00e6eb7042ccbd73d66f to your computer and use it in GitHub Desktop.
<?php
namespace Lokey\Fixes\Plugin\Magento\Framework\Reflection;
use Zend\Code\Reflection\MethodReflection;
class TypeProcessor
{
/**
* Fix handling of docblock return definitions
*
* @see \Magento\Framework\Reflection\TypeProcessor::getGetterReturnType()
*
* @param \Magento\Framework\Reflection\TypeProcessor $subject
* @param callable $proceed
* @param MethodReflection $methodReflection
*
* @return array
*/
public function aroundGetGetterReturnType(
\Magento\Framework\Reflection\TypeProcessor $subject,
callable $proceed,
MethodReflection $methodReflection
): array {
$result = call_user_func($proceed, $methodReflection);
$returnType = $result['type'];
if ($returnType === 'this') {
$result['type'] = '\\' . $methodReflection->getDeclaringClass()->getName();
return $result;
}
if (!$subject->isTypeSimple($returnType) && !$subject->isTypeAny($returnType) && $returnType[0] !== '\\') {
$isArray = false;
if ($subject->isArrayType($returnType)) {
$isArray = true;
$returnType = $subject->getArrayItemType($returnType);
}
$extra = explode('\\', $returnType);
$search = array_shift($extra);
$uses = $methodReflection->getDeclaringClass()->getDeclaringFile()->getUses();
foreach ($uses as $use) {
if ($use['as'] !== null) {
// We are looking for the alias only
if ($search === $use['as']) {
$returnType = '\\' . $use['use'] . '\\' . implode('\\', $extra);
break;
}
} else {
if ($search === last(explode('\\', $use['use']))) {
$returnType = '\\' . $use['use'] . '\\' . implode('\\', $extra);
break;
}
}
}
if ($returnType[0] !== '\\') {
$returnType = $methodReflection->getDeclaringClass()->getNamespaceName() . '\\' . $returnType;
}
$returnType = rtrim($returnType, '\\');
if ($isArray) {
$returnType .= '[]';
}
$result['type'] = $returnType;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment