@@ -628,6 +628,7 @@ static inheritance_status zend_perform_covariant_type_check(
628
628
* as the child can add types, however none of them can be a supertype of
629
629
* a parent type. */
630
630
if (ZEND_TYPE_IS_INTERSECTION (fe_type )) {
631
+ bool parent_union_has_unresolved = false;
631
632
/* First try to check whether we can succeed without resolving anything */
632
633
ZEND_TYPE_FOREACH (proto_type , single_type ) {
633
634
inheritance_status status ;
@@ -649,14 +650,33 @@ static inheritance_status zend_perform_covariant_type_check(
649
650
proto_scope , proto_class_name , proto_ce ,
650
651
fe_scope , fe_type , /* register_unresolved */ false);
651
652
652
- if (status == INHERITANCE_ERROR ) {
653
- return NHERITANCE_ERROR ;
654
- }
655
- if (status != INHERITANCE_SUCCESS ) {
656
- ZEND_ASSERT (status == INHERITANCE_UNRESOLVED );
657
- all_success = false;
653
+ /* If the parent is a union type then the intersection type must only be
654
+ * a subtype of one of them */
655
+ if (ZEND_TYPE_IS_UNION (proto_type )) {
656
+ if (status == INHERITANCE_SUCCESS ) {
657
+ return INHERITANCE_SUCCESS ;
658
+ }
659
+ if (status == INHERITANCE_UNRESOLVED ) {
660
+ all_success = false;
661
+ }
662
+ } else {
663
+ if (status == INHERITANCE_ERROR ) {
664
+ return INHERITANCE_ERROR ;
665
+ }
666
+ if (status != INHERITANCE_SUCCESS ) {
667
+ ZEND_ASSERT (status == INHERITANCE_UNRESOLVED );
668
+ parent_union_has_unresolved = true;
669
+ all_success = false;
670
+ }
658
671
}
659
672
} ZEND_TYPE_FOREACH_END ();
673
+
674
+ /* Reaching this means either the child intersection type is a valid/unresolved
675
+ * subtype of a parent single/intersection type, either it is an INvalid subtype
676
+ * when the parent is a union or it is unresolved, we check which case this is */
677
+ if (ZEND_TYPE_IS_UNION (proto_type ) && !parent_union_has_unresolved ) {
678
+ return tentative ? INHERITANCE_WARNING : INHERITANCE_ERROR ;
679
+ }
660
680
} else {
661
681
/* First try to check whether we can succeed without resolving anything */
662
682
ZEND_TYPE_FOREACH (fe_type , single_type ) {
0 commit comments