Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@

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

Expand Down
6 changes: 3 additions & 3 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public static function error(string $text, string $foreground = 'light_red', ?st
// Check color support for STDERR
$stdout = static::$isColored;

static::$isColored = static::hasColorSupport(STDERR);
static::$isColored = is_cli() && static::hasColorSupport(STDERR);

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

static::fwrite(STDERR, $text . PHP_EOL);
static::fwrite(is_cli() ? STDERR : STDOUT, $text . PHP_EOL);

// return STDOUT color support
static::$isColored = $stdout;
Expand Down Expand Up @@ -572,7 +572,7 @@ public static function clearScreen()
{
// Unix systems, and Windows with VT100 Terminal support (i.e. Win10)
// can handle CSI sequences. For lower than Win10 we just shove in 40 new lines.
is_windows() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
is_windows() && is_cli() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
? static::newLine(40)
: static::fwrite(STDOUT, "\033[H\033[2J");
}
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/SignalTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ protected function getProcessState(): array
if ($this->isPosixAvailable()) {
$state['session_id'] = posix_getsid($pid);
$state['process_group'] = posix_getpgid($pid);
$state['has_controlling_terminal'] = posix_isatty(STDIN);
$state['has_controlling_terminal'] = is_cli() && posix_isatty(STDIN);
}

return $state;
Expand Down
26 changes: 26 additions & 0 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,32 @@ public function testError(): void
$this->assertSame($expected, $this->getStreamFilterBuffer());
}

public function testErrorOutsideCliWritesToStdoutWithoutColor(): void
{
$io = new class () extends InputOutput {
/**
* @var list<array{string, string}>
*/
public array $writes = [];

public function fwrite($handle, string $string): void
{
$this->writes[] = [$handle === STDERR ? 'STDERR' : 'STDOUT', $string];
}
};
CLI::setInputOutput($io);

is_cli(false);

try {
CLI::error('test');
} finally {
is_cli(true);
}

$this->assertSame([['STDOUT', "\ntest\n"]], $io->writes);
}

public function testMixedWriteError(): void
{
CLI::write('test 1');
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.7.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Bugs Fixed
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.
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.
- **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``.
- **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.
``CLI::clearScreen()`` on Windows and ``SignalTrait::getProcessState()`` no longer probe the standard streams outside the CLI either.
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
- **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``.
Expand Down
7 changes: 6 additions & 1 deletion utils/phpstan-baseline/arguments.count.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# total 2 errors
# total 4 errors

parameters:
ignoreErrors:
-
message: '#^Function is_cli invoked with 1 parameter, 0 required\.$#'
count: 2
path: ../../tests/system/CLI/CLITest.php

-
message: '#^Function is_cli invoked with 1 parameter, 0 required\.$#'
count: 2
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 12 errors
# total 14 errors

includes:
- arguments.count.neon
Expand Down
Loading