diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index c8a5a4ee20c..5aacced0f61 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -276,7 +276,8 @@ public static function run(array $argv): void if (isset($options['set-baseline'])) { if (is_array($options['set-baseline'])) { - die('Only one baseline file can be created at a time' . PHP_EOL); + fwrite(STDERR, 'Only one baseline file can be created at a time' . PHP_EOL); + exit(1); } } @@ -486,7 +487,8 @@ static function (string $arg): void { private static function generateConfig(string $current_dir, array &$args): void { if (file_exists($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml')) { - die('A config file already exists in the current directory' . PHP_EOL); + fwrite(STDERR, 'A config file already exists in the current directory' . PHP_EOL); + exit(1); } $args = array_values(array_filter( @@ -507,12 +509,14 @@ private static function generateConfig(string $current_dir, array &$args): void $init_source_dir = null; if (count($args)) { if (count($args) > 2) { - die('Too many arguments provided for psalm --init' . PHP_EOL); + fwrite(STDERR, 'Too many arguments provided for psalm --init' . PHP_EOL); + exit(1); } if (isset($args[1])) { if (!preg_match('/^[1-8]$/', $args[1])) { - die('Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL); + fwrite(STDERR, 'Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL); + exit(1); } $init_level = (int)$args[1]; @@ -532,11 +536,13 @@ private static function generateConfig(string $current_dir, array &$args): void $vendor_dir, ); } catch (ConfigCreationException $e) { - die($e->getMessage() . PHP_EOL); + fwrite(STDERR, $e->getMessage() . PHP_EOL); + exit(1); } - if (!file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents)) { - die('Could not write to psalm.xml' . PHP_EOL); + if (file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents) === false) { + fwrite(STDERR, 'Could not write to psalm.xml' . PHP_EOL); + exit(1); } exit('Config file created successfully. Please re-run psalm.' . PHP_EOL); @@ -681,7 +687,8 @@ private static function updateBaseline(array $options, Config $config): array $baselineFile = $config->error_baseline; if (empty($baselineFile)) { - die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL); + fwrite(STDERR, 'Cannot update baseline, because no baseline file is configured.' . PHP_EOL); + exit(1); } try { @@ -776,11 +783,13 @@ private static function autoGenerateConfig( $vendor_dir, ); } catch (ConfigCreationException $e) { - die($e->getMessage() . PHP_EOL); + fwrite(STDERR, $e->getMessage() . PHP_EOL); + exit(1); } - if (!file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents)) { - die('Could not write to psalm.xml' . PHP_EOL); + if (file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents) === false) { + fwrite(STDERR, 'Could not write to psalm.xml' . PHP_EOL); + exit(1); } exit('Config file created successfully. Please re-run psalm.' . PHP_EOL); diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index 5b53ec0dc13..8f895265695 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -112,7 +112,8 @@ public static function run(array $argv): void self::syncShortOptions($options); if (isset($options['c']) && is_array($options['c'])) { - die('Too many config files provided' . PHP_EOL); + fwrite(STDERR, 'Too many config files provided' . PHP_EOL); + exit(1); } if (array_key_exists('h', $options)) { @@ -200,7 +201,8 @@ public static function run(array $argv): void $root_path = realpath($options['r']); if ($root_path === false) { - die('Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL); + fwrite(STDERR, 'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL); + exit(1); } $current_dir = $root_path; @@ -304,7 +306,8 @@ public static function run(array $argv): void if (array_key_exists('issues', $options)) { if (!is_string($options['issues']) || !$options['issues']) { - die('Expecting a comma-separated list of issues' . PHP_EOL); + fwrite(STDERR, 'Expecting a comma-separated list of issues' . PHP_EOL); + exit(1); } $issues = explode(',', $options['issues']); @@ -339,7 +342,8 @@ public static function run(array $argv): void ); if ($allow_backwards_incompatible_changes === null) { - die('--allow-backwards-incompatible-changes expects a boolean value [true|false|1|0]' . PHP_EOL); + fwrite(STDERR, '--allow-backwards-incompatible-changes expects a boolean value [true|false|1|0]' . PHP_EOL); + exit(1); } $project_analyzer->getCodebase()->allow_backwards_incompatible_changes @@ -354,7 +358,8 @@ public static function run(array $argv): void ); if ($doc_block_add_new_line_before_return === null) { - die('--add-newline-between-docblock-annotations expects a boolean value [true|false|1|0]' . PHP_EOL); + fwrite(STDERR, '--add-newline-between-docblock-annotations expects a boolean value [true|false|1|0]' . PHP_EOL); + exit(1); } ParsedDocblock::addNewLineBetweenAnnotations($doc_block_add_new_line_before_return); @@ -505,7 +510,8 @@ private static function loadCodeowners(Providers $providers): array } elseif (file_exists('docs/CODEOWNERS')) { $codeowners_file_path = realpath('docs/CODEOWNERS'); } else { - die('Cannot use --codeowner without a CODEOWNERS file' . PHP_EOL); + fwrite(STDERR, 'Cannot use --codeowner without a CODEOWNERS file' . PHP_EOL); + exit(1); } $codeowners_file = file_get_contents($codeowners_file_path); @@ -555,7 +561,8 @@ static function (string $line): bool { } if (!$codeowner_files) { - die('Could not find any available entries in CODEOWNERS' . PHP_EOL); + fwrite(STDERR, 'Could not find any available entries in CODEOWNERS' . PHP_EOL); + exit(1); } return $codeowner_files; @@ -571,11 +578,13 @@ private static function loadCodeownersFiles(array $desired_codeowners, array $co /** @psalm-suppress MixedAssignment */ foreach ($desired_codeowners as $desired_codeowner) { if (!is_string($desired_codeowner)) { - die('Invalid --codeowner ' . (string)$desired_codeowner . PHP_EOL); + fwrite(STDERR, 'Invalid --codeowner ' . (string) $desired_codeowner . PHP_EOL); + exit(1); } if ($desired_codeowner[0] !== '@') { - die('--codeowner option must start with @' . PHP_EOL); + fwrite(STDERR, '--codeowner option must start with @' . PHP_EOL); + exit(1); } $matched_file = false; @@ -588,7 +597,8 @@ private static function loadCodeownersFiles(array $desired_codeowners, array $co } if (!$matched_file) { - die('User/group ' . $desired_codeowner . ' does not own any PHP files' . PHP_EOL); + fwrite(STDERR, 'User/group ' . $desired_codeowner . ' does not own any PHP files' . PHP_EOL); + exit(1); } } diff --git a/src/Psalm/Internal/Cli/Refactor.php b/src/Psalm/Internal/Cli/Refactor.php index 22761b873f1..854b63d6d3b 100644 --- a/src/Psalm/Internal/Cli/Refactor.php +++ b/src/Psalm/Internal/Cli/Refactor.php @@ -121,7 +121,8 @@ static function (string $arg) use ($valid_long_options): void { } if (isset($options['c']) && is_array($options['c'])) { - die('Too many config files provided' . PHP_EOL); + fwrite(STDERR, 'Too many config files provided' . PHP_EOL); + exit(1); } if (array_key_exists('h', $options)) { @@ -171,7 +172,8 @@ static function (string $arg) use ($valid_long_options): void { $root_path = realpath($options['r']); if ($root_path === false) { - die('Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL); + fwrite(STDERR, 'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL); + exit(1); } $current_dir = $root_path; @@ -210,7 +212,8 @@ static function (string $arg) use ($valid_long_options): void { if ($arg === '--into') { if ($operation !== 'move' || !$last_arg) { - die('--into is not expected here' . PHP_EOL); + fwrite(STDERR, '--into is not expected here' . PHP_EOL); + exit(1); } $operation = 'move_into'; @@ -224,7 +227,8 @@ static function (string $arg) use ($valid_long_options): void { if ($arg === '--to') { if ($operation !== 'rename' || !$last_arg) { - die('--to is not expected here' . PHP_EOL); + fwrite(STDERR, '--to is not expected here' . PHP_EOL); + exit(1); } $operation = 'rename_to'; @@ -239,7 +243,8 @@ static function (string $arg) use ($valid_long_options): void { if ($operation === 'move_into' || $operation === 'rename_to') { if (!$last_arg) { - die('Expecting a previous argument' . PHP_EOL); + fwrite(STDERR, 'Expecting a previous argument' . PHP_EOL); + exit(1); } if ($operation === 'move_into') { @@ -273,11 +278,13 @@ static function (string $arg) use ($valid_long_options): void { continue; } - die('Unexpected argument "' . $arg . '"' . PHP_EOL); + fwrite(STDERR, 'Unexpected argument "' . $arg . '"' . PHP_EOL); + exit(1); } if (!$to_refactor) { - die('No --move or --rename arguments supplied' . PHP_EOL); + fwrite(STDERR, 'No --move or --rename arguments supplied' . PHP_EOL); + exit(1); } $config = CliUtils::initializeConfig( diff --git a/src/Psalm/Internal/CliUtils.php b/src/Psalm/Internal/CliUtils.php index e90f73e4e5d..8f0f1fbf9cb 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -483,7 +483,8 @@ public static function initPhpVersion(array $options, Config $config, ProjectAna if (isset($options['php-version'])) { if (!is_string($options['php-version'])) { - die('Expecting a version number in the format x.y' . PHP_EOL); + fwrite(STDERR, 'Expecting a version number in the format x.y' . PHP_EOL); + exit(1); } $version = $options['php-version']; $source = 'cli';