Skip to content

Commit

Permalink
4.x (vimeo#4363)
Browse files Browse the repository at this point in the history
* Enable --diff mode by default

* Bump required version
  • Loading branch information
muglug authored and danog committed Jan 29, 2021
1 parent 481cf84 commit e11be08
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Psalm also has a few features to make it perform as well as possible on large co
Wherever possible Psalm will run its analysis in parallel to save time. Useful for large codebases, it has a massive impact on performance.

- **Incremental checks**<br />
When using the `--diff` command line option, Psalm will only analyse files that have changed *and* files that reference them.
By default Psalm only analyses files that have changed and files that reference those changed files.

## Example output

Expand Down
5 changes: 3 additions & 2 deletions docs/running_psalm/command_line_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ Psalm has a couple of command-line options that will result in faster builds:

- `--threads=[n]` to run Psalm’s analysis in a number of threads
- `--diff` which only checks files you’ve updated since the last run (and their dependents).
- `--diff-methods` which only checks methods you’ve updated since the last run (and their dependents).

In Psalm 4 `--diff` is turned on by default (you can disable it with `--no-diff`).

Data from the last run is stored in the *cache directory*, which may be set in [configuration](./configuration.md).
If you are running Psalm on a build server, you may want to configure the server to ensure that the cache directory
is preserved between runs.

Running them together (e.g. `--threads=8 --diff --diff-methods`) will result in the fastest possible Psalm run.
Running them together (e.g. `--threads=8 --diff`) will result in the fastest possible Psalm run.
2 changes: 1 addition & 1 deletion docs/running_psalm/installation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Installation

The latest version of Psalm requires PHP >= 7.1 and [Composer](https://getcomposer.org/).
The latest version of Psalm requires PHP >= 7.3 and [Composer](https://getcomposer.org/).

```bash
composer require --dev vimeo/psalm
Expand Down
4 changes: 2 additions & 2 deletions src/command_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ function getPsalmHelpText(): string
--threads=INT
If greater than one, Psalm will run analysis on multiple threads, speeding things up.
--diff
Runs Psalm in diff mode, only checking files that have changed since last run (and their dependents)
--no-diff
Turns off Psalm’s diff mode, checks all files regardless of whether they've changed
--diff-methods
Only checks methods that have changed since last run (and their dependents)
Expand Down
90 changes: 42 additions & 48 deletions src/psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
'debug-performance',
'debug-emitted-issues',
'diff',
'diff-methods',
'disable-extension:',
'find-dead-code::',
'find-unused-code::',
Expand All @@ -94,6 +93,7 @@
'init',
'memory-limit:',
'monochrome',
'no-diff',
'no-cache',
'no-reflection-cache',
'no-file-cache',
Expand Down Expand Up @@ -483,7 +483,9 @@ function (string $arg): bool {
? $options['show-info'] === 'true' || $options['show-info'] === '1'
: false;

$is_diff = isset($options['diff']);
$is_diff = !isset($options['no-diff'])
&& !isset($options['set-baseline'])
&& !isset($options['update-baseline']);

/** @var false|'always'|'auto' $find_unused_code */
$find_unused_code = false;
Expand Down Expand Up @@ -680,67 +682,59 @@ function (string $arg): bool {
}

if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {
if ($is_diff) {
fwrite(STDERR, 'Cannot set baseline in --diff mode' . PHP_EOL);
} else {
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);
fwrite(STDERR, 'Writing error baseline to file...' . PHP_EOL);

ErrorBaseline::create(
new \Psalm\Internal\Provider\FileProvider,
$options['set-baseline'],
IssueBuffer::getIssuesData(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
);
ErrorBaseline::create(
new \Psalm\Internal\Provider\FileProvider,
$options['set-baseline'],
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 {$options['set-baseline']}.");

update_config_file(
$config,
$path_to_config ?? $current_dir,
$options['set-baseline']
);
update_config_file(
$config,
$path_to_config ?? $current_dir,
$options['set-baseline']
);

fwrite(STDERR, PHP_EOL);
}
fwrite(STDERR, PHP_EOL);
}

$issue_baseline = [];

if (isset($options['update-baseline'])) {
if ($is_diff) {
fwrite(STDERR, 'Cannot update baseline in --diff mode' . PHP_EOL);
} else {
$baselineFile = Config::getInstance()->error_baseline;
$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(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
);
$total_issues_updated_baseline = ErrorBaseline::countTotalIssues($issue_baseline);
$issue_baseline = ErrorBaseline::update(
new \Psalm\Internal\Provider\FileProvider,
$baselineFile,
IssueBuffer::getIssuesData(),
$config->include_php_versions_in_error_baseline || isset($options['include-php-versions'])
);
$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";
}
} catch (\Psalm\Exception\ConfigException $exception) {
fwrite(STDERR, 'Could not update baseline file: ' . $exception->getMessage() . PHP_EOL);
exit(1);
if ($total_fixed_issues > 0) {
echo str_repeat('-', 30) . "\n";
echo $total_fixed_issues . ' errors fixed' . "\n";
}
} catch (\Psalm\Exception\ConfigException $exception) {
fwrite(STDERR, 'Could not update baseline file: ' . $exception->getMessage() . PHP_EOL);
exit(1);
}
}

Expand Down

0 comments on commit e11be08

Please sign in to comment.