1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Merge pull request #7236 from orklah/suppress-unevaluatedCode

allow suppressing unevaluatedCode
This commit is contained in:
orklah 2021-12-29 13:10:47 +01:00 committed by GitHub
commit 10167a223a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 39 deletions

View File

@ -359,41 +359,10 @@ class StatementsAnalyzer extends SourceAnalyzer
$codebase = $statements_analyzer->getCodebase();
if ($context->has_returned
&& !$context->collect_initializations
&& !$context->collect_mutations
&& !($stmt instanceof PhpParser\Node\Stmt\Nop)
&& !($stmt instanceof PhpParser\Node\Stmt\Function_)
&& !($stmt instanceof PhpParser\Node\Stmt\Class_)
&& !($stmt instanceof PhpParser\Node\Stmt\Interface_)
&& !($stmt instanceof PhpParser\Node\Stmt\Trait_)
&& !($stmt instanceof PhpParser\Node\Stmt\HaltCompiler)
) {
if ($codebase->find_unused_variables) {
if (IssueBuffer::accepts(
new UnevaluatedCode(
'Expressions after return/throw/continue',
new CodeLocation($statements_analyzer->source, $stmt)
),
$statements_analyzer->source->getSuppressedIssues()
)) {
return null;
}
}
return null;
}
if ($statements_analyzer->getProjectAnalyzer()->debug_lines) {
fwrite(STDERR, $statements_analyzer->getFilePath() . ':' . $stmt->getLine() . "\n");
}
/*
if (isset($context->vars_in_scope['$array']) && !$stmt instanceof PhpParser\Node\Stmt\Nop) {
var_dump($stmt->getLine(), $context->vars_in_scope['$array']);
}
*/
$new_issues = null;
$traced_variables = [];
@ -517,6 +486,31 @@ class StatementsAnalyzer extends SourceAnalyzer
$statements_analyzer->parsed_docblock = null;
}
if ($context->has_returned
&& !$context->collect_initializations
&& !$context->collect_mutations
&& !($stmt instanceof PhpParser\Node\Stmt\Nop)
&& !($stmt instanceof PhpParser\Node\Stmt\Function_)
&& !($stmt instanceof PhpParser\Node\Stmt\Class_)
&& !($stmt instanceof PhpParser\Node\Stmt\Interface_)
&& !($stmt instanceof PhpParser\Node\Stmt\Trait_)
&& !($stmt instanceof PhpParser\Node\Stmt\HaltCompiler)
) {
if ($codebase->find_unused_variables) {
if (IssueBuffer::accepts(
new UnevaluatedCode(
'Expressions after return/throw/continue',
new CodeLocation($statements_analyzer->source, $stmt)
),
$statements_analyzer->source->getSuppressedIssues()
)) {
return null;
}
}
return null;
}
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
if (IfElseAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
return false;

View File

@ -18,6 +18,12 @@ class IssueSuppressionTest extends TestCase
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
public function setUp(): void
{
parent::setUp();
$this->project_analyzer->getCodebase()->find_unused_variables = true;
}
public function testIssueSuppressedOnFunction(): void
{
$this->expectException(CodeException::class);
@ -162,8 +168,6 @@ class IssueSuppressionTest extends TestCase
$this->addFile(
getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php',
'<?php
/** @psalm-suppress UncaughtThrowInGlobalScope */
throw new Exception();
if (rand(0, 1)) {
/** @psalm-suppress UncaughtThrowInGlobalScope */
@ -173,7 +177,10 @@ class IssueSuppressionTest extends TestCase
/** @psalm-suppress UncaughtThrowInGlobalScope */
if (rand(0, 1)) {
throw new Exception();
}'
}
/** @psalm-suppress UncaughtThrowInGlobalScope */
throw new Exception();'
);
$context = new Context();
@ -212,7 +219,7 @@ class IssueSuppressionTest extends TestCase
public string $bar = "baz";
}
$foo = new Foo();
$_foo = new Foo();
'
);
@ -234,7 +241,7 @@ class IssueSuppressionTest extends TestCase
public string $bar = "baz";
}
$foo = new Foo();
$_foo = new Foo();
'
);
@ -304,7 +311,7 @@ class IssueSuppressionTest extends TestCase
* @psalm-suppress TooManyArguments
* here
*/
strlen("a", "b");
echo strlen("a", "b");
}',
],
'suppressUndefinedFunction' => [
@ -319,14 +326,14 @@ class IssueSuppressionTest extends TestCase
'suppressAllStatementIssues' => [
'<?php
/** @psalm-suppress all */
strlen(123, 456, 789);',
echo strlen(123, 456, 789);',
],
'suppressAllFunctionIssues' => [
'<?php
/** @psalm-suppress all */
function foo($a)
{
strlen(123, 456, 789);
echo strlen(123, 456, 789);
}',
],
'possiblyNullSuppressedAtClassLevel' => [
@ -390,6 +397,15 @@ class IssueSuppressionTest extends TestCase
}
',
],
'suppressUnevaluatedCode' => [
'<?php
die();
/**
* @psalm-suppress UnevaluatedCode
*/
break;
',
],
];
}