From 2af7f51394b7d2324be6f927065b369aa0ec53aa Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sun, 22 Jan 2023 20:40:21 -0400 Subject: [PATCH] Suppress warnings when `--no-output` is specified --- src/Psalm/Config.php | 18 +++++++++--------- src/Psalm/Internal/Cli/Psalm.php | 4 ++++ src/Psalm/Progress/Progress.php | 6 ++++++ tests/EndToEnd/PsalmEndToEndTest.php | 13 +++++++++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 1737aea391a..7e438e31b4c 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -60,7 +60,6 @@ use function class_exists; use function clearstatcache; use function count; -use function defined; use function dirname; use function explode; use function extension_loaded; @@ -70,7 +69,6 @@ use function filetype; use function flock; use function fopen; -use function fwrite; use function get_class; use function get_defined_constants; use function get_defined_functions; @@ -124,7 +122,6 @@ use const PHP_VERSION_ID; use const PSALM_VERSION; use const SCANDIR_SORT_NONE; -use const STDERR; /** * @psalm-suppress PropertyNotSetInConstructor @@ -705,6 +702,9 @@ class Config */ private array $plugins = []; + /** @var list */ + public array $config_warnings = []; + /** @internal */ protected function __construct() { @@ -1179,18 +1179,18 @@ private static function fromXmlAndPaths( $config->use_igbinary = version_compare($igbinary_version, '2.0.5') >= 0; } - if (!isset($config_xml['findUnusedBaselineEntry']) && !defined('__IS_TEST_ENV__')) { - fwrite(STDERR, 'Warning: "findUnusedBaselineEntry" will be defaulted to "true" in Psalm 6. You should' - . ' explicitly enable or disable this setting.' . PHP_EOL); + if (!isset($config_xml['findUnusedBaselineEntry'])) { + $config->config_warnings[] = '"findUnusedBaselineEntry" will be defaulted to "true" in Psalm 6.' + . ' You should explicitly enable or disable this setting.'; } if (isset($config_xml['findUnusedCode'])) { $attribute_text = (string) $config_xml['findUnusedCode']; $config->find_unused_code = $attribute_text === 'true' || $attribute_text === '1'; $config->find_unused_variables = $config->find_unused_code; - } elseif (!defined('__IS_TEST_ENV__')) { - fwrite(STDERR, 'Warning: "findUnusedCode" will be defaulted to "true" in Psalm 6. You should explicitly' - . ' enable or disable this setting.' . PHP_EOL); + } else { + $config->config_warnings[] = '"findUnusedCode" will be defaulted to "true" in Psalm 6.' + . ' You should explicitly enable or disable this setting.'; } if (isset($config_xml['findUnusedVariablesAndParams'])) { diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 7917e1a0ba9..6ecb044189b 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -597,6 +597,10 @@ private static function initProgress(array $options, Config $config): Progress $progress = new DefaultProgress($show_errors, $show_info); } } + // output buffered warnings + foreach ($config->config_warnings as $warning) { + $progress->warning($warning); + } return $progress; } diff --git a/src/Psalm/Progress/Progress.php b/src/Psalm/Progress/Progress.php index 678f4d1731e..709d04bdb67 100644 --- a/src/Psalm/Progress/Progress.php +++ b/src/Psalm/Progress/Progress.php @@ -9,6 +9,7 @@ use function stripos; use const E_ERROR; +use const PHP_EOL; use const PHP_OS; use const STDERR; @@ -56,6 +57,11 @@ public function write(string $message): void fwrite(STDERR, $message); } + public function warning(string $message): void + { + $this->write('Warning: ' . $message . PHP_EOL); + } + protected static function doesTerminalSupportUtf8(): bool { if (stripos(PHP_OS, 'WIN') === 0) { diff --git a/tests/EndToEnd/PsalmEndToEndTest.php b/tests/EndToEnd/PsalmEndToEndTest.php index 3a9d3e74019..b8e0f1dac9a 100644 --- a/tests/EndToEnd/PsalmEndToEndTest.php +++ b/tests/EndToEnd/PsalmEndToEndTest.php @@ -231,6 +231,19 @@ public function testLegacyConfigWithoutresolveFromConfigFile(): void $this->assertStringContainsString('InvalidReturnType', $process->getOutput()); } + public function testPsalmWithNoProgressDoesNotProduceOutputOnStderr(): void + { + $this->runPsalmInit(); + + $psalmXml = file_get_contents(self::$tmpDir . '/psalm.xml'); + $psalmXml = preg_replace('/findUnusedCode="(true|false)"/', '', $psalmXml); + file_put_contents(self::$tmpDir . '/psalm.xml', $psalmXml); + + $result = $this->runPsalm(['--no-progress'], self::$tmpDir); + + $this->assertSame('', $result['STDERR']); + } + /** * @return array{STDOUT: string, STDERR: string, CODE: int|null} */