@@ -26,6 +26,7 @@ namespace sigs = ptdecl::MethodThrowPointHelper::sig;
2626#include " TypeOps.h"
2727#include " AcceptorValues.h"
2828#include " ParserVisitors.h"
29+ #include " AnalyserValues.h"
2930
3031static zend_class_entry *pt_ce_method_throw_point_helper;
3132
@@ -104,6 +105,12 @@ class MethodThrowPointHelper
104105 * exception */
105106 zv::Val getThrowPoint (zval *methodReflection, zval *parametersAcceptor, zval *normalizedMethodCall, zval *scope, zval *context, zval *methodCallReturnType) const
106107 {
108+ zend_class_entry *unionTypeMethodReflectionCe = pt_class (PT_CLASS_UNION_TYPE_METHOD_REFLECTION );
109+ if (UNEXPECTED (unionTypeMethodReflectionCe == NULL )) return zv::Val ();
110+ if (instanceof_function (Z_OBJCE_P (methodReflection), unionTypeMethodReflectionCe)) {
111+ return getUnionThrowPoint (methodReflection, parametersAcceptor, normalizedMethodCall, scope, context, methodCallReturnType);
112+ }
113+
107114 zend_class_entry *methodCallCe = pt_class (PT_CLASS_METHOD_CALL );
108115 if (UNEXPECTED (methodCallCe == NULL )) return zv::Val ();
109116 bool isMethodCall = instanceof_function (Z_OBJCE_P (normalizedMethodCall), methodCallCe);
@@ -258,6 +265,52 @@ class MethodThrowPointHelper
258265private:
259266 zend_object *self;
260267
268+ /* Mirrors getThrowPoint()'s UnionTypeMethodReflection arm: the inner
269+ * methods' throw points, combined */
270+ zv::Val getUnionThrowPoint (zval *methodReflection, zval *parametersAcceptor, zval *normalizedMethodCall, zval *scope, zval *context, zval *methodCallReturnType) const
271+ {
272+ zv::Val methods = pt_type_call (Z_OBJ_P (methodReflection), PT_LC (" getmethods" ), 0 , NULL );
273+ if (UNEXPECTED (methods.isUndef ())) return zv::Val ();
274+
275+ zv::Arr throwTypes = zv::Arr::create (0 );
276+ bool canContainAnyThrowable = false ;
277+ bool isExplicit = false ;
278+ if (EXPECTED (Z_TYPE_P (methods.raw ()) == IS_ARRAY )) {
279+ for (zv::ArrayEntry entry : zv::ArrRef (methods.raw ())) {
280+ zval *innerMethodReflection = entry.value ().deref ().raw ();
281+ if (UNEXPECTED (Z_TYPE_P (innerMethodReflection) != IS_OBJECT )) {
282+ zend_type_error (" PHPStan\\ Analyser\\ ExprHandler\\ Helper\\ MethodThrowPointHelper::getThrowPoint(): Argument #1 ($methodReflection) must be of type PHPStan\\ Reflection\\ MethodReflection, %s given" , zend_zval_value_name (innerMethodReflection));
283+ return zv::Val ();
284+ }
285+ zv::Val throwPoint = getThrowPoint (innerMethodReflection, parametersAcceptor, normalizedMethodCall, scope, context, methodCallReturnType);
286+ if (UNEXPECTED (throwPoint.isUndef ())) return zv::Val ();
287+ if (Z_TYPE_P (throwPoint.raw ()) == IS_NULL ) continue ;
288+
289+ zv::Val typeHold;
290+ zval *type = pt_internal_throw_point_type (throwPoint.raw (), typeHold);
291+ if (UNEXPECTED (type == NULL )) return zv::Val ();
292+ throwTypes.push (zv::Val::copyOf (zv::Ref (type)));
293+ bool innerCanContainAnyThrowable;
294+ if (UNEXPECTED (!pt_internal_throw_point_can_contain_any_throwable (throwPoint.raw (), innerCanContainAnyThrowable))) return zv::Val ();
295+ if (innerCanContainAnyThrowable) canContainAnyThrowable = true ;
296+ bool innerIsExplicit;
297+ if (UNEXPECTED (!pt_internal_throw_point_is_explicit (throwPoint.raw (), innerIsExplicit))) return zv::Val ();
298+ if (innerIsExplicit) isExplicit = true ;
299+ }
300+ }
301+
302+ HashTable *table = throwTypes.table ();
303+ uint32_t count = zend_hash_num_elements (table);
304+ if (count == 0 ) return zv::Val::null ();
305+ if (!isExplicit) return createImplicit (scope, normalizedMethodCall);
306+
307+ zv::Val throwType = EXPECTED (HT_IS_PACKED (table) && table->nNumUsed == count)
308+ ? pt_type_combinator_union (count, table->arPacked )
309+ : pt_type_combinator_call_spread (PT_LC (" union" ), table);
310+ if (UNEXPECTED (throwType.isUndef ())) return zv::Val ();
311+ return createExplicit (scope, throwType.raw (), normalizedMethodCall, canContainAnyThrowable);
312+ }
313+
261314 zval *slot (uint32_t index) const { return OBJ_PROP_NUM (self, index); }
262315
263316 /* the engine's Error for reading a never-written typed property */
0 commit comments