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 ceaea62 commit b35bb3f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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
9 changes: 5 additions & 4 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@
class Config
{
private const DEFAULT_FILE_NAME = 'psalm.xml';
public const CONFIG_NAMESPACE = 'https://getpsalm.org/schema/config';
public const REPORT_INFO = 'info';
public const REPORT_ERROR = 'error';
public const REPORT_SUPPRESS = 'suppress';
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';
final public const REPORT_SUPPRESS = 'suppress';

/**
* @var array<string>
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 @@ -131,7 +131,7 @@ final class Psalm
'report:',
'report-show-info:',
'root:',
'set-baseline:',
'set-baseline::',
'show-info:',
'show-snippet:',
'stats',
Expand Down Expand Up @@ -271,7 +271,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 @@ -639,7 +638,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 @@ -650,29 +649,34 @@ private static function generateBaseline(
): array {
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);

$errorBaseline = ((string) $options['set-baseline'])
?: $config->error_baseline ?: Config::DEFAULT_BASELINE_NAME;

try {
$issue_baseline = ErrorBaseline::read(
new FileProvider,
$options['set-baseline'],
$errorBaseline,
);
} catch (ConfigException $e) {
$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 @@ -1033,7 +1037,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 @@ -1307,11 +1311,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 @@ -190,6 +190,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 @@ -206,7 +222,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 @@ -215,7 +231,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 @@ -257,7 +273,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 b35bb3f

Please sign in to comment.