@@ -76,7 +76,20 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
76
76
77
77
public static function castGenerator (\Generator $ c , array $ a , Stub $ stub , $ isNested )
78
78
{
79
- return class_exists ('ReflectionGenerator ' , false ) ? self ::castReflectionGenerator (new \ReflectionGenerator ($ c ), $ a , $ stub , $ isNested ) : $ a ;
79
+ if (!class_exists ('ReflectionGenerator ' , false )) {
80
+ return $ a ;
81
+ }
82
+
83
+ // Cannot create ReflectionGenerator based on a terminated Generator
84
+ try {
85
+ $ reflectionGenerator = new \ReflectionGenerator ($ c );
86
+ } catch (\Exception $ e ) {
87
+ $ a [Caster::PREFIX_VIRTUAL .'closed ' ] = true ;
88
+
89
+ return $ a ;
90
+ }
91
+
92
+ return self ::castReflectionGenerator ($ reflectionGenerator , $ a , $ stub , $ isNested );
80
93
}
81
94
82
95
public static function castType (\ReflectionType $ c , array $ a , Stub $ stub , $ isNested )
@@ -99,31 +112,33 @@ public static function castReflectionGenerator(\ReflectionGenerator $c, array $a
99
112
if ($ c ->getThis ()) {
100
113
$ a [$ prefix .'this ' ] = new CutStub ($ c ->getThis ());
101
114
}
102
- $ x = $ c ->getFunction ();
115
+ $ function = $ c ->getFunction ();
103
116
$ frame = array (
104
- 'class ' => isset ($ x ->class ) ? $ x ->class : null ,
105
- 'type ' => isset ($ x ->class ) ? ($ x ->isStatic () ? ':: ' : '-> ' ) : null ,
106
- 'function ' => $ x ->name ,
117
+ 'class ' => isset ($ function ->class ) ? $ function ->class : null ,
118
+ 'type ' => isset ($ function ->class ) ? ($ function ->isStatic () ? ':: ' : '-> ' ) : null ,
119
+ 'function ' => $ function ->name ,
107
120
'file ' => $ c ->getExecutingFile (),
108
121
'line ' => $ c ->getExecutingLine (),
109
122
);
110
123
if ($ trace = $ c ->getTrace (DEBUG_BACKTRACE_IGNORE_ARGS )) {
111
- $ x = new \ReflectionGenerator ($ c ->getExecutingGenerator ());
124
+ $ function = new \ReflectionGenerator ($ c ->getExecutingGenerator ());
112
125
array_unshift ($ trace , array (
113
126
'function ' => 'yield ' ,
114
- 'file ' => $ x ->getExecutingFile (),
115
- 'line ' => $ x ->getExecutingLine () - 1 ,
127
+ 'file ' => $ function ->getExecutingFile (),
128
+ 'line ' => $ function ->getExecutingLine () - 1 ,
116
129
));
117
130
$ trace [] = $ frame ;
118
131
$ a [$ prefix .'trace ' ] = new TraceStub ($ trace , false , 0 , -1 , -1 );
119
132
} else {
120
- $ x = new FrameStub ($ frame , false , true );
121
- $ x = ExceptionCaster::castFrameStub ($ x , array (), $ x , true );
133
+ $ function = new FrameStub ($ frame , false , true );
134
+ $ function = ExceptionCaster::castFrameStub ($ function , array (), $ function , true );
122
135
$ a [$ prefix .'executing ' ] = new EnumStub (array (
123
- $ frame ['class ' ].$ frame ['type ' ].$ frame ['function ' ].'() ' => $ x [$ prefix .'src ' ],
136
+ $ frame ['class ' ].$ frame ['type ' ].$ frame ['function ' ].'() ' => $ function [$ prefix .'src ' ],
124
137
));
125
138
}
126
139
140
+ $ a [Caster::PREFIX_VIRTUAL .'closed ' ] = false ;
141
+
127
142
return $ a ;
128
143
}
129
144
0 commit comments