@@ -43,6 +43,17 @@ static zend_function *pt_object_shape_reason_decorator_invoke = nullptr;
4343
4444namespace phpstanturbo {
4545
46+ /* a $properties value the twin calls $method() on: the twin's
47+ * array<string, Type> is not checked when it is constructed, so a
48+ * non-object value surfaces at its first method call as the engine's Error;
49+ * NULL with that Error pending */
50+ static zend_object *propertyTypeObject (zval *value, const char *method)
51+ {
52+ if (EXPECTED (Z_TYPE_P (value) == IS_OBJECT )) return Z_OBJ_P (value);
53+ zend_throw_error (NULL , " Call to a member function %s() on %s" , method, zend_zval_value_name (value));
54+ return NULL ;
55+ }
56+
4657/* Mirrors PHPStan\Type\ObjectShapeType. State lives in the PHP object's
4758 * $properties and $optionalProperties. */
4859class ObjectShapeType
@@ -84,7 +95,9 @@ class ObjectShapeType
8495 if (UNEXPECTED (props == NULL )) return zv::Val ();
8596 zv::Arr classes = zv::Arr::create (0 );
8697 for (zv::ArrayEntry entry : zv::ArrRef (props)) {
87- zv::Val referenced = pt_type_call (Z_OBJ_P (entry.value ().raw ()), PT_LC (" getreferencedclasses" ), 0 , NULL );
98+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " getReferencedClasses" );
99+ if (UNEXPECTED (propertyType == NULL )) return zv::Val ();
100+ zv::Val referenced = pt_type_call (propertyType, PT_LC (" getreferencedclasses" ), 0 , NULL );
88101 if (UNEXPECTED (referenced.isUndef ())) return zv::Val ();
89102 if (UNEXPECTED (!zv::Ref (referenced.raw ()).isArray ())) {
90103 zend_type_error (" phpstan_turbo: getReferencedClasses() must return array" );
@@ -233,7 +246,14 @@ class ObjectShapeType
233246
234247 zv::Val otherPropertyType = pt_type_call (Z_OBJ_P (otherProperty.raw ()), PT_LC (" getreadabletype" ), 0 , NULL );
235248 if (UNEXPECTED (otherPropertyType.isUndef ())) return zv::Val ();
236- /* $verbosity = VerbosityLevel::getRecommendedLevelByType($propertyType, $otherPropertyType) */
249+ /* $verbosity = VerbosityLevel::getRecommendedLevelByType($propertyType, $otherPropertyType),
250+ * whose `Type $acceptingType` parameter is the first to see the property type */
251+ bool propertyIsType;
252+ if (UNEXPECTED (!pt_type_instanceof (propertyType, PT_CLASS_TYPE , propertyIsType))) return zv::Val ();
253+ if (UNEXPECTED (!propertyIsType)) {
254+ zend_type_error (" %s::getRecommendedLevelByType(): Argument #1 ($acceptingType) must be of type %s, %s given" , ZSTR_VAL (pt_ce_verbosity_level->name ), ptcls::type, zend_zval_value_name (propertyType));
255+ return zv::Val ();
256+ }
237257 zv::Val verbosity = pt_type_verbosity_recommended (propertyType, otherPropertyType.raw ());
238258 if (UNEXPECTED (verbosity.isUndef ())) return zv::Val ();
239259 /* $propertyType->accepts($otherPropertyType, $strictTypes)->decorateReasons(...) */
@@ -355,7 +375,9 @@ class ObjectShapeType
355375
356376 zv::Val otherPropertyType = pt_type_call (Z_OBJ_P (otherProperty.raw ()), PT_LC (" getreadabletype" ), 0 , NULL );
357377 if (UNEXPECTED (otherPropertyType.isUndef ())) return zv::Val ();
358- zv::Val isSuperType = pt_type_op (Z_OBJ_P (propertyType), PT_OP_IS_SUPER_TYPE_OF , 1 , otherPropertyType.raw ());
378+ zend_object *propertyTypeObj = propertyTypeObject (propertyType, " isSuperTypeOf" );
379+ if (UNEXPECTED (propertyTypeObj == NULL )) return zv::Val ();
380+ zv::Val isSuperType = pt_type_op (propertyTypeObj, PT_OP_IS_SUPER_TYPE_OF , 1 , otherPropertyType.raw ());
359381 if (UNEXPECTED (isSuperType.isUndef ())) return zv::Val ();
360382 zend_long isSuperTypeValue = pt_type_result_trinary (isSuperType.raw ());
361383 if (UNEXPECTED (isSuperTypeValue < 0 )) return zv::Val ();
@@ -398,7 +420,9 @@ class ObjectShapeType
398420 out = false ;
399421 return true ;
400422 }
401- zv::Val equal = pt_type_op (Z_OBJ_P (entry.value ().raw ()), PT_OP_EQUALS , 1 , other);
423+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " equals" );
424+ if (UNEXPECTED (propertyType == NULL )) return false ;
425+ zv::Val equal = pt_type_op (propertyType, PT_OP_EQUALS , 1 , other);
402426 if (UNEXPECTED (equal.isUndef ())) return false ;
403427 if (!zend_is_true (equal.raw ())) {
404428 out = false ;
@@ -498,7 +522,9 @@ class ObjectShapeType
498522 if (skip) continue ;
499523 zv::Val receivedPropertyType = pt_type_call (Z_OBJ_P (receivedProperty.raw ()), PT_LC (" getreadabletype" ), 0 , NULL );
500524 if (UNEXPECTED (receivedPropertyType.isUndef ())) return zv::Val ();
501- zv::Val inferred = pt_type_call (Z_OBJ_P (entry.value ().raw ()), PT_LC (" infertemplatetypes" ), 1 , receivedPropertyType.raw ());
525+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " inferTemplateTypes" );
526+ if (UNEXPECTED (propertyType == NULL )) return zv::Val ();
527+ zv::Val inferred = pt_type_call (propertyType, PT_LC (" infertemplatetypes" ), 1 , receivedPropertyType.raw ());
502528 if (UNEXPECTED (inferred.isUndef ())) return zv::Val ();
503529 if (UNEXPECTED (!zv::Ref (typeMap.raw ()).isObject ())) {
504530 zend_type_error (" phpstan_turbo: TemplateTypeMap expected" );
@@ -527,7 +553,9 @@ class ObjectShapeType
527553 if (UNEXPECTED (props == NULL )) return zv::Val ();
528554 zv::Arr references = zv::Arr::create (0 );
529555 for (zv::ArrayEntry entry : zv::ArrRef (props)) {
530- zv::Val referenced = pt_type_op (Z_OBJ_P (entry.value ().raw ()), PT_OP_GET_REFERENCED_TEMPLATE_TYPES , 1 , variance.raw ());
556+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " getReferencedTemplateTypes" );
557+ if (UNEXPECTED (propertyType == NULL )) return zv::Val ();
558+ zv::Val referenced = pt_type_op (propertyType, PT_OP_GET_REFERENCED_TEMPLATE_TYPES , 1 , variance.raw ());
531559 if (UNEXPECTED (referenced.isUndef ())) return zv::Val ();
532560 if (UNEXPECTED (!zv::Ref (referenced.raw ()).isArray ())) {
533561 zend_type_error (" phpstan_turbo: getReferencedTemplateTypes() must return array" );
@@ -558,7 +586,12 @@ class ObjectShapeType
558586 smart_str_free (&description);
559587 return zv::Val ();
560588 }
561- zv::Val item = pt_type_op (Z_OBJ_P (entry.value ().raw ()), PT_OP_DESCRIBE , 1 , level);
589+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " describe" );
590+ if (UNEXPECTED (propertyType == NULL )) {
591+ smart_str_free (&description);
592+ return zv::Val ();
593+ }
594+ zv::Val item = pt_type_op (propertyType, PT_OP_DESCRIBE , 1 , level);
562595 if (UNEXPECTED (item.isUndef ())) {
563596 smart_str_free (&description);
564597 return zv::Val ();
@@ -596,7 +629,7 @@ class ObjectShapeType
596629 for (zv::ArrayEntry entry : zv::ArrRef (props)) {
597630 zval transformed;
598631 if (UNEXPECTED (!pt_call_fci (fci, fcc, 1 , entry.value ().raw (), &transformed))) return zv::Val ();
599- if (Z_TYPE (transformed) != IS_OBJECT || Z_OBJ ( transformed) != Z_OBJ_P ( entry.value ().raw ())) {
632+ if (! zend_is_identical (& transformed, entry.value ().raw ())) { /* `$transformed !== $propertyType` */
600633 stillOriginal = false ;
601634 }
602635 setEntry (mapped, entry, zv::Val::adopt (transformed));
@@ -638,7 +671,7 @@ class ObjectShapeType
638671 zv::Args cbArgs{entry.value ().raw (), rightType.raw ()};
639672 zval transformed;
640673 if (UNEXPECTED (!pt_call_fci (fci, fcc, 2 , cbArgs, &transformed))) return zv::Val ();
641- if (Z_TYPE (transformed) != IS_OBJECT || Z_OBJ ( transformed) != Z_OBJ_P ( entry.value ().raw ())) {
674+ if (! zend_is_identical (& transformed, entry.value ().raw ())) { /* `$transformed !== $propertyType` */
642675 stillOriginal = false ;
643676 }
644677 setEntry (mapped, entry, zv::Val::adopt (transformed));
@@ -713,7 +746,9 @@ class ObjectShapeType
713746 }
714747 bool optional;
715748 if (UNEXPECTED (!isOptional (&propertyName, optional))) return zv::Val ();
716- zv::Val valueNode = pt_type_call (Z_OBJ_P (entry.value ().raw ()), PT_LC (" tophpdocnode" ), 0 , NULL );
749+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " toPhpDocNode" );
750+ if (UNEXPECTED (propertyType == NULL )) return zv::Val ();
751+ zv::Val valueNode = pt_type_call (propertyType, PT_LC (" tophpdocnode" ), 0 , NULL );
717752 if (UNEXPECTED (valueNode.isUndef ())) return zv::Val ();
718753 zv::Args args{keyNode.raw (), optional, valueNode.raw ()};
719754 zv::Val item = pt_type_new (PT_CLASS_OBJECT_SHAPE_ITEM_NODE , 3 , args);
@@ -729,7 +764,9 @@ class ObjectShapeType
729764 zval *props = properties ();
730765 if (UNEXPECTED (props == NULL )) return false ;
731766 for (zv::ArrayEntry entry : zv::ArrRef (props)) {
732- zv::Val has = pt_type_op (Z_OBJ_P (entry.value ().raw ()), PT_OP_HAS_TEMPLATE_OR_LATE_RESOLVABLE_TYPE , 0 , NULL );
767+ zend_object *propertyType = propertyTypeObject (entry.value ().raw (), " hasTemplateOrLateResolvableType" );
768+ if (UNEXPECTED (propertyType == NULL )) return false ;
769+ zv::Val has = pt_type_op (propertyType, PT_OP_HAS_TEMPLATE_OR_LATE_RESOLVABLE_TYPE , 0 , NULL );
733770 if (UNEXPECTED (has.isUndef ())) return false ;
734771 if (zend_is_true (has.raw ())) {
735772 out = true ;
0 commit comments