Skip to content

Commit 12a9bb5

Browse files
authored
fix: avoid undefined STDERR and STDIN outside the CLI (#10605)
1 parent d5de58f commit 12a9bb5

7 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎rector.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100

101101
// Exclude test file because `is_cli()` is mocked and Rector might remove needed parameters.
102102
RemoveExtraParametersRector::class => [
103+
__DIR__ . '/tests/system/CLI/CLITest.php',
103104
__DIR__ . '/tests/system/Debug/ToolbarTest.php',
104105
],
105106

‎system/CLI/CLI.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public static function error(string $text, string $foreground = 'light_red', ?st
481481
// Check color support for STDERR
482482
$stdout = static::$isColored;
483483

484-
static::$isColored = static::hasColorSupport(STDERR);
484+
static::$isColored = is_cli() && static::hasColorSupport(STDERR);
485485

486486
if ($foreground !== '' || (string) $background !== '') {
487487
$text = static::color($text, $foreground, $background);
@@ -493,7 +493,7 @@ public static function error(string $text, string $foreground = 'light_red', ?st
493493
static::$lastWrite = 'write';
494494
}
495495

496-
static::fwrite(STDERR, $text . PHP_EOL);
496+
static::fwrite(is_cli() ? STDERR : STDOUT, $text . PHP_EOL);
497497

498498
// return STDOUT color support
499499
static::$isColored = $stdout;
@@ -572,7 +572,7 @@ public static function clearScreen()
572572
{
573573
// Unix systems, and Windows with VT100 Terminal support (i.e. Win10)
574574
// can handle CSI sequences. For lower than Win10 we just shove in 40 new lines.
575-
is_windows() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
575+
is_windows() && is_cli() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
576576
? static::newLine(40)
577577
: static::fwrite(STDOUT, "\033[H\033[2J");
578578
}

‎system/CLI/SignalTrait.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ protected function getProcessState(): array
394394
if ($this->isPosixAvailable()) {
395395
$state['session_id'] = posix_getsid($pid);
396396
$state['process_group'] = posix_getpgid($pid);
397-
$state['has_controlling_terminal'] = posix_isatty(STDIN);
397+
$state['has_controlling_terminal'] = is_cli() && posix_isatty(STDIN);
398398
}
399399

400400
return $state;

‎tests/system/CLI/CLITest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,32 @@ public function testError(): void
563563
$this->assertSame($expected, $this->getStreamFilterBuffer());
564564
}
565565

566+
public function testErrorOutsideCliWritesToStdoutWithoutColor(): void
567+
{
568+
$io = new class () extends InputOutput {
569+
/**
570+
* @var list<array{string, string}>
571+
*/
572+
public array $writes = [];
573+
574+
public function fwrite($handle, string $string): void
575+
{
576+
$this->writes[] = [$handle === STDERR ? 'STDERR' : 'STDOUT', $string];
577+
}
578+
};
579+
CLI::setInputOutput($io);
580+
581+
is_cli(false);
582+
583+
try {
584+
CLI::error('test');
585+
} finally {
586+
is_cli(true);
587+
}
588+
589+
$this->assertSame([['STDOUT', "\ntest\n"]], $io->writes);
590+
}
591+
566592
public function testMixedWriteError(): void
567593
{
568594
CLI::write('test 1');

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Bugs Fixed
4646
ANSI color codes in the prompt (e.g., option defaults) are wrapped in readline's non-printing markers under GNU readline so cursor positioning stays accurate.
4747
On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally.
4848
- **CLI:** Fixed a bug where ``CLI::input()`` and ``CLI::prompt()`` threw a ``TypeError`` when STDIN reached end-of-file (e.g., Ctrl+D) with the ``readline`` extension enabled. An empty string is now returned, matching the behavior without ``readline``.
49+
- **CLI:** Fixed a bug where ``CLI::error()`` threw an ``Error`` for the undefined ``STDERR`` constant when a command ran outside the CLI (e.g., through ``command()`` in a controller). The message is now written like other output.
50+
``CLI::clearScreen()`` on Windows and ``SignalTrait::getProcessState()`` no longer probe the standard streams outside the CLI either.
4951
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
5052
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
5153
- **Commands:** Fixed a bug where ``make:migration --session`` silently generated a broken migration when the database group's driver is neither ``MySQLi`` nor ``Postgre``. The command now reports an error and returns ``EXIT_ERROR``.

‎utils/phpstan-baseline/arguments.count.neon‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# total 2 errors
1+
# total 4 errors
22

33
parameters:
44
ignoreErrors:
5+
-
6+
message: '#^Function is_cli invoked with 1 parameter, 0 required\.$#'
7+
count: 2
8+
path: ../../tests/system/CLI/CLITest.php
9+
510
-
611
message: '#^Function is_cli invoked with 1 parameter, 0 required\.$#'
712
count: 2

‎utils/phpstan-baseline/loader.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 12 errors
1+
# total 14 errors
22

33
includes:
44
- arguments.count.neon

0 commit comments

Comments
 (0)