Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 2d4aaf2

Browse files
committed
fix: handle file open errors differently in PHP 8
In PHP 8, `fopen()` raises a `ValueError` if the path provided is empty/invalid, instead of returning `false` and triggering a warning. Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
1 parent b59d718 commit 2d4aaf2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/Adapter/Console.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Laminas\Stdlib\ErrorHandler;
1212
use Laminas\Stdlib\StringUtils;
13+
use ValueError;
1314

1415
/**
1516
* Laminas\ProgressBar\Adapter\Console offers a text-based progressbar for console
@@ -168,12 +169,18 @@ public function __destruct()
168169
*/
169170
public function setOutputStream($resource)
170171
{
172+
$fileOpenError = null;
171173
ErrorHandler::start();
172-
$stream = fopen($resource, 'w');
173-
$error = ErrorHandler::stop();
174+
try {
175+
$stream = fopen($resource, 'w');
176+
} catch (ValueError $fileOpenError) {
177+
$stream = false;
178+
}
179+
$error = ErrorHandler::stop();
174180

175181
if ($stream === false) {
176-
throw new Exception\RuntimeException('Unable to open stream', 0, $error);
182+
$previous = $fileOpenError ?: $error;
183+
throw new Exception\RuntimeException('Unable to open stream', 0, $previous);
177184
}
178185

179186
if ($this->outputStream !== null) {

0 commit comments

Comments
 (0)