Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature-other-schemes' into feat…
Browse files Browse the repository at this point in the history
…ure-other-schemes
  • Loading branch information
Stilch committed Apr 27, 2021
2 parents f4caf8c + 15acaf0 commit 37da6c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"ralouphie/getallheaders": "^3",
"symfony/polyfill-php80": "^1.20"
"symfony/polyfill-php80": "^1.22"
},
"require-dev": {
"ext-json": "*",
"adriansuter/php-autoload-override": "^1.2",
"http-interop/http-factory-tests": "^0.8.0",
"http-interop/http-factory-tests": "^0.9.0",
"php-http/psr7-integration-tests": "dev-master",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.5 || ^9.3",
"phpunit/phpunit": "^8.5 || ^9.5",
"squizlabs/php_codesniffer": "^3.5",
"weirdan/prophecy-shim": "^1.0 || ^2.0.2"
},
Expand Down
39 changes: 11 additions & 28 deletions src/Factory/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function restore_error_handler;
use function rewind;
use function set_error_handler;
use function sprintf;

class StreamFactory implements StreamFactoryInterface
{
Expand Down Expand Up @@ -54,37 +53,21 @@ public function createStreamFromFile(
string $mode = 'r',
StreamInterface $cache = null
): StreamInterface {
// When fopen fails, PHP 7 normally raises a warning. Add an error
// handler to check for errors and throw an exception instead.
// On PHP 8, exceptions are thrown.
$exc = null;

// Would not be initialized if fopen throws on PHP >= 8.0
$resource = null;

$errorHandler = function (string $errorMessage) use ($filename, $mode, &$exc) {
$exc = new RuntimeException(sprintf(
'Unable to open %s using mode %s: %s',
$filename,
$mode,
$errorMessage
));
};

set_error_handler(function (int $errno, string $errstr) use ($errorHandler) {
$errorHandler($errstr);
});
set_error_handler(
static function (int $errno, string $errstr) use ($filename, $mode): void {
throw new RuntimeException(
"Unable to open $filename using mode $mode: $errstr",
$errno
);
}
);

try {
$resource = fopen($filename, $mode);
} catch (ValueError $exception) {
$errorHandler($exception->getMessage());
}
restore_error_handler();

if ($exc) {
/** @var RuntimeException $exc */
throw $exc;
throw new RuntimeException("Unable to open $filename using mode $mode: " . $exception->getMessage());
} finally {
restore_error_handler();
}

if (!is_resource($resource)) {
Expand Down
8 changes: 7 additions & 1 deletion tests/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
Expand All @@ -36,6 +37,7 @@
use function sys_get_temp_dir;
use function uniqid;
use function unlink;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const UPLOAD_ERR_CANT_WRITE;
Expand Down Expand Up @@ -358,7 +360,11 @@ public function testMoveToStream()
$movedFileContents = ob_get_clean();

$this->assertEquals($contents, $movedFileContents);
$this->assertFileNotExists($fileName);
if (version_compare(Version::series(), '9.1', '>=')) {
$this->assertFileDoesNotExist($fileName);
} else {
$this->assertFileNotExists($fileName);
}
}

/**
Expand Down

0 comments on commit 37da6c8

Please sign in to comment.