Skip to content

Commit b496e50

Browse files
committed
Add support for declaring class entries in stubs
1 parent 9719d6c commit b496e50

33 files changed

+895
-159
lines changed

Zend/zend_exceptions.c

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -734,87 +734,49 @@ ZEND_METHOD(Exception, __toString)
734734
}
735735
/* }}} */
736736

737-
static void declare_exception_properties(zend_class_entry *ce)
738-
{
739-
zval val;
740-
741-
zend_declare_property_string(ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
742-
zend_declare_property_string(ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
743-
zend_declare_property_long(ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
744-
zend_declare_property_null(ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
745-
zend_declare_property_null(ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
746-
747-
ZVAL_EMPTY_ARRAY(&val);
748-
zend_declare_typed_property(
749-
ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL,
750-
(zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
751-
752-
ZVAL_NULL(&val);
753-
zend_declare_typed_property(
754-
ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL,
755-
(zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0));
756-
}
757-
758737
void zend_register_default_exception(void) /* {{{ */
759738
{
760-
zend_class_entry ce;
761-
762-
REGISTER_MAGIC_INTERFACE(throwable, Throwable);
763-
zend_class_implements(zend_ce_throwable, 1, zend_ce_stringable);
739+
register_class_Throwable(zend_ce_throwable, zend_ce_stringable);
740+
zend_ce_throwable->interface_gets_implemented = zend_implement_throwable;
764741

765742
memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
766743
default_exception_handlers.clone_obj = NULL;
767744

768-
INIT_CLASS_ENTRY(ce, "Exception", class_Exception_methods);
769-
zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
745+
register_class_Exception(zend_ce_exception, zend_ce_throwable);
770746
zend_ce_exception->create_object = zend_default_exception_new;
771-
zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
772-
declare_exception_properties(zend_ce_exception);
773747

774-
INIT_CLASS_ENTRY(ce, "ErrorException", class_ErrorException_methods);
775-
zend_ce_error_exception = zend_register_internal_class_ex(&ce, zend_ce_exception);
748+
register_class_ErrorException(zend_ce_error_exception, zend_ce_exception);
776749
zend_ce_error_exception->create_object = zend_error_exception_new;
777750
zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
778751

779-
INIT_CLASS_ENTRY(ce, "Error", class_Error_methods);
780-
zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
752+
register_class_Error(zend_ce_error, zend_ce_throwable);
781753
zend_ce_error->create_object = zend_default_exception_new;
782-
zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
783-
declare_exception_properties(zend_ce_error);
784754

785-
INIT_CLASS_ENTRY(ce, "CompileError", class_CompileError_methods);
786-
zend_ce_compile_error = zend_register_internal_class_ex(&ce, zend_ce_error);
755+
register_class_CompileError(zend_ce_compile_error, zend_ce_error);
787756
zend_ce_compile_error->create_object = zend_default_exception_new;
788757

789-
INIT_CLASS_ENTRY(ce, "ParseError", class_ParseError_methods);
790-
zend_ce_parse_error = zend_register_internal_class_ex(&ce, zend_ce_compile_error);
758+
register_class_ParseError(zend_ce_parse_error, zend_ce_compile_error);
791759
zend_ce_parse_error->create_object = zend_default_exception_new;
792760

793-
INIT_CLASS_ENTRY(ce, "TypeError", class_TypeError_methods);
794-
zend_ce_type_error = zend_register_internal_class_ex(&ce, zend_ce_error);
761+
register_class_TypeError(zend_ce_type_error, zend_ce_error);
795762
zend_ce_type_error->create_object = zend_default_exception_new;
796763

797-
INIT_CLASS_ENTRY(ce, "ArgumentCountError", class_ArgumentCountError_methods);
798-
zend_ce_argument_count_error = zend_register_internal_class_ex(&ce, zend_ce_type_error);
764+
register_class_ArgumentCountError(zend_ce_argument_count_error, zend_ce_type_error);
799765
zend_ce_argument_count_error->create_object = zend_default_exception_new;
800766

801-
INIT_CLASS_ENTRY(ce, "ValueError", class_ValueError_methods);
802-
zend_ce_value_error = zend_register_internal_class_ex(&ce, zend_ce_error);
767+
register_class_ValueError(zend_ce_value_error, zend_ce_error);
803768
zend_ce_value_error->create_object = zend_default_exception_new;
804769

805-
INIT_CLASS_ENTRY(ce, "ArithmeticError", class_ArithmeticError_methods);
806-
zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_ce_error);
770+
register_class_ArithmeticError(zend_ce_arithmetic_error, zend_ce_error);
807771
zend_ce_arithmetic_error->create_object = zend_default_exception_new;
808772

809-
INIT_CLASS_ENTRY(ce, "DivisionByZeroError", class_DivisionByZeroError_methods);
810-
zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error);
773+
register_class_DivisionByZeroError(zend_ce_division_by_zero_error, zend_ce_arithmetic_error);
811774
zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
812775

813-
INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
814-
815-
INIT_CLASS_ENTRY(ce, "UnhandledMatchError", NULL);
816-
zend_ce_unhandled_match_error = zend_register_internal_class_ex(&ce, zend_ce_error);
776+
register_class_UnhandledMatchError(zend_ce_unhandled_match_error, zend_ce_error);
817777
zend_ce_unhandled_match_error->create_object = zend_default_exception_new;
778+
779+
INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
818780
}
819781
/* }}} */
820782

Zend/zend_exceptions.stub.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
/** @generate-function-entries */
3+
/**
4+
* @generate-function-entries
5+
* @generate-class-entries
6+
*/
47

58
interface Throwable extends Stringable
69
{
@@ -22,6 +25,21 @@ public function getTraceAsString(): string;
2225

2326
class Exception implements Throwable
2427
{
28+
/** @var string */
29+
protected $message = "";
30+
/** @var string */
31+
private $string = "";
32+
/** @var int */
33+
protected $code = 0;
34+
/** @var string|null */
35+
protected $file = null;
36+
/** @var int|null */
37+
protected $line = null;
38+
/** @known */
39+
private array $trace = [];
40+
/** @known */
41+
private ?Throwable $previous = null;
42+
2543
final private function __clone() {}
2644

2745
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {}
@@ -48,13 +66,38 @@ public function __toString(): string {}
4866

4967
class ErrorException extends Exception
5068
{
51-
public function __construct(string $message = "", int $code = 0, int $severity = E_ERROR, ?string $filename = null, ?int $line = null, ?Throwable $previous = null) {}
69+
/** @var int */
70+
protected $severity = E_ERROR;
71+
72+
public function __construct(
73+
string $message = "",
74+
int $code = 0,
75+
int $severity = E_ERROR,
76+
?string $filename = null,
77+
?int $line = null,
78+
?Throwable $previous = null
79+
) {}
5280

5381
final public function getSeverity(): int {}
5482
}
5583

5684
class Error implements Throwable
5785
{
86+
/** @var string */
87+
protected $message = "";
88+
/** @var string */
89+
private $string = "";
90+
/** @var int */
91+
protected $code = 0;
92+
/** @var string|null */
93+
protected $file = null;
94+
/** @var int|null */
95+
protected $line = null;
96+
/** @known */
97+
private array $trace = [];
98+
/** @known */
99+
private ?Throwable $previous = null;
100+
58101
/** @implementation-alias Exception::__clone */
59102
final private function __clone() {}
60103

@@ -119,3 +162,7 @@ class ArithmeticError extends Error
119162
class DivisionByZeroError extends ArithmeticError
120163
{
121164
}
165+
166+
class UnhandledMatchError extends Error
167+
{
168+
}

Zend/zend_exceptions_arginfo.h

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: bc49b326136997660887b12f0c59f8a57b17ecaf */
2+
* Stub hash: 8593bcd9dd8350f84533b9592c3d6a2885e97d07 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()
@@ -179,3 +179,143 @@ static const zend_function_entry class_ArithmeticError_methods[] = {
179179
static const zend_function_entry class_DivisionByZeroError_methods[] = {
180180
ZEND_FE_END
181181
};
182+
183+
184+
static const zend_function_entry class_UnhandledMatchError_methods[] = {
185+
ZEND_FE_END
186+
};
187+
188+
#define register_class_Throwable(class_entry, class_entry_Stringable) \
189+
{ \
190+
zend_class_entry ce; \
191+
\
192+
INIT_CLASS_ENTRY(ce, "Throwable", class_Throwable_methods); \
193+
class_entry = zend_register_internal_interface(&ce); \
194+
}
195+
196+
#define register_class_Exception(class_entry, class_entry_Throwable) \
197+
{ \
198+
zend_class_entry ce; \
199+
\
200+
INIT_CLASS_ENTRY(ce, "Exception", class_Exception_methods); \
201+
class_entry = zend_register_internal_class_ex(&ce, NULL); \
202+
zend_class_implements(class_entry, 1, class_entry_Throwable); \
203+
\
204+
zend_declare_property_string(class_entry, "message", sizeof("message") - 1, "", ZEND_ACC_PROTECTED); \
205+
\
206+
zend_declare_property_string(class_entry, "string", sizeof("string") - 1, "", ZEND_ACC_PRIVATE); \
207+
\
208+
zend_declare_property_long(class_entry, "code", sizeof("code") - 1, 0, ZEND_ACC_PROTECTED); \
209+
\
210+
zend_declare_property_null(class_entry, "file", sizeof("file") - 1, ZEND_ACC_PROTECTED); \
211+
\
212+
zend_declare_property_null(class_entry, "line", sizeof("line") - 1, ZEND_ACC_PROTECTED); \
213+
\
214+
zval property_trace_default_value; \
215+
ZVAL_EMPTY_ARRAY(&property_trace_default_value); \
216+
zend_declare_typed_property(class_entry, ZSTR_KNOWN(ZEND_STR_TRACE), &property_trace_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY)); \
217+
\
218+
zval property_previous_default_value; \
219+
ZVAL_NULL(&property_previous_default_value); \
220+
zend_declare_typed_property(class_entry, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &property_previous_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_CE(class_entry_Throwable, 1, 0)); \
221+
}
222+
223+
#define register_class_ErrorException(class_entry, class_entry_Exception) \
224+
{ \
225+
zend_class_entry ce; \
226+
\
227+
INIT_CLASS_ENTRY(ce, "ErrorException", class_ErrorException_methods); \
228+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Exception); \
229+
}
230+
231+
#define register_class_Error(class_entry, class_entry_Throwable) \
232+
{ \
233+
zend_class_entry ce; \
234+
\
235+
INIT_CLASS_ENTRY(ce, "Error", class_Error_methods); \
236+
class_entry = zend_register_internal_class_ex(&ce, NULL); \
237+
zend_class_implements(class_entry, 1, class_entry_Throwable); \
238+
\
239+
zend_declare_property_string(class_entry, "message", sizeof("message") - 1, "", ZEND_ACC_PROTECTED); \
240+
\
241+
zend_declare_property_string(class_entry, "string", sizeof("string") - 1, "", ZEND_ACC_PRIVATE); \
242+
\
243+
zend_declare_property_long(class_entry, "code", sizeof("code") - 1, 0, ZEND_ACC_PROTECTED); \
244+
\
245+
zend_declare_property_null(class_entry, "file", sizeof("file") - 1, ZEND_ACC_PROTECTED); \
246+
\
247+
zend_declare_property_null(class_entry, "line", sizeof("line") - 1, ZEND_ACC_PROTECTED); \
248+
\
249+
zval property_trace_default_value; \
250+
ZVAL_EMPTY_ARRAY(&property_trace_default_value); \
251+
zend_declare_typed_property(class_entry, ZSTR_KNOWN(ZEND_STR_TRACE), &property_trace_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY)); \
252+
\
253+
zval property_previous_default_value; \
254+
ZVAL_NULL(&property_previous_default_value); \
255+
zend_declare_typed_property(class_entry, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &property_previous_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_CE(class_entry_Throwable, 1, 0)); \
256+
}
257+
258+
#define register_class_CompileError(class_entry, class_entry_Error) \
259+
{ \
260+
zend_class_entry ce; \
261+
\
262+
INIT_CLASS_ENTRY(ce, "CompileError", class_CompileError_methods); \
263+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Error); \
264+
}
265+
266+
#define register_class_ParseError(class_entry, class_entry_CompileError) \
267+
{ \
268+
zend_class_entry ce; \
269+
\
270+
INIT_CLASS_ENTRY(ce, "ParseError", class_ParseError_methods); \
271+
class_entry = zend_register_internal_class_ex(&ce, class_entry_CompileError); \
272+
}
273+
274+
#define register_class_TypeError(class_entry, class_entry_Error) \
275+
{ \
276+
zend_class_entry ce; \
277+
\
278+
INIT_CLASS_ENTRY(ce, "TypeError", class_TypeError_methods); \
279+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Error); \
280+
}
281+
282+
#define register_class_ArgumentCountError(class_entry, class_entry_TypeError) \
283+
{ \
284+
zend_class_entry ce; \
285+
\
286+
INIT_CLASS_ENTRY(ce, "ArgumentCountError", class_ArgumentCountError_methods); \
287+
class_entry = zend_register_internal_class_ex(&ce, class_entry_TypeError); \
288+
}
289+
290+
#define register_class_ValueError(class_entry, class_entry_Error) \
291+
{ \
292+
zend_class_entry ce; \
293+
\
294+
INIT_CLASS_ENTRY(ce, "ValueError", class_ValueError_methods); \
295+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Error); \
296+
}
297+
298+
#define register_class_ArithmeticError(class_entry, class_entry_Error) \
299+
{ \
300+
zend_class_entry ce; \
301+
\
302+
INIT_CLASS_ENTRY(ce, "ArithmeticError", class_ArithmeticError_methods); \
303+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Error); \
304+
}
305+
306+
#define register_class_DivisionByZeroError(class_entry, class_entry_ArithmeticError) \
307+
{ \
308+
zend_class_entry ce; \
309+
\
310+
INIT_CLASS_ENTRY(ce, "DivisionByZeroError", class_DivisionByZeroError_methods); \
311+
class_entry = zend_register_internal_class_ex(&ce, class_entry_ArithmeticError); \
312+
}
313+
314+
#define register_class_UnhandledMatchError(class_entry, class_entry_Error) \
315+
{ \
316+
zend_class_entry ce; \
317+
\
318+
INIT_CLASS_ENTRY(ce, "UnhandledMatchError", class_UnhandledMatchError_methods); \
319+
class_entry = zend_register_internal_class_ex(&ce, class_entry_Error); \
320+
}
321+

0 commit comments

Comments
 (0)