mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Track exception suppressions (#2211)
This commit is contained in:
parent
3de056cdc2
commit
064d4886c3
@ -16,6 +16,8 @@ use Psalm\Internal\Type\AssertionReconciler;
|
||||
use Psalm\Type\Union;
|
||||
use function strpos;
|
||||
use function strtolower;
|
||||
use function array_search;
|
||||
use function is_int;
|
||||
|
||||
class Context
|
||||
{
|
||||
@ -812,7 +814,14 @@ class Context
|
||||
|
||||
$issue_type = $this->is_global ? 'UncaughtThrowInGlobalScope' : 'MissingThrowsDocblock';
|
||||
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
||||
if (in_array($issue_type, $suppressed_issues, true)) {
|
||||
$suppressed_issue_position = array_search($issue_type, $suppressed_issues, true);
|
||||
if ($suppressed_issue_position !== false) {
|
||||
if (is_int($suppressed_issue_position)) {
|
||||
$file = $statements_analyzer->getFileAnalyzer()->getFilePath();
|
||||
IssueBuffer::addUsedSuppressions([
|
||||
$file => [$suppressed_issue_position => true],
|
||||
]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,8 @@
|
||||
namespace Psalm\Tests;
|
||||
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use Psalm\Config;
|
||||
use Psalm\Context;
|
||||
|
||||
class IssueSuppressionTest extends TestCase
|
||||
{
|
||||
@ -53,6 +55,131 @@ class IssueSuppressionTest extends TestCase
|
||||
$this->analyzeFile('somefile.php', new \Psalm\Context());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMissingThrowsDocblockSuppressed()
|
||||
{
|
||||
Config::getInstance()->check_for_throws_docblock = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
function example1 (): void {
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
if (rand(0, 1)) {
|
||||
function example2 (): void {
|
||||
throw new Exception();
|
||||
}
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMissingThrowsDocblockSuppressedWithoutThrow()
|
||||
{
|
||||
$this->expectException(\Psalm\Exception\CodeException::class);
|
||||
$this->expectExceptionMessage('UnusedPsalmSuppress');
|
||||
Config::getInstance()->check_for_throws_docblock = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
if (rand(0, 1)) {
|
||||
function example (): void {}
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMissingThrowsDocblockSuppressedDuplicate()
|
||||
{
|
||||
$this->expectException(\Psalm\Exception\CodeException::class);
|
||||
$this->expectExceptionMessage('UnusedPsalmSuppress');
|
||||
Config::getInstance()->check_for_throws_docblock = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
function example1 (): void {
|
||||
/** @psalm-suppress MissingThrowsDocblock */
|
||||
throw new Exception();
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtThrowInGlobalScopeSuppressed()
|
||||
{
|
||||
Config::getInstance()->check_for_throws_in_global_scope = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
throw new Exception();
|
||||
|
||||
if (rand(0, 1)) {
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
if (rand(0, 1)) {
|
||||
throw new Exception();
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testUncaughtThrowInGlobalScopeSuppressedWithoutThrow()
|
||||
{
|
||||
$this->expectException(\Psalm\Exception\CodeException::class);
|
||||
$this->expectExceptionMessage('UnusedPsalmSuppress');
|
||||
Config::getInstance()->check_for_throws_in_global_scope = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
||||
strlen("a");'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user