Skip to content
Permalink
Browse files

Prevent --update-baseline from running in --diff mode

Fixes #1566
  • Loading branch information...
muglug committed Apr 16, 2019
1 parent 282dd8e commit 4abdc1503ff0f0757458c97be682f880396e26da
Showing with 70 additions and 58 deletions.
  1. +70 −58 src/psalm.php
@@ -591,84 +591,96 @@ function ($arg) {
}
if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
echo 'Writing error baseline to file...', PHP_EOL;
if ($is_diff) {
if ($output_format === ProjectAnalyzer::TYPE_CONSOLE) {
echo 'Cannot set baseline in --diff mode' . PHP_EOL;
}
} else {
echo 'Writing error baseline to file...', PHP_EOL;
ErrorBaseline::create(
new \Psalm\Internal\Provider\FileProvider,
$options['set-baseline'],
IssueBuffer::getIssuesData()
);
ErrorBaseline::create(
new \Psalm\Internal\Provider\FileProvider,
$options['set-baseline'],
IssueBuffer::getIssuesData()
);
echo "Baseline saved to {$options['set-baseline']}.";
echo "Baseline saved to {$options['set-baseline']}.";
/** @var string $configFile */
$configFile = Config::locateConfigFile($path_to_config ?? $current_dir);
$configFileContents = $amendedConfigFileContents = file_get_contents($configFile);
/** @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) {
echo " 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
if ($config->error_baseline) {
$amendedConfigFileContents = preg_replace(
'/errorBaseline=".*?"/',
"errorBaseline=\"{$options['set-baseline']}\"",
$configFileContents
);
} else {
$amendedConfigFileContents = substr_replace(
$configFileContents,
" errorBaseline=\"{$options['set-baseline']}\">",
$endPsalmOpenTag,
1
);
$endPsalmOpenTag = strpos($configFileContents, '>', (int)strpos($configFileContents, '<psalm'));
if (!$endPsalmOpenTag) {
echo " 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);
file_put_contents($configFile, $amendedConfigFileContents);
echo PHP_EOL;
echo PHP_EOL;
}
}
$issue_baseline = [];
if (isset($options['update-baseline'])) {
$baselineFile = Config::getInstance()->error_baseline;
if ($is_diff) {
if ($output_format === ProjectAnalyzer::TYPE_CONSOLE) {
echo 'Cannot update baseline in --diff mode' . PHP_EOL;
}
} else {
$baselineFile = Config::getInstance()->error_baseline;
if (empty($baselineFile)) {
die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
}
if (empty($baselineFile)) {
die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
}
try {
$issue_current_baseline = ErrorBaseline::read(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile
);
$total_issues_current_baseline = ErrorBaseline::countTotalIssues($issue_current_baseline);
try {
$issue_current_baseline = ErrorBaseline::read(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile
);
$total_issues_current_baseline = ErrorBaseline::countTotalIssues($issue_current_baseline);
$issue_baseline = ErrorBaseline::update(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile,
IssueBuffer::getIssuesData()
);
$total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline);
$issue_baseline = ErrorBaseline::update(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile,
IssueBuffer::getIssuesData()
);
$total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline);
$total_fixed_issues = $total_issues_current_baseline - $total_issues_updated_baseline;
$total_fixed_issues = $total_issues_current_baseline - $total_issues_updated_baseline;
if ($total_fixed_issues > 0) {
echo str_repeat('-', 30) . "\n";
echo $total_fixed_issues . ' errors fixed' . "\n";
if ($total_fixed_issues > 0) {
echo str_repeat('-', 30) . "\n";
echo $total_fixed_issues . ' errors fixed' . "\n";
}
} catch (\Psalm\Exception\ConfigException $exception) {
die('Could not update baseline file: ' . $exception->getMessage());
}
} catch (\Psalm\Exception\ConfigException $exception) {
die('Could not update baseline file: ' . $exception->getMessage());
}
}

0 comments on commit 4abdc15

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