Skip to content
Permalink
Browse files

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

  • Loading branch information...
muglug committed Sep 9, 2019
1 parent 34d98f9 commit 28a954e623e1672decb7742a868c2dda021cad23
Showing with 56 additions and 33 deletions.
  1. +51 −0 src/command_functions.php
  2. +5 −33 src/psalm.php
@@ -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;
@@ -548,39 +548,11 @@ function ($arg) {
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);
}

0 comments on commit 28a954e

Please sign in to comment.
You can’t perform that action at this time.