1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #2108 - don’t worry if config can’t be found when updating baseline

This commit is contained in:
Brown 2019-09-09 17:03:23 -04:00
parent 34d98f9f42
commit 28a954e623
2 changed files with 56 additions and 33 deletions

View File

@ -409,6 +409,57 @@ function initialiseConfig(
return $config;
}
function update_config_file(Config $config, string $config_file_path, string $baseline_path) : void
{
if ($config->error_baseline === $baseline_path) {
return;
}
$configFile = Config::locateConfigFile($config_file_path);
if (!$configFile) {
fwrite(STDERR, "Don't forget to set errorBaseline=\"{$baseline_path}\" to your config.");
return;
}
$configFileContents = file_get_contents($configFile);
if ($config->error_baseline) {
$amendedConfigFileContents = preg_replace(
'/errorBaseline=".*?"/',
"errorBaseline=\"{$baseline_path}\"",
$configFileContents
);
} else {
$endPsalmOpenTag = strpos($configFileContents, '>', (int)strpos($configFileContents, '<psalm'));
if (!$endPsalmOpenTag) {
fwrite(STDERR, " Don't forget to set errorBaseline=\"{$baseline_path}\" in your config.");
return;
}
if ($configFileContents[$endPsalmOpenTag - 1] === "\n") {
$amendedConfigFileContents = substr_replace(
$configFileContents,
" errorBaseline=\"{$baseline_path}\"\n>",
$endPsalmOpenTag,
1
);
} else {
$amendedConfigFileContents = substr_replace(
$configFileContents,
" errorBaseline=\"{$baseline_path}\">",
$endPsalmOpenTag,
1
);
}
}
file_put_contents($configFile, $amendedConfigFileContents);
}
function get_path_to_config(array $options): ?string
{
$path_to_config = isset($options['c']) && is_string($options['c']) ? realpath($options['c']) : null;

View File

@ -548,39 +548,11 @@ if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
fwrite(STDERR, "Baseline saved to {$options['set-baseline']}.");
/** @var string $configFile */
$configFile = Config::locateConfigFile($path_to_config ?? $current_dir);
$configFileContents = $amendedConfigFileContents = file_get_contents($configFile);
if ($config->error_baseline) {
$amendedConfigFileContents = preg_replace(
'/errorBaseline=".*?"/',
"errorBaseline=\"{$options['set-baseline']}\"",
$configFileContents
);
} else {
$endPsalmOpenTag = strpos($configFileContents, '>', (int)strpos($configFileContents, '<psalm'));
if (!$endPsalmOpenTag) {
fwrite(STDERR, " Don't forget to set errorBaseline=\"{$options['set-baseline']}\" in your config.");
} elseif ($configFileContents[$endPsalmOpenTag - 1] === "\n") {
$amendedConfigFileContents = substr_replace(
$configFileContents,
" errorBaseline=\"{$options['set-baseline']}\"\n>",
$endPsalmOpenTag,
1
);
} else {
$amendedConfigFileContents = substr_replace(
$configFileContents,
" errorBaseline=\"{$options['set-baseline']}\">",
$endPsalmOpenTag,
1
);
}
}
file_put_contents($configFile, $amendedConfigFileContents);
update_config_file(
$config,
$path_to_config ?? $current_dir,
$options['set-baseline']
);
fwrite(STDERR, PHP_EOL);
}