Skip to content

Commit f66f0e5

Browse files
committedAug 23, 2020
Prevent passing invalid resources.
1 parent 88cb954 commit f66f0e5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed
 

‎src/Filesystem.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function write($path, $contents, array $config = [])
7474
*/
7575
public function writeStream($path, $resource, array $config = [])
7676
{
77-
if ( ! is_resource($resource)) {
77+
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
7878
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
7979
}
8080

@@ -107,7 +107,7 @@ public function put($path, $contents, array $config = [])
107107
*/
108108
public function putStream($path, $resource, array $config = [])
109109
{
110-
if ( ! is_resource($resource)) {
110+
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
111111
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
112112
}
113113

@@ -158,7 +158,7 @@ public function update($path, $contents, array $config = [])
158158
*/
159159
public function updateStream($path, $resource, array $config = [])
160160
{
161-
if ( ! is_resource($resource)) {
161+
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
162162
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
163163
}
164164

‎tests/FilesystemTests.php

+20
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ public function testPutStreamInvalid()
156156
$this->filesystem->putStream('path.txt', '__INVALID__');
157157
}
158158

159+
/**
160+
* @dataProvider methodsThatGuardAgainstClosedResources
161+
*/
162+
public function testSupplyingClosedStreams($method)
163+
{
164+
$handle = tmpfile();
165+
fclose($handle);
166+
$this->expectException('InvalidArgumentException');
167+
$this->filesystem->{$method}('path.txt', $handle);
168+
}
169+
170+
public function methodsThatGuardAgainstClosedResources()
171+
{
172+
return [
173+
['putStream'],
174+
['writeStream'],
175+
['updateStream'],
176+
];
177+
}
178+
159179
public function testWriteStreamInvalid()
160180
{
161181
$this->expectException('InvalidArgumentException');

0 commit comments

Comments
 (0)