File tree Expand file tree Collapse file tree 4 files changed +77
-1
lines changed
lib/internal/Magento/Framework/Reflection Expand file tree Collapse file tree 4 files changed +77
-1
lines changed Original file line number Diff line number Diff line change @@ -22,4 +22,20 @@ public function getName()
22
22
{
23
23
return '' ;
24
24
}
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
+ }
25
41
}
Original file line number Diff line number Diff line change @@ -18,4 +18,16 @@ public function getPropertyName();
18
18
* Doc block without return tag.
19
19
*/
20
20
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 ();
21
33
}
Original file line number Diff line number Diff line change @@ -364,6 +364,39 @@ public function testGetReturnTypeWithoutReturnTag()
364
364
$ this ->typeProcessor ->getGetterReturnType ($ methodReflection );
365
365
}
366
366
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
+
367
400
/**
368
401
* Simple and complex data provider
369
402
*
Original file line number Diff line number Diff line change @@ -286,7 +286,22 @@ public function getGetterReturnType($methodReflection)
286
286
{
287
287
$ returnAnnotation = $ this ->getMethodReturnAnnotation ($ methodReflection );
288
288
$ 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
+ }
290
305
$ nullable = in_array ('null ' , $ types );
291
306
292
307
return [
You can’t perform that action at this time.
0 commit comments