44 *
55 * The @phpstan-assert tags of a function or method: a final class over the
66 * tag list with a private constructor, the empty instance held where the
7- * twin keeps it (the private static $empty), the filters and mapTypes() over
8- * the PHP AssertTag objects through one cached method site per tag method.
9- * The twin's array_filter() / array_map() closures are native loops with the
10- * closures' AssertTag parameter check. Native callers use the
7+ * twin keeps it (the private static $empty), and loops over the PHP AssertTag
8+ * objects through one cached method site per tag method. Native callers use the
119 * pt_assertions_* entries (support.h) and the inline pt_assertions_all()
1210 * reader.
1311 */
@@ -144,20 +142,20 @@ class Assertions
144142 return filterByIf (asserts, " " , 0 , false , PT_AS_CLOSURE (" getAsserts" , " 58" ));
145143 }
146144
147- /* array_merge(the IF_TRUE tags, the negated IF_FALSE non-equality tags) */
145+ /* foreach: collect IF_TRUE tags and negated IF_FALSE non-equality tags */
148146 zv::Val getAssertsIfTrue () const
149147 {
150148 zval *asserts = slot ();
151149 if (UNEXPECTED (asserts == NULL )) return zv::Val ();
152- return mergedWithNegated (asserts, PT_LC (" true" ), PT_AS_CLOSURE ( " getAssertsIfTrue " , " 69 " ), PT_LC (" false" ), PT_AS_CLOSURE ( " getAssertsIfTrue " , " 72 " ), PT_AS_CLOSURE ( " getAssertsIfTrue " , " 71 " ));
150+ return assertsIf (asserts, PT_LC (" true" ), PT_LC (" false" ));
153151 }
154152
155- /* array_merge(the IF_FALSE tags, the negated IF_TRUE non-equality tags) */
153+ /* foreach: collect IF_FALSE tags and negated IF_TRUE non-equality tags */
156154 zv::Val getAssertsIfFalse () const
157155 {
158156 zval *asserts = slot ();
159157 if (UNEXPECTED (asserts == NULL )) return zv::Val ();
160- return mergedWithNegated (asserts, PT_LC (" false" ), PT_AS_CLOSURE ( " getAssertsIfFalse " , " 85 " ), PT_LC (" true" ), PT_AS_CLOSURE ( " getAssertsIfFalse " , " 88 " ), PT_AS_CLOSURE ( " getAssertsIfFalse " , " 87 " ));
158+ return assertsIf (asserts, PT_LC (" false" ), PT_LC (" true" ));
161159 }
162160
163161 /* self::create(array_map(static fn (AssertTag $tag): AssertTag =>
@@ -170,7 +168,7 @@ class Assertions
170168 zv::Arr mapped = zv::Arr::create (zend_hash_num_elements (table));
171169 for (zv::ArrayEntry entry : zv::TableRef (table)) {
172170 zval *tag = entry.value ().deref ().raw ();
173- if (UNEXPECTED (!requireAssertTag (tag, PT_AS_CLOSURE (" mapTypes" , " 96 " ), " tag" ))) return zv::Val ();
171+ if (UNEXPECTED (!requireAssertTag (tag, PT_AS_CLOSURE (" mapTypes" , " 104 " ), " tag" ))) return zv::Val ();
174172 zv::Val type = tagCall (pt_as_get_type_site, tag, PT_LC (" gettype" ));
175173 if (UNEXPECTED (type.isUndef ())) return zv::Val ();
176174 zv::Val mappedType = pt_type_call_callable (callable, 1 , type.raw ());
@@ -353,24 +351,34 @@ class Assertions
353351 return zv::Val (std::move (filtered));
354352 }
355353
356- /* array_merge(array_filter($asserts, fn getIf() === $own),
357- * array_map(static fn (AssertTag $assert) => $assert->negate(),
358- * array_filter($asserts, fn getIf() === $opposite && !isEquality())) */
359- static zv::Val mergedWithNegated (zval *asserts, const char *own, size_t ownLen, const char *ownClosure, const char *opposite, size_t oppositeLen, const char *oppositeClosure, const char *negateClosure)
354+ /* Mirrors the two-branch foreach in getAssertsIfTrue() / getAssertsIfFalse(). */
355+ static zv::Val assertsIf (zval *asserts, const char *own, size_t ownLen, const char *opposite, size_t oppositeLen)
360356 {
361- zv::Val ownTags = filterByIf (asserts, own, ownLen, false , ownClosure);
362- if (UNEXPECTED (ownTags.isUndef ())) return zv::Val ();
363- zv::Val oppositeTags = filterByIf (asserts, opposite, oppositeLen, true , oppositeClosure);
364- if (UNEXPECTED (oppositeTags.isUndef ())) return zv::Val ();
365- zv::Arr negated = zv::Arr::create (zend_hash_num_elements (Z_ARRVAL_P (oppositeTags.raw ())));
366- for (zv::ArrayEntry entry : zv::ArrRef (oppositeTags.raw ())) {
357+ HashTable *table = Z_ARRVAL_P (asserts);
358+ zv::Arr ownTags = zv::Arr::create (0 );
359+ zv::Arr negatedTags = zv::Arr::create (0 );
360+ for (zv::ArrayEntry entry : zv::TableRef (table)) {
367361 zval *tag = entry.value ().deref ().raw ();
368- if (UNEXPECTED (!requireAssertTag (tag, negateClosure, " assert" ))) return zv::Val ();
369- zv::Val negatedTag = tagCall (pt_as_negate_site, tag, PT_LC (" negate" ));
370- if (UNEXPECTED (negatedTag.isUndef ())) return zv::Val ();
371- setKeyed (negated, entry, std::move (negatedTag));
362+ if (UNEXPECTED (Z_TYPE_P (tag) != IS_OBJECT )) {
363+ zend_throw_error (NULL , " Call to a member function getIf() on %s" , zend_zval_value_name (tag));
364+ return zv::Val ();
365+ }
366+ bool matches;
367+ if (UNEXPECTED (!tagIfIs (tag, own, ownLen, matches))) return zv::Val ();
368+ if (matches) {
369+ ownTags.push (zv::Ref (entry.value ().deref ().raw ()));
370+ } else {
371+ if (UNEXPECTED (!tagIfIs (tag, opposite, oppositeLen, matches))) return zv::Val ();
372+ if (!matches) continue ;
373+ bool equality;
374+ if (UNEXPECTED (!tagBool (pt_as_is_equality_site, tag, PT_LC (" isequality" ), equality))) return zv::Val ();
375+ if (equality) continue ;
376+ zv::Val negatedTag = tagCall (pt_as_negate_site, tag, PT_LC (" negate" ));
377+ if (UNEXPECTED (negatedTag.isUndef ())) return zv::Val ();
378+ negatedTags.push (std::move (negatedTag));
379+ }
372380 }
373- zv::Val negatedValue (std::move (negated ));
381+ zv::Val negatedValue (std::move (negatedTags ));
374382 return ptcall::arrayMerge (ownTags.raw (), negatedValue.raw ());
375383 }
376384};
0 commit comments