Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection: Fix null as first return type #25806

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ public function getName()
{
return '';
}

/**
* @inheritdoc
*/
public function getWithNull()
{
return null;
}

/**
* @inheritdoc
*/
public function getOnlyNull()
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ public function getPropertyName();
* Doc block without return tag.
*/
public function getName();

/**
* return annotation with a null type at first position
* @return null|string
*/
public function getWithNull();

/**
* return annotation with only null type
* @return null
*/
public function getOnlyNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,25 @@ public function testGetReturnTypeWithoutReturnTag()
$this->typeProcessor->getGetterReturnType($methodReflection);
}

/**
* Checks a case when method return annotation has a null-type at first position,
* and a valid type at second.
*/
public function testGetReturnTypeNullAtFirstPos()
{
$expected = [
'type' => 'string',
'isRequired' => false,
'description' => null,
'parameterCount' => 0
];

$classReflection = new ClassReflection(TSample::class);
$methodReflection = $classReflection->getMethod('getWithNull');

self::assertEquals($expected, $this->typeProcessor->getGetterReturnType($methodReflection));
}

/**
* Simple and complex data provider
*
Expand Down
26 changes: 18 additions & 8 deletions lib/internal/Magento/Framework/Reflection/TypeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,14 @@ public function getGetterReturnType($methodReflection)
{
$returnAnnotation = $this->getMethodReturnAnnotation($methodReflection);
$types = $returnAnnotation->getTypes();
$returnType = current($types);
$returnType = null;
foreach ($types as $type) {
if ($type != 'null') {
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
$returnType = $type;
break;
}
}

$nullable = in_array('null', $types);

return [
Expand All @@ -312,6 +319,7 @@ public function getExceptions($methodReflection)
if (is_array($throwsTypes)) {
/** @var $throwsType \Zend\Code\Reflection\DocBlock\Tag\ThrowsTag */
foreach ($throwsTypes as $throwsType) {
// phpcs:ignore
$exceptions = array_merge($exceptions, $throwsType->getTypes());
}
}
Expand Down Expand Up @@ -513,13 +521,15 @@ public function getParamType(ParameterReflection $param)
{
$type = $param->detectType();
if ($type === 'null') {
throw new \LogicException(sprintf(
'@param annotation is incorrect for the parameter "%s" in the method "%s:%s".'
. ' First declared type should not be null. E.g. string|null',
$param->getName(),
$param->getDeclaringClass()->getName(),
$param->getDeclaringFunction()->name
));
throw new \LogicException(
sprintf(
'@param annotation is incorrect for the parameter "%s" in the method "%s:%s".'
. ' First declared type should not be null. E.g. string|null',
$param->getName(),
$param->getDeclaringClass()->getName(),
$param->getDeclaringFunction()->name
)
);
}
if ($type === 'array') {
// try to determine class, if it's array of objects
Expand Down