Skip to content

Commit

Permalink
Added default baseline file
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgsowa committed Feb 12, 2024
1 parent cb0e6a1 commit 95c70d9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
],
"verify-callmap": "@php phpunit tests/Internal/Codebase/InternalCallMapHandlerTest.php",
"psalm": "@php ./psalm",
"psalm-set-baseline": "@php ./psalm --set-baseline=psalm-baseline.xml",
"psalm-set-baseline": "@php ./psalm --set-baseline",
"tests": [
"@lint",
"@cs",
Expand Down
8 changes: 7 additions & 1 deletion docs/running_psalm/dealing_with_code_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@ If you wish to suppress all issues, you can use `@psalm-suppress all` instead of

If you have a bunch of errors and you don't want to fix them all at once, Psalm can grandfather-in errors in existing code, while ensuring that new code doesn't have those same sorts of errors.

```
vendor/bin/psalm --set-baseline
```

will generate a file psalm-baseline.xml containing the current errors. Alternateivly you can specify the name of your baseline file.

```
vendor/bin/psalm --set-baseline=your-baseline.xml
```

will generate a file containing the current errors. You should commit that generated file so that Psalm can use it when running in other places (e.g. CI). It won't complain about those errors either.
You should commit that generated file so that Psalm can use it when running in other places (e.g. CI). It won't complain about those errors either.

You have two options to use the generated baseline when running psalm:

Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
final class Config
{
private const DEFAULT_FILE_NAME = 'psalm.xml';
final public const DEFAULT_BASELINE_NAME = 'psalm-baseline.xml';
final public const CONFIG_NAMESPACE = 'https://getpsalm.org/schema/config';
final public const REPORT_INFO = 'info';
final public const REPORT_ERROR = 'error';
Expand Down
32 changes: 19 additions & 13 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ final class Psalm
'report:',
'report-show-info:',
'root:',
'set-baseline:',
'set-baseline::',
'show-info:',
'show-snippet:',
'stats',
Expand Down Expand Up @@ -273,7 +273,6 @@ public static function run(array $argv): void
$config->debug_emitted_issues = true;
}


setlocale(LC_CTYPE, 'C');

if (isset($options['set-baseline'])) {
Expand Down Expand Up @@ -641,7 +640,7 @@ private static function initProviders(array $options, Config $config, string $cu
}

/**
* @param array{"set-baseline": string, ...} $options
* @param array{"set-baseline": mixed, ...} $options
* @return array<string,array<string,array{o:int, s: list<string>}>>
*/
private static function generateBaseline(
Expand All @@ -652,29 +651,34 @@ private static function generateBaseline(
): array {
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);

$errorBaseline = ((string) $options['set-baseline'])

Check failure on line 654 in src/Psalm/Internal/Cli/Psalm.php

View workflow job for this annotation

GitHub Actions / build

RiskyTruthyFalsyComparison

src/Psalm/Internal/Cli/Psalm.php:654:26: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
?: $config->error_baseline ?: Config::DEFAULT_BASELINE_NAME;

try {
$issue_baseline = ErrorBaseline::read(
new FileProvider,
$options['set-baseline'],
$errorBaseline,
);
} catch (ConfigException) {
$issue_baseline = [];
}

ErrorBaseline::create(
new FileProvider,
$options['set-baseline'],
$errorBaseline,
IssueBuffer::getIssuesData(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions']),
);

fwrite(STDERR, "Baseline saved to {$options['set-baseline']}.");
fwrite(STDERR, "Baseline saved to $errorBaseline.");

CliUtils::updateConfigFile(
$config,
$path_to_config ?? $current_dir,
$options['set-baseline'],
);
if ($errorBaseline !== $config->error_baseline) {
CliUtils::updateConfigFile(
$config,
$path_to_config ?? $current_dir,
$errorBaseline,
);
}

fwrite(STDERR, PHP_EOL);

Expand Down Expand Up @@ -1037,7 +1041,7 @@ private static function initBaseline(
): array {
$issue_baseline = [];

if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
if (isset($options['set-baseline'])) {
if ($paths_to_check !== null) {
fwrite(STDERR, PHP_EOL . 'Cannot generate baseline when checking specific files' . PHP_EOL);
exit(1);
Expand Down Expand Up @@ -1285,11 +1289,13 @@ private static function getHelpText(): string
Output the taint graph using the DOT language – requires --taint-analysis
Issue baselines:
--set-baseline=PATH
--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
Default value is `psalm-baseline.xml`
--use-baseline=PATH
Allows you to use a baseline other than the default baseline provided in your config
Expand Down
22 changes: 19 additions & 3 deletions tests/EndToEnd/PsalmEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ public function testTainting(): void
$this->assertSame(2, $result['CODE']);
}

public function testPsalmSetBaseline(): void
{
$this->runPsalmInit(1);
$this->runPsalm(['--set-baseline'], self::$tmpDir, true);

$this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']);
}

public function testPsalmSetBaselineWithArgument(): void
{
$this->runPsalmInit(1);
$this->runPsalm(['--set-baseline=psalm-custom-baseline.xml'], self::$tmpDir, true);

$this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']);
}

public function testTaintingWithoutInit(): void
{
$result = $this->runPsalm(['--taint-analysis'], self::$tmpDir, true, false);
Expand All @@ -210,7 +226,7 @@ public function testTaintGraphDumping(): void
$result = $this->runPsalm(
[
'--taint-analysis',
'--dump-taint-graph='.self::$tmpDir.'/taints.dot',
'--dump-taint-graph=' . self::$tmpDir . '/taints.dot',
],
self::$tmpDir,
true,
Expand All @@ -219,7 +235,7 @@ public function testTaintGraphDumping(): void
$this->assertSame(2, $result['CODE']);
$this->assertFileEquals(
__DIR__ . '/../fixtures/expected_taint_graph.dot',
self::$tmpDir.'/taints.dot',
self::$tmpDir . '/taints.dot',
);
}

Expand Down Expand Up @@ -263,7 +279,7 @@ private function runPsalmInit(?int $level = null, ?string $php_version = null):

if ($level) {
$args[] = 'src';
$args[] = (string) $level;
$args[] = (string)$level;
}

$ret = $this->runPsalm($args, self::$tmpDir, false, false);
Expand Down

0 comments on commit 95c70d9

Please sign in to comment.