Skip to content

Commit 403a284

Browse files
pawell67claude
andcommitted
Port the generic static return type resolution to the turbo extension
Mirrors CalledOnTypeUnresolvedMethodPrototypeReflection::transformStaticType() from the previous commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 3f078e0 commit 403a284

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

‎turbo-ext/src/CalledOnTypeUnresolvedMethodPrototypeReflection.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,29 @@ class CalledOnTypeUnresolvedMethodPrototypeReflection
217217
}
218218
zv::Val staticObjectType = pt_type_call(Z_OBJ_P(changed.raw()), PT_LC("getstaticobjecttype"), 0, NULL);
219219
if (UNEXPECTED(staticObjectType.isUndef())) return;
220+
/* a GenericObjectType is rebuilt without the class reflection of the
221+
* declaring class, which would not know the resolved type arguments:
222+
* new GenericObjectType($t->getClassName(), $t->getTypes(), $t->getSubtractedType(), variances: $t->getVariances()) */
223+
bool isGenericObject;
224+
pt_type_instanceof_ce(staticObjectType.raw(), pt_ce_generic_object_type, isGenericObject);
225+
if (isGenericObject) {
226+
zend_object *objectType = Z_OBJ_P(staticObjectType.raw());
227+
zv::Val className = pt_type_call(objectType, PT_LC("getclassname"), 0, NULL);
228+
if (UNEXPECTED(className.isUndef())) return;
229+
zv::Val objectTypes = pt_type_call(objectType, PT_LC("gettypes"), 0, NULL);
230+
if (UNEXPECTED(objectTypes.isUndef())) return;
231+
zv::Val subtracted = pt_type_call(objectType, PT_LC("getsubtractedtype"), 0, NULL);
232+
if (UNEXPECTED(subtracted.isUndef())) return;
233+
zv::Val objectVariances = pt_type_call(objectType, PT_LC("getvariances"), 0, NULL);
234+
if (UNEXPECTED(objectVariances.isUndef())) return;
235+
if (UNEXPECTED(Z_TYPE_P(className.raw()) != IS_STRING)) {
236+
zend_type_error("phpstan_turbo: getClassName() must return a string");
237+
return;
238+
}
239+
zval rebuiltRaw;
240+
if (UNEXPECTED(!pt_generic_object_type_new(&rebuiltRaw, Z_STR_P(className.raw()), objectTypes.raw(), subtracted.raw(), NULL, objectVariances.raw()))) return;
241+
staticObjectType = zv::Val::adopt(rebuiltRaw);
242+
}
220243
zv::Val traversed = pt_type_call_callable(&argv[1], 1, staticObjectType.raw());
221244
if (UNEXPECTED(traversed.isUndef())) return;
222245
traversed.intoReturnValue(return_value);

0 commit comments

Comments
 (0)