Skip to content

Commit 1d2e690

Browse files
committed
stream: return array of errors instead of linked list
1 parent e61751a commit 1d2e690

18 files changed

+195
-265
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3556,8 +3556,11 @@ function stream_get_wrappers(): array {}
35563556

35573557
/**
35583558
* @refcount 1
3559+
* @return list<StreamError>
35593560
*/
3560-
function stream_get_last_error(): ?StreamError {}
3561+
function stream_last_errors(): array {}
3562+
3563+
function stream_clear_errors(): void {}
35613564

35623565
/**
35633566
* @return array<int, string>

ext/standard/basic_functions_arginfo.h

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

ext/standard/basic_functions_decl.h

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

ext/standard/streamsfuncs.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,16 @@ PHP_FUNCTION(stream_get_wrappers)
634634
}
635635
/* }}} */
636636

637-
/* Retrieves the last stored stream error */
638-
PHP_FUNCTION(stream_get_last_error)
637+
PHP_FUNCTION(stream_last_errors)
639638
{
640-
ZEND_PARSE_PARAMETERS_NONE();
641-
642-
/* Check if we have any stored errors */
643-
if (!FG(stream_error_state).stored_errors) {
644-
RETURN_NULL();
645-
}
646-
647-
/* Get the most recent stored error (head of list) */
648-
php_stream_stored_error *stored = FG(stream_error_state).stored_errors;
649-
650-
if (!stored->first_error) {
651-
RETURN_NULL();
652-
}
653-
654-
/* Create StreamError object from the error chain */
655-
php_stream_error_create_object(return_value, stored->first_error);
639+
ZEND_PARSE_PARAMETERS_NONE();
640+
php_stream_error_get_last(return_value);
641+
}
642+
643+
PHP_FUNCTION(stream_clear_errors)
644+
{
645+
ZEND_PARSE_PARAMETERS_NONE();
646+
php_stream_error_clear_stored();
656647
}
657648

658649
/* {{{ stream_select related functions */

