Skip to content

Commit 4c5767f

Browse files
authored
PHP 8.4 | Exit as function: fix incorrect parameter name (#15433)
Follow up on 13483 As previously reported in #13483 (comment): > The parameter names seem to be incorrect. > > It should be `$status`, not `$code`. > > The RFC explicitly uses that parameter name in the proposal: https://wiki.php.net/rfc/exit-as-function#proposal > > It is also the name already used in the [manual](https://www.php.net/exit). > > Lastly, the parameter name `$status` better covers what can be passed: either a status _message_ or a status _code_. > While `$code` would read pretty weird when passing a message: > ```php > exit(code: 'message'); > ``` This commit attempts to fix this. Includes adding a test for exit/die using a named argument. Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent f0d0293 commit 4c5767f

7 files changed

+79
-20
lines changed

Zend/tests/exit/die_string_cast_exception.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111

1212
?>
1313
--EXPECT--
14-
exit(): Argument #1 ($code) must be of type string|int, stdClass given
14+
exit(): Argument #1 ($status) must be of type string|int, stdClass given

Zend/tests/exit/exit_as_function.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object(Closure)#1 (2) {
2727
string(4) "exit"
2828
["parameter"]=>
2929
array(1) {
30-
["$code"]=>
30+
["$status"]=>
3131
string(10) "<optional>"
3232
}
3333
}
@@ -36,7 +36,7 @@ object(Closure)#2 (2) {
3636
string(4) "exit"
3737
["parameter"]=>
3838
array(1) {
39-
["$code"]=>
39+
["$status"]=>
4040
string(10) "<optional>"
4141
}
4242
}

Zend/tests/exit/exit_named_arg.phpt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
Using exit()/die() as function call with a named argument
3+
--FILE--
4+
<?php
5+
6+
$values = [
7+
12,
8+
"Goodbye!",
9+
];
10+
11+
const FILE_PATH = __DIR__ . '/exit_named_arg_test.php';
12+
const FILE_CONTENT = <<<'TEMPLATE'
13+
<?php
14+
try {
15+
exit(status: VALUE);
16+
} catch (\Throwable $e) {
17+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
18+
}
19+
20+
TEMPLATE;
21+
22+
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
23+
$command = $php . ' --no-php-ini ' . escapeshellarg(FILE_PATH);
24+
25+
foreach ([FILE_CONTENT, str_replace('exit', 'die', FILE_CONTENT)] as $code) {
26+
foreach ($values as $value) {
27+
echo 'Using ', var_export($value, true), ' as value:', PHP_EOL;
28+
$output = [];
29+
$content = str_replace('VALUE', var_export($value, true), $code);
30+
file_put_contents(FILE_PATH, $content);
31+
exec($command, $output, $exit_status);
32+
echo 'Exit status is: ', $exit_status, PHP_EOL,
33+
'Output is:', PHP_EOL, join($output), PHP_EOL;
34+
}
35+
}
36+
37+
?>
38+
--CLEAN--
39+
<?php
40+
const FILE_PATH = __DIR__ . '/exit_named_arg_test.php';
41+
@unlink(FILE_PATH);
42+
?>
43+
--EXPECT--
44+
Using 12 as value:
45+
Exit status is: 12
46+
Output is:
47+
48+
Using 'Goodbye!' as value:
49+
Exit status is: 0
50+
Output is:
51+
Goodbye!
52+
Using 12 as value:
53+
Exit status is: 12
54+
Output is:
55+
56+
Using 'Goodbye!' as value:
57+
Exit status is: 0
58+
Output is:
59+
Goodbye!

Zend/tests/exit/exit_values.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const FILE_PATH = __DIR__ . '/exit_values_test.php';
8080
Using NULL as value:
8181
Exit status is: 0
8282
Output is:
83-
Deprecated: exit(): Passing null to parameter #1 ($code) of type string|int is deprecated in %s on line %d
83+
Deprecated: exit(): Passing null to parameter #1 ($status) of type string|int is deprecated in %s on line %d
8484
Using false as value:
8585
Exit status is: 0
8686
Output is:
@@ -116,23 +116,23 @@ Hello world
116116
Using [] as value:
117117
Exit status is: 0
118118
Output is:
119-
TypeError: exit(): Argument #1 ($code) must be of type string|int, array given
119+
TypeError: exit(): Argument #1 ($status) must be of type string|int, array given
120120
Using STDERR as value:
121121
Exit status is: 0
122122
Output is:
123-
TypeError: exit(): Argument #1 ($code) must be of type string|int, resource given
123+
TypeError: exit(): Argument #1 ($status) must be of type string|int, resource given
124124
Using new stdClass() as value:
125125
Exit status is: 0
126126
Output is:
127-
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
127+
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
128128
As a statement:
129129
Exit status is: 0
130130
Output is:
131-
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
131+
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
132132
Using NULL as value:
133133
Exit status is: 0
134134
Output is:
135-
Deprecated: exit(): Passing null to parameter #1 ($code) of type string|int is deprecated in %s on line %d
135+
Deprecated: exit(): Passing null to parameter #1 ($status) of type string|int is deprecated in %s on line %d
136136
Using false as value:
137137
Exit status is: 0
138138
Output is:
@@ -168,16 +168,16 @@ Hello world
168168
Using [] as value:
169169
Exit status is: 0
170170
Output is:
171-
TypeError: exit(): Argument #1 ($code) must be of type string|int, array given
171+
TypeError: exit(): Argument #1 ($status) must be of type string|int, array given
172172
Using STDERR as value:
173173
Exit status is: 0
174174
Output is:
175-
TypeError: exit(): Argument #1 ($code) must be of type string|int, resource given
175+
TypeError: exit(): Argument #1 ($status) must be of type string|int, resource given
176176
Using new stdClass() as value:
177177
Exit status is: 0
178178
Output is:
179-
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
179+
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
180180
As a statement:
181181
Exit status is: 0
182182
Output is:
183-
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
183+
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given

Zend/zend_builtin_functions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ zend_result zend_startup_builtin_functions(void) /* {{{ */
7272
ZEND_FUNCTION(exit)
7373
{
7474
zend_string *str = NULL;
75-
zend_long code = 0;
75+
zend_long status = 0;
7676

7777
ZEND_PARSE_PARAMETERS_START(0, 1)
7878
Z_PARAM_OPTIONAL
79-
Z_PARAM_STR_OR_LONG(str, code)
79+
Z_PARAM_STR_OR_LONG(str, status)
8080
ZEND_PARSE_PARAMETERS_END();
8181

8282
if (str) {
@@ -89,7 +89,7 @@ ZEND_FUNCTION(exit)
8989
}
9090
}
9191
} else {
92-
EG(exit_status) = code;
92+
EG(exit_status) = status;
9393
}
9494

9595
ZEND_ASSERT(!EG(exception));

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class stdClass
77
{
88
}
99

10-
function exit(string|int $code = 0): never {}
10+
function exit(string|int $status = 0): never {}
1111

1212
/** @alias exit */
13-
function die(string|int $code = 0): never {}
13+
function die(string|int $status = 0): never {}
1414

1515
/** @refcount 1 */
1616
function zend_version(): string {}

Zend/zend_builtin_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)