Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow suppressing unevaluatedCode #7236

Merged
merged 2 commits into from Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 25 additions & 31 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Expand Up @@ -359,41 +359,10 @@ private static function analyzeStatement(

$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 = [];

Expand Down Expand Up @@ -517,6 +486,31 @@ private static function analyzeStatement(
$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;
Expand Down
32 changes: 24 additions & 8 deletions tests/IssueSuppressionTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -162,8 +168,6 @@ public function testUncaughtThrowInGlobalScopeSuppressed(): void
$this->addFile(
getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php',
'<?php
/** @psalm-suppress UncaughtThrowInGlobalScope */
throw new Exception();

if (rand(0, 1)) {
/** @psalm-suppress UncaughtThrowInGlobalScope */
Expand All @@ -173,7 +177,10 @@ public function testUncaughtThrowInGlobalScopeSuppressed(): void
/** @psalm-suppress UncaughtThrowInGlobalScope */
if (rand(0, 1)) {
throw new Exception();
}'
}

/** @psalm-suppress UncaughtThrowInGlobalScope */
throw new Exception();'
);

$context = new Context();
Expand Down Expand Up @@ -212,7 +219,7 @@ class Foo {
public string $bar = "baz";
}

$foo = new Foo();
$_foo = new Foo();
'
);

Expand All @@ -234,7 +241,7 @@ class Foo {
public string $bar = "baz";
}

$foo = new Foo();
$_foo = new Foo();
'
);

Expand Down Expand Up @@ -304,7 +311,7 @@ function foo() : void {
* @psalm-suppress TooManyArguments
* here
*/
strlen("a", "b");
echo strlen("a", "b");
}',
],
'suppressUndefinedFunction' => [
Expand All @@ -319,14 +326,14 @@ function verify_return_type(): DateTime {
'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' => [
Expand Down Expand Up @@ -390,6 +397,15 @@ public function foobar(): string
}
',
],
'suppressUnevaluatedCode' => [
'<?php
die();
/**
* @psalm-suppress UnevaluatedCode
*/
break;
',
],
];
}

Expand Down