ext/standard/tests/streams/stream_errors_error_code_helpers.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ $context = stream_context_create([
1212
// Generate a network error
1313
$stream = fopen('php://nonexistent', 'r', false, $context);
1414

15-
$error = stream_get_last_error();
16-
if ($error) {
15+
$errors = stream_last_errors();
16+
if (!empty($errors)) {
17+
$error = $errors[0];
1718
echo "Is I/O error: " . ($error->code->isIoError() ? 'yes' : 'no') . "\n";
1819
echo "Is filesystem error: " . ($error->code->isFileSystemError() ? 'yes' : 'no') . "\n";
1920
echo "Is network error: " . ($error->code->isNetworkError() ? 'yes' : 'no') . "\n";

ext/standard/tests/streams/stream_errors_error_has_code.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ $context = stream_context_create([
1212

1313
// First operation
1414
$stream1 = fopen('php://nonexistent1', 'r', false, $context);
15-
$error1 = stream_get_last_error();
15+
$error1 = stream_last_errors()[0];
1616

1717
// Second operation
1818
$stream2 = fopen('php://nonexistent2', 'r', false, $context);
19-
$error2 = stream_get_last_error();
19+
$error2 = stream_last_errors()[0];
2020

2121
// Should get the most recent error (second operation)
2222
if ($error2) {

ext/standard/tests/streams/stream_errors_exception_mode_terminal.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ try {
1515
echo "Caught: " . $e->getMessage() . "\n";
1616
echo "Code: " . $e->getCode() . "\n";
1717

18-
$error = $e->getError();
19-
if ($error) {
18+
$errors = $e->getErrors();
19+
if (!empty($errors)) {
20+
$error = $errors[0];
2021
echo "Wrapper: " . $error->wrapperName . "\n";
2122
echo "Error code name: " . $error->code->name . "\n";
2223
}

ext/standard/tests/streams/stream_errors_mix_modes_storage.phpt

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TestStream {
2828
stream_wrapper_register('test', 'TestStream');
2929

3030
function stream_test_errors($title, $contextOptions) {
31+
stream_clear_errors();
3132
$context = stream_context_create($contextOptions);
3233
$stream = fopen('test://foo', 'r', false, $context);
3334
try {
@@ -43,22 +44,18 @@ function stream_test_errors($title, $contextOptions) {
4344
echo 'EXCEPTION: ' . $e->getMessage() . "\n";
4445
}
4546

46-
$error = stream_get_last_error();
47-
if ($error) {
47+
$errors = stream_last_errors();
48+
if ($errors) {
49+
$first = $errors[0];
4850
echo "Error details:\n";
49-
echo "- Message: $error->message\n";
50-
echo "- Code: " . $error->code->name . "\n";
51-
echo "- Wrapper: $error->wrapperName\n";
52-
echo "- Terminating: " . ($error->terminating ? 'yes' : 'no') . "\n";
53-
echo "- Count: " . $error->count() . "\n";
54-
55-
// Show all errors in chain
56-
$current = $error;
57-
$idx = 0;
58-
while ($current) {
59-
echo " [$idx] " . $current->code->name . ": " . $current->message . "\n";
60-
$current = $current->next;
61-
$idx++;
51+
echo "- Message: $first->message\n";
52+
echo "- Code: " . $first->code->name . "\n";
53+
echo "- Wrapper: $first->wrapperName\n";
54+
echo "- Terminating: " . ($first->terminating ? 'yes' : 'no') . "\n";
55+
echo "- Count: " . count($errors) . "\n";
56+
57+
foreach ($errors as $idx => $error) {
58+
echo " [$idx] " . $error->code->name . ": " . $error->message . "\n";
6259
}
6360
} else {
6461
echo "No errors stored\n";
@@ -149,10 +146,4 @@ Warning: fread(): TestStream::stream_read - read 10 bytes more data than request
149146
Warning: stream_select(): TestStream::stream_cast is not implemented! in %s on line %d
150147

151148
Warning: stream_select(): Cannot represent a stream of type user-space as a select()able descriptor in %s on line %d
152-
Error details:
153-
- Message: TestStream::stream_read - read 10 bytes more data than requested (8202 read, 8192 max) - excess data will be lost
154-
- Code: UserspaceInvalidReturn
155-
- Wrapper: user-space
156-
- Terminating: no
157-
- Count: 1
158-
[0] UserspaceInvalidReturn: TestStream::stream_read - read 10 bytes more data than requested (8202 read, 8192 max) - excess data will be lost
149+
No errors stored

ext/standard/tests/streams/stream_errors_modes_with_auto_store.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ $context1 = stream_context_create([
1212
]);
1313

1414
@fopen('php://nonexistent', 'r', false, $context1);
15-
$error1 = stream_get_last_error();
16-
echo "ERROR mode AUTO: " . ($error1 ? "has error" : "no error") . "\n";
15+
$errors1 = stream_last_errors();
16+
echo "ERROR mode AUTO: " . (!empty($errors1) ? "has error" : "no error") . "\n";
1717

1818
// AUTO with EXCEPTION mode should store NON_TERM
1919
$context2 = stream_context_create([
@@ -27,8 +27,8 @@ try {
2727
fopen('php://nonexistent2', 'r', false, $context2);
2828
} catch (StreamException $e) {}
2929

30-
$error2 = stream_get_last_error();
31-
echo "EXCEPTION mode AUTO: " . ($error2 ? "has error" : "no error") . "\n";
30+
$errors2 = stream_last_errors();
31+
echo "EXCEPTION mode AUTO: " . (!empty($errors2) ? "has error" : "no error") . "\n";
3232

3333
// AUTO with SILENT mode should store ALL
3434
$context3 = stream_context_create([
@@ -39,8 +39,8 @@ $context3 = stream_context_create([
3939
]);
4040

4141
fopen('php://nonexistent3', 'r', false, $context3);
42-
$error3 = stream_get_last_error();
43-
echo "SILENT mode AUTO: " . ($error3 ? "has error" : "no error") . "\n";
42+
$errors3 = stream_last_errors();
43+
echo "SILENT mode AUTO: " . (!empty($errors3) ? "has error" : "no error") . "\n";
4444

4545
?>
4646
--EXPECTF--

ext/standard/tests/streams/stream_errors_multi_store.phpt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)