Skip to content

Commit ed857c2

Browse files
committed
Fix CS
1 parent 469b8eb commit ed857c2

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/GridFS/Bucket.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,9 @@ private function registerStreamWrapper(): void
782782
*
783783
* @see StreamWrapper::setContextResolver()
784784
*
785-
* @param string $path The full url provided to fopen(). It contains the filename.
786-
* gridfs://database_name/collection_name.files/file_name
785+
* @param string $path The full url provided to fopen(). It contains the filename.
786+
* gridfs://database_name/collection_name.files/file_name
787+
* @param array{revision?: int, chunkSizeBytes?: int, disableMD5?: bool} $context The options provided to fopen()
787788
*
788789
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
789790
*/
@@ -796,7 +797,7 @@ private function resolveStreamContext(string $path, string $mode, array $context
796797
$file = $this->collectionWrapper->findFileByFilenameAndRevision($filename, $context['revision'] ?? -1);
797798

798799
// File not found
799-
if ($file === null) {
800+
if (! is_object($file)) {
800801
return null;
801802
}
802803

src/GridFS/StreamWrapper.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
use function is_resource;
3131
use function is_string;
3232
use function sprintf;
33-
use function str_contains;
3433
use function stream_context_get_options;
3534
use function stream_get_wrappers;
3635
use function stream_wrapper_register;
3736
use function stream_wrapper_unregister;
3837
use function trigger_error;
3938

40-
use const E_USER_WARNING;
39+
use const E_USER_ERROR;
4140
use const SEEK_CUR;
4241
use const SEEK_END;
4342
use const SEEK_SET;
@@ -60,7 +59,7 @@ class StreamWrapper
6059
/** @var ReadableStream|WritableStream|null */
6160
private $stream;
6261

63-
/** @var array<string, Closure(string, string): ContextOptions|null> */
62+
/** @var array<string, Closure(string, string, array): ContextOptions> */
6463
private static array $contextResolvers = [];
6564

6665
public function __destruct()
@@ -103,7 +102,11 @@ public static function register(string $protocol = 'gridfs'): void
103102
*/
104103
public static function setContextResolver(string $name, ?Closure $resolver): void
105104
{
106-
self::$contextResolvers[$name] = $resolver;
105+
if ($resolver === null) {
106+
unset(self::$contextResolvers[$name]);
107+
} else {
108+
self::$contextResolvers[$name] = $resolver;
109+
}
107110
}
108111

109112
/**
@@ -150,6 +153,14 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
150153
$context = [];
151154
if (is_resource($this->context)) {
152155
$context = stream_context_get_options($this->context)[$protocol] ?? [];
156+
157+
if (! is_array($context)) {
158+
if ($options & STREAM_REPORT_ERRORS) {
159+
trigger_error(sprintf('Invalid context for "%s" protocol.', $protocol), E_USER_ERROR);
160+
}
161+
162+
return false;
163+
}
153164
}
154165

155166
// Opening stream from gridfs
@@ -158,32 +169,38 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
158169

159170
if (count($parts) < 4) {
160171
if ($options & STREAM_REPORT_ERRORS) {
161-
trigger_error(sprintf('Invalid GridFS file name: "%s"', $path), E_USER_WARNING);
172+
trigger_error(sprintf('Invalid GridFS file name: "%s"', $path), E_USER_ERROR);
162173
}
163174

164175
return false;
165176
}
166177

167-
if (! isset(self::$contextResolvers[$parts[2]])) {
178+
$resolver = self::$contextResolvers[$parts[2]] ?? null;
179+
if (null === $resolver) {
168180
if ($options & STREAM_REPORT_ERRORS) {
169-
trigger_error(sprintf('Unknown GridFS Bucket "%1$s". Call $bucket->asStreamWrap(\'%1$s\') on the Bucket you want to access.', $parts[2]), E_USER_WARNING);
181+
trigger_error(sprintf('Unknown GridFS Bucket "%1$s". Call $bucket->asStreamWrap(\'%1$s\') on the Bucket you want to access.', $parts[2]), E_USER_ERROR);
170182
}
171183

172184
return false;
173185
}
174186

175-
$context = self::$contextResolvers[$parts[2]]($path, $mode, $context);
187+
$context = $resolver($path, $mode, $context);
176188
if ($context === null) {
177189
if ($options & STREAM_REPORT_ERRORS) {
178-
trigger_error(sprintf('File not found "%s".', $path), E_USER_WARNING);
190+
trigger_error(sprintf('File not found "%s".', $path), E_USER_ERROR);
179191
}
180192

181193
return false;
182194
}
183195
}
184196

185-
assert(is_array($context));
186-
assert(isset($context['collectionWrapper']) && $context['collectionWrapper'] instanceof CollectionWrapper);
197+
if (! $context['collectionWrapper'] instanceof CollectionWrapper) {
198+
if ($options & STREAM_REPORT_ERRORS) {
199+
trigger_error('Invalid context: "gridfs[collectionWrapper]" must be a CollectionWrapper.', E_USER_ERROR);
200+
}
201+
202+
return false;
203+
}
187204

188205
if ($mode === 'r' || $mode === 'rb') {
189206
assert(isset($context['file']) && is_object($context['file']));
@@ -315,6 +332,7 @@ public function stream_write(string $data): int
315332
return $this->stream->writeBytes($data);
316333
}
317334

335+
/** @return false|array */
318336
public function url_stat(string $path, int $flags)
319337
{
320338
$success = $this->stream_open($path, 'r', 0, $openedPath);

tests/GridFS/FunctionalTestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use function fwrite;
1212
use function get_resource_type;
1313
use function rewind;
14-
use function sprintf;
1514
use function stream_get_contents;
1615

1716
/**

0 commit comments

Comments
 (0)