diff --git a/src/Psalm/ErrorBaseline.php b/src/Psalm/ErrorBaseline.php index 47f78198c..ef1b1a14c 100644 --- a/src/Psalm/ErrorBaseline.php +++ b/src/Psalm/ErrorBaseline.php @@ -52,11 +52,15 @@ class ErrorBaseline * * @return void */ - public static function create(FileProvider $fileProvider, string $baselineFile, array $issues) - { + public static function create( + FileProvider $fileProvider, + string $baselineFile, + array $issues, + bool $include_php_versions + ) { $groupedIssues = self::countIssueTypesByFile($issues); - self::writeToFile($fileProvider, $baselineFile, $groupedIssues); + self::writeToFile($fileProvider, $baselineFile, $groupedIssues, $include_php_versions); } /** @@ -127,8 +131,12 @@ class ErrorBaseline * * @return array}>> */ - public static function update(FileProvider $fileProvider, string $baselineFile, array $issues) - { + public static function update( + FileProvider $fileProvider, + string $baselineFile, + array $issues, + bool $include_php_versions + ) { $existingIssues = self::read($fileProvider, $baselineFile); $newIssues = self::countIssueTypesByFile($issues); @@ -159,7 +167,7 @@ class ErrorBaseline $groupedIssues = array_filter($existingIssues); - self::writeToFile($fileProvider, $baselineFile, $groupedIssues); + self::writeToFile($fileProvider, $baselineFile, $groupedIssues, $include_php_versions); return $groupedIssues; } @@ -227,27 +235,30 @@ class ErrorBaseline private static function writeToFile( FileProvider $fileProvider, string $baselineFile, - array $groupedIssues + array $groupedIssues, + bool $include_php_versions ) { $baselineDoc = new \DOMDocument('1.0', 'UTF-8'); $filesNode = $baselineDoc->createElement('files'); $filesNode->setAttribute('psalm-version', PSALM_VERSION); - $extensions = array_merge(get_loaded_extensions(), get_loaded_extensions(true)); + if ($include_php_versions) { + $extensions = array_merge(get_loaded_extensions(), get_loaded_extensions(true)); - usort($extensions, 'strnatcasecmp'); + usort($extensions, 'strnatcasecmp'); - $filesNode->setAttribute('php-version', implode(';' . "\n\t", array_merge( - [ - ('php:' . PHP_VERSION), - ], - array_map( - function (string $extension) : string { - return $extension . ':' . phpversion($extension); - }, - $extensions - ) - ))); + $filesNode->setAttribute('php-version', implode(';' . "\n\t", array_merge( + [ + ('php:' . PHP_VERSION), + ], + array_map( + function (string $extension) : string { + return $extension . ':' . phpversion($extension); + }, + $extensions + ) + ))); + } foreach ($groupedIssues as $file => $issueTypes) { $fileNode = $baselineDoc->createElement('file'); diff --git a/src/command_functions.php b/src/command_functions.php index 2f17cccb8..19d37b266 100644 --- a/src/command_functions.php +++ b/src/command_functions.php @@ -363,12 +363,16 @@ Options: --set-baseline=PATH Save all current error level issues to a file, to mark them as info in subsequent runs + Add --include-php-versions to also include a list of PHP extension versions + --ignore-baseline Ignore the error baseline --update-baseline Update the baseline by removing fixed issues. This will not add new issues to the baseline + Add --include-php-versions to also include a list of PHP extension versions + --generate-json-map=PATH Generate a map of node references and types in JSON format, saved to the given path. diff --git a/src/psalm.php b/src/psalm.php index a1476b91a..3a5b08d3d 100644 --- a/src/psalm.php +++ b/src/psalm.php @@ -61,6 +61,7 @@ $valid_long_options = [ 'refactor', 'shepherd::', 'no-progress', + 'include-php-versions', // used for baseline ]; gc_collect_cycles(); @@ -531,7 +532,8 @@ if (isset($options['set-baseline']) && is_string($options['set-baseline'])) { ErrorBaseline::create( new \Psalm\Internal\Provider\FileProvider, $options['set-baseline'], - IssueBuffer::getIssuesData() + IssueBuffer::getIssuesData(), + isset($options['include-php-versions']) ); fwrite(STDERR, "Baseline saved to {$options['set-baseline']}."); @@ -596,7 +598,8 @@ if (isset($options['update-baseline'])) { $issue_baseline = ErrorBaseline::update( new \Psalm\Internal\Provider\FileProvider, $baselineFile, - IssueBuffer::getIssuesData() + IssueBuffer::getIssuesData(), + isset($options['include-php-versions']) ); $total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline); diff --git a/tests/ErrorBaselineTest.php b/tests/ErrorBaselineTest.php index 656439971..bc1363d47 100644 --- a/tests/ErrorBaselineTest.php +++ b/tests/ErrorBaselineTest.php @@ -191,7 +191,7 @@ class ErrorBaselineTest extends TestCase 'severity' => 'error', 'selected_text' => 'hardy' . "\n", ], - ]); + ], false); $baselineDocument = new \DOMDocument(); $baselineDocument->loadXML($documentContent, LIBXML_NOBLANKS); @@ -285,7 +285,8 @@ class ErrorBaselineTest extends TestCase $remainingBaseline = ErrorBaseline::update( $this->fileProvider->reveal(), $baselineFile, - $newIssues + $newIssues, + false ); $this->assertSame([