77use  Laminas \Code \Generator \DocBlockGenerator ;
88use  Laminas \Code \Generator \MethodGenerator  as  LaminasMethodGenerator ;
99use  Laminas \Code \Reflection \MethodReflection ;
10+ use  ReflectionException ;
11+ use  ReflectionMethod ;
1012
1113/** 
1214 * Method generator that fixes minor quirks in ZF2's method generator 
1315 */ 
1416class  MethodGenerator extends  LaminasMethodGenerator
1517{
18+     protected  $ hasTentativeReturnType  = false ;
19+ 
1620    /** 
1721     * @return static 
1822     */ 
@@ -24,15 +28,72 @@ public static function fromReflectionWithoutBodyAndDocBlock(MethodReflection $re
2428        $ method ->setInterface (false );
2529        $ method ->setBody ('' );
2630
31+         if  (\PHP_VERSION_ID  < 80100 ) {
32+             return  $ method ;
33+         }
34+ 
35+         /** @var callable(ReflectionMethod) : ReflectionMethod $getPrototype */ 
36+         $ getPrototype  = \Closure::fromCallable ([new  ReflectionMethod (ReflectionMethod::class, 'getPrototype ' ), 'invoke ' ]);
37+ 
38+         while  (true ) {
39+             if  ($ reflectionMethod ->hasTentativeReturnType ()) {
40+                 $ method ->hasTentativeReturnType  = true ;
41+                 break ;
42+             }
43+ 
44+             if  ($ reflectionMethod ->isAbstract ()) {
45+                 break ;
46+             }
47+ 
48+             try  {
49+                 $ reflectionMethod  = $ getPrototype ($ reflectionMethod );
50+             } catch  (ReflectionException   $ e ) {
51+                 break ;
52+             }
53+         }
54+ 
2755        return  $ method ;
2856    }
2957
30-     /** 
31-      * {@inheritDoc} override needed to specify type in more detail 
32-      */ 
3358    public  function  getDocBlock (): ?DocBlockGenerator 
3459    {
35-         return  parent ::getDocBlock ();
60+         $ docBlock  = parent ::getDocBlock ();
61+ 
62+         if  (! $ this  ->hasTentativeReturnType ) {
63+             return  $ docBlock ;
64+         }
65+ 
66+         if  ($ docBlock  === null ) {
67+             return  new  class  ($ this  ->getIndentation ()) extends  DocBlockGenerator {
68+                 public  function  __construct (string  $ indentation )
69+                 {
70+                     $ this  ->setIndentation ($ indentation );
71+                 }
72+ 
73+                 public  function  generate (): string 
74+                 {
75+                     return  $ this  ->getIndentation () . '#[\ReturnTypeWillChange] '  . self ::LINE_FEED ;
76+                 }
77+             };
78+         }
79+ 
80+         return  new  class  ($ docBlock ) extends  DocBlockGenerator {
81+             public  function  __construct (DocBlockGenerator   $ docBlock )
82+             {
83+                 $ this  ->setShortDescription ($ docBlock ->getShortDescription ());
84+                 $ this  ->setLongDescription ($ docBlock ->getLongDescription ());
85+                 $ this  ->setTags ($ docBlock ->getTags ());
86+                 $ this  ->setWordWrap ($ docBlock ->getWordWrap ());
87+                 $ this  ->setSourceDirty ($ docBlock ->isSourceDirty ());
88+                 $ this  ->setIndentation ($ docBlock ->getIndentation ());
89+                 $ this  ->setSourceContent ($ docBlock ->getSourceContent ());
90+             }
91+ 
92+             public  function  generate (): string 
93+             {
94+                 return  parent ::generate () . $ this  ->getIndentation () . '#[\ReturnTypeWillChange] '  . self ::LINE_FEED ;
95+             }
96+         };
3697    }
3798
3899    /** 
0 commit comments