Skip to content

Commit

Permalink
Manually implement stream_context_set_options in a php8.1-compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 3, 2024
1 parent 08a716a commit bd0b582
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
27 changes: 0 additions & 27 deletions generated/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,6 @@

use Safe\Exceptions\StreamException;

/**
* Sets options on the specified context.
*
* @param resource $context The stream or context resource to apply the options to.
* @param array $options The options to set for context.
*
* options must be an associative
* array of associative arrays in the format
* $array['wrapper']['option'] = $value.
*
* Refer to context options and parameters
* for a listing of stream options.
* @return true Returns TRUE on success.
* @throws StreamException
*
*/
function stream_context_set_options($context, array $options): true
{
error_clear_last();
$safeResult = \stream_context_set_options($context, $options);
if ($safeResult === false) {
throw StreamException::createFromPhpError();
}
return $safeResult;
}


/**
* Sets parameters on the specified context.
*
Expand Down
6 changes: 5 additions & 1 deletion generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
'simplexml_import_dom',
'simplexml_load_file',
'simplexml_load_string',
'fgetcsv', // This function need to return false when iterating on an end of file.
// returns literal "true", which php8.1 doesn't support, so we implement
// this one manually and return "bool"
'stream_context_set_options',
// This function need to return false when iterating on an end of file.
'fgetcsv',
];
27 changes: 27 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Safe\Exceptions\OpensslException;
use Safe\Exceptions\PcreException;
use Safe\Exceptions\SimplexmlException;
use Safe\Exceptions\StreamException;
use Safe\Exceptions\FilesystemException;

use const PREG_NO_ERROR;
Expand Down Expand Up @@ -442,3 +443,29 @@ function fgetcsv($stream, ?int $length = null, string $separator = ",", string $
}
return $safeResult;
}

/**
* Sets options on the specified context.
*
* @param resource $context The stream or context resource to apply the options to.
* @param array $options The options to set for context.
*
* options must be an associative
* array of associative arrays in the format
* $array['wrapper']['option'] = $value.
*
* Refer to context options and parameters
* for a listing of stream options.
* @return true Returns TRUE on success.
* @throws StreamException
*
*/
function stream_context_set_options($context, array $options): bool

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.3)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.3)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.

Check failure on line 463 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Function Safe\stream_context_set_options() has parameter $options with no value type specified in iterable type array.
{
error_clear_last();
$safeResult = \stream_context_set_options($context, $options);

Check failure on line 466 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Function stream_context_set_options not found.

Check failure on line 466 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Function stream_context_set_options not found.

Check failure on line 466 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.1)

Function stream_context_set_options not found.

Check failure on line 466 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.2)

Function stream_context_set_options not found.
if ($safeResult === false) {

Check failure on line 467 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Strict comparison using === between true and false will always evaluate to false.

Check failure on line 467 in lib/special_cases.php

View workflow job for this annotation

GitHub Actions / Generated Tests (8.4)

Strict comparison using === between true and false will always evaluate to false.
throw StreamException::createFromPhpError();
}
return $safeResult;
}

0 comments on commit bd0b582

Please sign in to comment.