@@ -53,11 +53,13 @@ bool pt_result_object_create(zval *out, zend_class_entry *ce, zval *trinary, zva
5353#define PT_CAT_CASE_LOWER 0
5454#define PT_CAT_CASE_UPPER 1
5555
56- /* isValidIdentifier()'s pattern (interned once) and the internal
57- * preg_match() it is matched with (resolved once; the pcre extension
58- * caches the compiled pattern per request) */
56+ /* isValidIdentifier()'s pattern (a permanent interned string, made at
57+ * MINIT) and the internal preg_match() / preg_last_error() it is matched
58+ * with (internal functions, resolved once; the pcre extension caches the
59+ * compiled pattern per request) */
5960static zend_string *pt_carr_identifier_regex = nullptr ;
6061static zend_function *pt_carr_preg_match = nullptr ;
62+ static zend_function *pt_carr_preg_last_error = nullptr ;
6163
6264namespace phpstanturbo {
6365
@@ -5678,12 +5680,10 @@ class ConstantArrayType
56785680 * frames; false = pending exception */
56795681 [[nodiscard]] static bool isValidIdentifier (zend_string *value, bool &out)
56805682 {
5681- if (UNEXPECTED (pt_carr_identifier_regex == nullptr )) {
5682- pt_carr_identifier_regex = zend_string_init_interned (PT_LC (" ~^(?:[\\\\ ]?+[a-z_\\ x80-\\ xFF][0-9a-z_\\ x80-\\ xFF-]*+)++$~si" ), 1 );
5683- }
5684- if (UNEXPECTED (pt_carr_preg_match == nullptr )) {
5683+ if (UNEXPECTED (pt_carr_preg_match == nullptr || pt_carr_preg_last_error == nullptr )) {
56855684 pt_carr_preg_match = (zend_function *) zend_hash_str_find_ptr (EG (function_table), PT_LC (" preg_match" ));
5686- if (UNEXPECTED (pt_carr_preg_match == nullptr )) {
5685+ pt_carr_preg_last_error = (zend_function *) zend_hash_str_find_ptr (EG (function_table), PT_LC (" preg_last_error" ));
5686+ if (UNEXPECTED (pt_carr_preg_match == nullptr || pt_carr_preg_last_error == nullptr )) {
56875687 zend_throw_error (NULL , " phpstan_turbo: preg_match() is not available" );
56885688 return false ;
56895689 }
@@ -5697,19 +5697,48 @@ class ConstantArrayType
56975697 return false ;
56985698 }
56995699 if (UNEXPECTED (Z_TYPE (matched) != IS_LONG )) {
5700- /* preg_match() failed (a backtracking/recursion limit) — Nette
5701- * Strings::match() raises its RegexpException here */
57025700 zval_ptr_dtor (&matched);
5703- zend_class_entry *exceptionCe = pt_class (PT_CLASS_NETTE_REGEXP_EXCEPTION );
5704- if (exceptionCe != NULL ) {
5705- zend_throw_exception (exceptionCe, " preg_match() failed" , 0 );
5706- }
5701+ throwRegexpException ();
57075702 return false ;
57085703 }
57095704 out = Z_LVAL (matched) > 0 ;
57105705 return true ;
57115706 }
57125707
5708+ /* preg_match() failed (a backtracking/recursion limit): Nette's
5709+ * Strings::pcre() throws new RegexpException((RegexpException::MESSAGES[$code]
5710+ * ?? 'Unknown error') . ' (pattern: ' . $pattern . ')', $code) with
5711+ * $code = preg_last_error() */
5712+ static void throwRegexpException ()
5713+ {
5714+ zend_class_entry *exceptionCe = pt_class (PT_CLASS_NETTE_REGEXP_EXCEPTION );
5715+ if (UNEXPECTED (exceptionCe == NULL )) return ;
5716+ zval code;
5717+ zend_call_known_function (pt_carr_preg_last_error, NULL , NULL , &code, 0 , NULL , NULL );
5718+ if (UNEXPECTED (EG (exception))) return ;
5719+ zend_long codeValue = Z_TYPE (code) == IS_LONG ? Z_LVAL (code) : 0 ;
5720+ zval_ptr_dtor (&code);
5721+
5722+ const char *reason = " Unknown error" ;
5723+ size_t reasonLength = sizeof (" Unknown error" ) - 1 ;
5724+ zend_string *constantName = zend_string_init (PT_LC (" MESSAGES" ), 0 );
5725+ zval *messages = zend_get_class_constant_ex (exceptionCe->name , constantName, exceptionCe, ZEND_FETCH_CLASS_SILENT );
5726+ zend_string_release (constantName);
5727+ if (UNEXPECTED (EG (exception))) return ;
5728+ if (messages != NULL && Z_TYPE_P (messages) == IS_ARRAY ) {
5729+ zval *message = zend_hash_index_find (Z_ARRVAL_P (messages), (zend_ulong) codeValue);
5730+ if (message != NULL && Z_TYPE_P (message) == IS_STRING ) {
5731+ reason = Z_STRVAL_P (message);
5732+ reasonLength = Z_STRLEN_P (message);
5733+ }
5734+ }
5735+ zend_string *text = zend_string_concat3 (reason, reasonLength, PT_LC (" (pattern: " ), ZSTR_VAL (pt_carr_identifier_regex), ZSTR_LEN (pt_carr_identifier_regex));
5736+ zend_string *full = zend_string_concat2 (ZSTR_VAL (text), ZSTR_LEN (text), PT_LC (" )" ));
5737+ zend_string_release (text);
5738+ zend_throw_exception (exceptionCe, ZSTR_VAL (full), codeValue);
5739+ zend_string_release (full);
5740+ }
5741+
57135742 /* [] for an unsealed shape; else every combination of the values'
57145743 * finite types (an optional key forking a without-variant), built one
57155744 * key at a time and bounded by CALCULATE_SCALARS_LIMIT partial shapes;
@@ -7170,6 +7199,8 @@ static void ZEND_FASTCALL catTraverse(INTERNAL_FUNCTION_PARAMETERS)
71707199
71717200void pt_register_constant_array_type ()
71727201{
7202+ pt_carr_identifier_regex = zend_string_init_interned (PT_LC (" ~^(?:[\\\\ ]?+[a-z_\\ x80-\\ xFF][0-9a-z_\\ x80-\\ xFF-]*+)++$~si" ), 1 );
7203+
71737204 reg::Class cls (" PHPStan\\ Type\\ Constant\\ ConstantArrayType" );
71747205 ptdecl::ConstantArrayType::declareClass (cls);
71757206 /* the slots PT_CAT_PROP_*: the eight class-body properties first, the
0 commit comments