Skip to content

Commit 0cea191

Browse files
committed
#25656: Fixes a bug in reflection, where null couldn't be the first type in return type
1 parent c7ca62e commit 0cea191

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

lib/internal/Magento/Framework/Reflection/Test/Unit/Fixture/TSample.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ public function getName()
2222
{
2323
return '';
2424
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function getWithNull()
30+
{
31+
return null;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getOnlyNull()
38+
{
39+
return null;
40+
}
2541
}

lib/internal/Magento/Framework/Reflection/Test/Unit/Fixture/TSampleInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ public function getPropertyName();
1818
* Doc block without return tag.
1919
*/
2020
public function getName();
21+
22+
/**
23+
* return annotation with a null type at first position
24+
* @return null|string
25+
*/
26+
public function getWithNull();
27+
28+
/**
29+
* return annotation with only null type
30+
* @return null
31+
*/
32+
public function getOnlyNull();
2133
}

lib/internal/Magento/Framework/Reflection/Test/Unit/TypeProcessorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,39 @@ public function testGetReturnTypeWithoutReturnTag()
364364
$this->typeProcessor->getGetterReturnType($methodReflection);
365365
}
366366

367+
/**
368+
* Checks a case when method return annotation has a null-type at first position,
369+
* and a valid type at second.
370+
*/
371+
public function testGetReturnTypeNullAtFirstPos()
372+
{
373+
$expected = [
374+
'type' => 'string',
375+
'isRequired' => false,
376+
'description' => null,
377+
'parameterCount' => 0
378+
];
379+
380+
$classReflection = new ClassReflection(TSample::class);
381+
$methodReflection = $classReflection->getMethod('getWithNull');
382+
383+
self::assertEquals($expected, $this->typeProcessor->getGetterReturnType($methodReflection));
384+
}
385+
386+
/**
387+
* Checks a case when method return annotation has a null-type at first position,
388+
* and no other valid return type.
389+
*
390+
* @expectedException \InvalidArgumentException
391+
* @expectedExceptionMessage No valid return type for Magento\Framework\Reflection\Test\Unit\Fixture\TSample::getOnlyNull() specified. Verify the return type and try again.
392+
*/
393+
public function testGetReturnTypeNullAtFirstPosNoValidType()
394+
{
395+
$classReflection = new ClassReflection(TSample::class);
396+
$methodReflection = $classReflection->getMethod('getOnlyNull');
397+
$this->typeProcessor->getGetterReturnType($methodReflection);
398+
}
399+
367400
/**
368401
* Simple and complex data provider
369402
*

lib/internal/Magento/Framework/Reflection/TypeProcessor.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,22 @@ public function getGetterReturnType($methodReflection)
286286
{
287287
$returnAnnotation = $this->getMethodReturnAnnotation($methodReflection);
288288
$types = $returnAnnotation->getTypes();
289-
$returnType = current($types);
289+
$returnType = null;
290+
foreach ($types as $type) {
291+
if ($type != 'null') {
292+
$returnType = $type;
293+
break;
294+
}
295+
}
296+
if (!$returnType) {
297+
throw new \InvalidArgumentException(
298+
sprintf(
299+
'No valid return type for %s::%s() specified. Verify the return type and try again.',
300+
$methodReflection->getDeclaringClass()->getName(),
301+
$methodReflection->getName()
302+
)
303+
);
304+
}
290305
$nullable = in_array('null', $types);
291306

292307
return [

0 commit comments

Comments
 (0)