Skip to content

Commit

Permalink
Merge pull request #9162 from vimeo/silence-stderr-warnings-when-prog…
Browse files Browse the repository at this point in the history
…ress-is-suppressed
  • Loading branch information
weirdan committed Jan 23, 2023
2 parents 29b654e + 2af7f51 commit b63061a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -124,7 +122,6 @@
use const PHP_VERSION_ID;
use const PSALM_VERSION;
use const SCANDIR_SORT_NONE;
use const STDERR;

/**
* @psalm-suppress PropertyNotSetInConstructor
Expand Down Expand Up @@ -705,6 +702,9 @@ class Config
*/
private array $plugins = [];

/** @var list<string> */
public array $config_warnings = [];

/** @internal */
protected function __construct()
{
Expand Down Expand Up @@ -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'])) {
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Progress/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function stripos;

use const E_ERROR;
use const PHP_EOL;
use const PHP_OS;
use const STDERR;

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/EndToEnd/PsalmEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down

0 comments on commit b63061a

Please sign in to comment.