@@ -7321,12 +7321,6 @@ ZEND_METHOD(ReflectionAttribute, newInstance)
7321
7321
RETURN_THROWS ();
7322
7322
}
7323
7323
7324
- zend_attribute * delayed_target_validation = zend_get_attribute_str (
7325
- attr -> attributes ,
7326
- "delayedtargetvalidation" ,
7327
- strlen ("delayedtargetvalidation" )
7328
- );
7329
-
7330
7324
/* This code can be reached under one of three possible conditions:
7331
7325
* - the attribute is an internal attribute, and it had the target and
7332
7326
* and repetition validated already
@@ -7360,15 +7354,23 @@ ZEND_METHOD(ReflectionAttribute, newInstance)
7360
7354
RETURN_THROWS ();
7361
7355
}
7362
7356
7363
- /* Report the delayed validator error for internal attributes */
7364
- if (delayed_target_validation && ce -> type == ZEND_INTERNAL_CLASS ) {
7365
- zend_string * error = attr -> data -> validation_error ;
7366
- if (error != NULL ) {
7367
- zend_throw_exception (zend_ce_error , ZSTR_VAL (error ), 0 );
7368
- RETURN_THROWS ();
7369
- }
7370
- } else {
7371
- ZEND_ASSERT (attr -> data -> validation_error == NULL );
7357
+ if (attr -> data -> validation_error != NULL ) {
7358
+ /* Delayed validation errors should only be set for internal attributes. */
7359
+ ZEND_ASSERT (ce -> type == ZEND_INTERNAL_CLASS );
7360
+ /* Delayed validation errors should only be set when
7361
+ * #[\DelayedTargetValidation] is used. Searching for the attribute is
7362
+ * more expensive than just an assertion and so we don't worry about it
7363
+ * for non-debug builds. See discussion on GH-18817. */
7364
+ #if ZEND_DEBUG
7365
+ zend_attribute * delayed_target_validation = zend_get_attribute_str (
7366
+ attr -> attributes ,
7367
+ "delayedtargetvalidation" ,
7368
+ strlen ("delayedtargetvalidation" )
7369
+ );
7370
+ ZEND_ASSERT (delayed_target_validation != NULL );
7371
+ #endif
7372
+ zend_throw_exception (zend_ce_error , ZSTR_VAL (attr -> data -> validation_error ), 0 );
7373
+ RETURN_THROWS ();
7372
7374
}
7373
7375
7374
7376
/* Repetition validation is done even if #[DelayedTargetValidation] is used
0 commit comments