vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.57k stars 660 forks source link

Errors are not shown with baseline file and output-format=compact #10875

Open andrey-tech opened 7 months ago

andrey-tech commented 7 months ago

Hi!

I have a problem with Psalm v5.23.1 and PHP 8.1: new errors are NOT SHOWN in the console if I use baseline file and output-format=compact (other output formats - OK). Please check and fix!

Description

1) Example with option --output-format=compact — new errors are NOT SHOWN in the console, but Psalm exit code is 2:

vendor/bin/psalm --config=psalm.xml.dist --output-format=compact --use-baseline=psalm-baseline.xml src/Kernel.php

Target PHP version: 8.1 (set by config file) Enabled extensions: dom, mongodb, pdo, simplexml, soap (unsupported extensions: fileinfo, zend-opcache).          
Scanning files...
Analyzing files...

░
FILE: src/Kernel.php

2) Example without option --output-format all new errors are SHOWN in the console and Psalm exit code is 2:

vendor/bin/psalm --config=psalm.xml.dist --use-baseline=psalm-baseline.xml src/Kernel.php         

Target PHP version: 8.1 (set by config file) Enabled extensions: dom, mongodb, pdo, simplexml, soap (unsupported extensions: fileinfo, zend-opcache).
Scanning files...
Analyzing files...

░

To whom it may concern: Psalm cannot detect unused classes, methods and properties
when analyzing individual files and folders. Run on the full project to enable
complete unused code detection.

ERROR: InvalidReturnType - src/Kernel.php:43:36 - The declared return type 'int' for App\Kernel::getCacheDir is incorrect, got 'non-falsy-string' (see https://psalm.dev/011)
    public function getCacheDir(): int

ERROR: InvalidReturnStatement - src/Kernel.php:45:16 - The inferred type 'non-falsy-string' does not match the declared return type 'int' for App\Kernel::getCacheDir (see https://psalm.dev/128)
        return '/tmp/cache/' . $this->environment;

------------------------------
2 errors found
------------------------------
10 other issues found.
You can display them with --show-info=true
------------------------------
Psalm can automatically fix 1 of these issues.
Run Psalm again with
--alter --issues=InvalidReturnType --dry-run
to see what it can fix.
------------------------------

Checks took 2.58 seconds and used 227.321MB of memory
Psalm was able to infer types for 90.6667% of the codebase
psalm-github-bot[bot] commented 7 months ago

Hey @andrey-tech, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

andrey-tech commented 7 months ago

Hey @psalm-github-bot, I can't reproduce the issue on psalm.dev, because this is a console options issue.

andrey-tech commented 1 month ago

The problem in class \Psalm\Report\CompactReport:

76            // If we're at the end of the issue sets, then wrap up the last table and render it out.
77            if ($i === count($this->issues_data) - 1) {
78                $table->render();
79                $output[] = $buffer->fetch();
80            }

The condition inside this if will never be true if array $this->issues_data has non-sequential integer keys, so $table->render() will not be executed.

For example, if baseline file is used, then $this->issues_data is:

Array
(
    [9] => Psalm\Internal\Analyzer\IssueData Object(...)
    [10] => Psalm\Internal\Analyzer\IssueData Object(...)
)