1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #6899 from pilif/fix-6887

don't stop processing for class usage after raising an issue
This commit is contained in:
orklah 2021-11-12 09:42:49 +01:00 committed by GitHub
commit 2d0758ab10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -357,7 +357,7 @@ class StatementsAnalyzer extends SourceAnalyzer
), ),
$statements_analyzer->source->getSuppressedIssues() $statements_analyzer->source->getSuppressedIssues()
)) { )) {
return false; return null;
} }
} }

View File

@ -5,8 +5,13 @@ use Psalm\Config;
use Psalm\Context; use Psalm\Context;
use Psalm\Internal\Provider\FakeFileProvider; use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\RuntimeCaches; use Psalm\Internal\RuntimeCaches;
use Psalm\IssueBuffer;
use Psalm\Tests\Internal\Provider; use Psalm\Tests\Internal\Provider;
use function getcwd;
use const DIRECTORY_SEPARATOR;
class UnusedCodeTest extends TestCase class UnusedCodeTest extends TestCase
{ {
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */ /** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
@ -99,6 +104,70 @@ class UnusedCodeTest extends TestCase
\Psalm\IssueBuffer::processUnusedSuppressions($this->project_analyzer->getCodebase()->file_provider); \Psalm\IssueBuffer::processUnusedSuppressions($this->project_analyzer->getCodebase()->file_provider);
} }
public function testSeesClassesUsedAfterUnevaluatedCodeIssue(): void
{
$this->project_analyzer->getConfig()->throw_exception = false;
$file_path = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php';
$this->addFile(
$file_path,
'<?php
if (rand(0, 1)) {
throw new Exception("foo");
echo "bar";
} else {
$f = new Foo();
$f->bar();
}
class Foo {
function bar(): void{
echo "foo";
}
}
'
);
$this->analyzeFile($file_path, new Context(), false);
$this->project_analyzer->consolidateAnalyzedData();
$this->assertSame(1, IssueBuffer::getErrorCount());
$issue = IssueBuffer::getIssuesDataForFile($file_path)[0];
$this->assertSame('UnevaluatedCode', $issue->type);
$this->assertSame(4, $issue->line_from);
}
public function testSeesUnusedClassReferencedByUnevaluatedCode(): void
{
$this->project_analyzer->getConfig()->throw_exception = false;
$file_path = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php';
$this->addFile(
$file_path,
'<?php
if (rand(0, 1)) {
throw new Exception("foo");
$f = new Foo();
$f->bar();
} else {
echo "bar";
}
class Foo {
function bar(): void{
echo "foo";
}
}
'
);
$this->analyzeFile($file_path, new Context(), false);
$this->project_analyzer->consolidateAnalyzedData();
$this->assertSame(3, IssueBuffer::getErrorCount());
$issue = IssueBuffer::getIssuesDataForFile($file_path)[2];
$this->assertSame('UnusedClass', $issue->type);
$this->assertSame(10, $issue->line_from);
}
/** /**
* @return array<string, array{string}> * @return array<string, array{string}>
*/ */