Skip to content

Commit 2a5eafc

Browse files
committed
Refactor StreamWrapper to include error reporting options in openDirectHandle; add NativeStreamWarningTest for file_get_contents error handling
1 parent bb68681 commit 2a5eafc

7 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎src/Internal/StreamWrapper.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
206206
$isInclude = ($options & self::STREAM_OPEN_FOR_INCLUDE) !== 0;
207207

208208
if (! $isInclude || ($mode !== 'r' && $mode !== 'rb' && $mode !== 'rt') || ! str_ends_with(strtolower($path), '.php') || ! Config::isEnabled()) {
209-
return $this->openDirectHandle($path, $mode);
209+
return $this->openDirectHandle($path, $mode, $options);
210210
}
211211

212212
$normalizedRaw = str_replace('\\', '/', $path);
213213
if (! PathMatcher::mayPathBeIncluded($normalizedRaw)) {
214-
return $this->openDirectHandle($path, $mode);
214+
return $this->openDirectHandle($path, $mode, $options);
215215
}
216216

217217
if (self::isReadOnlyCall()) {
218-
return $this->openDirectHandle($path, $mode);
218+
return $this->openDirectHandle($path, $mode, $options);
219219
}
220220

221221
self::unregister();
@@ -257,16 +257,22 @@ private static function isReadOnlyCall(): bool
257257
}
258258

259259
/**
260-
* Opens an underlying filesystem handle directly with error handler suppression.
260+
* Opens an underlying filesystem handle directly with error reporting options.
261261
*/
262-
private function openDirectHandle(string $targetFile, string $mode): bool
262+
private function openDirectHandle(string $targetFile, string $mode, int $options): bool
263263
{
264+
$isInclude = ($options & self::STREAM_OPEN_FOR_INCLUDE) !== 0;
265+
264266
self::unregister();
265267
/** @var resource|false $handle */
266268
$handle = self::silent(fn () => fopen($targetFile, $mode));
267269
$this->handle = $handle !== false ? $handle : null;
268270
self::register();
269271

272+
if ($this->handle === null && ! $isInclude) {
273+
trigger_error("fopen({$targetFile}): Failed to open stream: No such file or directory", E_USER_WARNING);
274+
}
275+
270276
return $this->handle !== null;
271277
}
272278

‎src/Resolver/SpecialTypeResolver.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,4 +1110,4 @@ private static function parseFileMetadata(string $fileName, string $source): voi
11101110
// Silently fall back to empty metadata if parsing fails
11111111
}
11121112
}
1113-
}
1113+
}

‎tests/Fixtures/Shopware/Error/Error.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ public function __construct(string $message = '')
1010
{
1111
parent::__construct($message);
1212
}
13-
}
13+
}

‎tests/Fixtures/Shopware/Error/ErrorCollection.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ public function __construct(iterable $elements = [])
2424
$this->elements[] = $element;
2525
}
2626
}
27-
}
27+
}

‎tests/Fixtures/Shopware/Error/TestError.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
class TestError extends Error
88
{
9-
}
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\TypeChecking\Boundaries;
6+
7+
describe('Native Stream Error Reporting Passthrough (Shopware Migration Test)', function () {
8+
test('file_get_contents on non-existent file produces native No such file or directory warning message', function () {
9+
$nonExistentPath = '/non/existent/path/template_' . uniqid() . '.html.twig';
10+
11+
$caughtWarning = '';
12+
set_error_handler(function (int $errno, string $errstr) use (&$caughtWarning) {
13+
$caughtWarning .= $errstr . "\n";
14+
15+
return true;
16+
});
17+
18+
try {
19+
file_get_contents($nonExistentPath);
20+
} finally {
21+
restore_error_handler();
22+
}
23+
24+
expect($caughtWarning)->toContain('No such file or directory');
25+
});
26+
});

‎tests/TypeChecking/InheritanceAndAttributes/SameNamespaceGlobalCollisionTest.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
$ref = new ReflectionMethod(ErrorCollection::class, '__construct');
1616

1717
expect(SpecialTypeResolver::resolveFqcn('Error', $ref))
18-
->toBe(Error::class);
18+
->toBe(Error::class)
19+
;
1920
});
2021

2122
test('accepts TestError in ErrorCollection without global \Error TypeError', function () {
@@ -27,4 +28,4 @@
2728
->and($collection->elements[0])->toBe($testError)
2829
;
2930
});
30-
});
31+
});

0 commit comments

Comments
 (0)