Skip to content

Commit 7ea3af7

Browse files
ENGCOM-6920: Reflection: Fix null as first return type #25806
- Merge Pull Request #25806 from Parakoopa/magento2:nullable-api-getters - Merged commits: 1. 0cea191 2. 99a1a98 3. 253f881 4. 7e3390d
2 parents a25c107 + 7e3390d commit 7ea3af7

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,25 @@ 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+
367386
/**
368387
* Simple and complex data provider
369388
*

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ 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+
290297
$nullable = in_array('null', $types);
291298

292299
return [
@@ -312,6 +319,7 @@ public function getExceptions($methodReflection)
312319
if (is_array($throwsTypes)) {
313320
/** @var $throwsType \Zend\Code\Reflection\DocBlock\Tag\ThrowsTag */
314321
foreach ($throwsTypes as $throwsType) {
322+
// phpcs:ignore
315323
$exceptions = array_merge($exceptions, $throwsType->getTypes());
316324
}
317325
}
@@ -513,13 +521,15 @@ public function getParamType(ParameterReflection $param)
513521
{
514522
$type = $param->detectType();
515523
if ($type === 'null') {
516-
throw new \LogicException(sprintf(
517-
'@param annotation is incorrect for the parameter "%s" in the method "%s:%s".'
518-
. ' First declared type should not be null. E.g. string|null',
519-
$param->getName(),
520-
$param->getDeclaringClass()->getName(),
521-
$param->getDeclaringFunction()->name
522-
));
524+
throw new \LogicException(
525+
sprintf(
526+
'@param annotation is incorrect for the parameter "%s" in the method "%s:%s".'
527+
. ' First declared type should not be null. E.g. string|null',
528+
$param->getName(),
529+
$param->getDeclaringClass()->getName(),
530+
$param->getDeclaringFunction()->name
531+
)
532+
);
523533
}
524534
if ($type === 'array') {
525535
// try to determine class, if it's array of objects

0 commit comments

Comments
 (0)