Skip to content

Commit

Permalink
PHP 8.4 | Exit as function: fix incorrect parameter name (#15433)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jrfnl and jrfnl authored Aug 16, 2024
1 parent f0d0293 commit 4c5767f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/exit/die_string_cast_exception.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ try {

?>
--EXPECT--
exit(): Argument #1 ($code) must be of type string|int, stdClass given
exit(): Argument #1 ($status) must be of type string|int, stdClass given
4 changes: 2 additions & 2 deletions Zend/tests/exit/exit_as_function.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object(Closure)#1 (2) {
string(4) "exit"
["parameter"]=>
array(1) {
["$code"]=>
["$status"]=>
string(10) "<optional>"
}
}
Expand All @@ -36,7 +36,7 @@ object(Closure)#2 (2) {
string(4) "exit"
["parameter"]=>
array(1) {
["$code"]=>
["$status"]=>
string(10) "<optional>"
}
}
59 changes: 59 additions & 0 deletions Zend/tests/exit/exit_named_arg.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Using exit()/die() as function call with a named argument
--FILE--
<?php

$values = [
12,
"Goodbye!",
];

const FILE_PATH = __DIR__ . '/exit_named_arg_test.php';
const FILE_CONTENT = <<<'TEMPLATE'
<?php
try {
exit(status: VALUE);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

TEMPLATE;

$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$command = $php . ' --no-php-ini ' . escapeshellarg(FILE_PATH);

foreach ([FILE_CONTENT, str_replace('exit', 'die', FILE_CONTENT)] as $code) {
foreach ($values as $value) {
echo 'Using ', var_export($value, true), ' as value:', PHP_EOL;
$output = [];
$content = str_replace('VALUE', var_export($value, true), $code);
file_put_contents(FILE_PATH, $content);
exec($command, $output, $exit_status);
echo 'Exit status is: ', $exit_status, PHP_EOL,
'Output is:', PHP_EOL, join($output), PHP_EOL;
}
}

?>
--CLEAN--
<?php
const FILE_PATH = __DIR__ . '/exit_named_arg_test.php';
@unlink(FILE_PATH);
?>
--EXPECT--
Using 12 as value:
Exit status is: 12
Output is:

Using 'Goodbye!' as value:
Exit status is: 0
Output is:
Goodbye!
Using 12 as value:
Exit status is: 12
Output is:

Using 'Goodbye!' as value:
Exit status is: 0
Output is:
Goodbye!
20 changes: 10 additions & 10 deletions Zend/tests/exit/exit_values.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const FILE_PATH = __DIR__ . '/exit_values_test.php';
Using NULL as value:
Exit status is: 0
Output is:
Deprecated: exit(): Passing null to parameter #1 ($code) of type string|int is deprecated in %s on line %d
Deprecated: exit(): Passing null to parameter #1 ($status) of type string|int is deprecated in %s on line %d
Using false as value:
Exit status is: 0
Output is:
Expand Down Expand Up @@ -116,23 +116,23 @@ Hello world
Using [] as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, array given
TypeError: exit(): Argument #1 ($status) must be of type string|int, array given
Using STDERR as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, resource given
TypeError: exit(): Argument #1 ($status) must be of type string|int, resource given
Using new stdClass() as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
As a statement:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
Using NULL as value:
Exit status is: 0
Output is:
Deprecated: exit(): Passing null to parameter #1 ($code) of type string|int is deprecated in %s on line %d
Deprecated: exit(): Passing null to parameter #1 ($status) of type string|int is deprecated in %s on line %d
Using false as value:
Exit status is: 0
Output is:
Expand Down Expand Up @@ -168,16 +168,16 @@ Hello world
Using [] as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, array given
TypeError: exit(): Argument #1 ($status) must be of type string|int, array given
Using STDERR as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, resource given
TypeError: exit(): Argument #1 ($status) must be of type string|int, resource given
Using new stdClass() as value:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
As a statement:
Exit status is: 0
Output is:
TypeError: exit(): Argument #1 ($code) must be of type string|int, stdClass given
TypeError: exit(): Argument #1 ($status) must be of type string|int, stdClass given
6 changes: 3 additions & 3 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ zend_result zend_startup_builtin_functions(void) /* {{{ */
ZEND_FUNCTION(exit)
{
zend_string *str = NULL;
zend_long code = 0;
zend_long status = 0;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_LONG(str, code)
Z_PARAM_STR_OR_LONG(str, status)
ZEND_PARSE_PARAMETERS_END();

if (str) {
Expand All @@ -89,7 +89,7 @@ ZEND_FUNCTION(exit)
}
}
} else {
EG(exit_status) = code;
EG(exit_status) = status;
}

ZEND_ASSERT(!EG(exception));
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class stdClass
{
}

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

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

/** @refcount 1 */
function zend_version(): string {}
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c5767f

Please sign in to comment.