Skip to content
Permalink
Browse files

Remove unused suppressions in tests

  • Loading branch information...
muglug committed Aug 18, 2019
1 parent b6d0aea commit 5d94e340ee73b6356436128389bc7d3b74a238c8
@@ -490,8 +490,7 @@ protected function __construct()
* @param string $output_format
*
* @return Config
* @psalm-suppress MixedArgument
*@throws ConfigException if a config path is not found
* @throws ConfigException if a config path is not found
*
*/
public static function getConfigForPath($path, $current_dir, $output_format)
@@ -307,7 +307,9 @@ function ($line) {
continue;
}
IssueBuffer::addUnusedSuppression($this->getFilePath(), $offset, $issue_type);
if ($codebase->track_unused_suppressions) {
IssueBuffer::addUnusedSuppression($this->getFilePath(), $offset, $issue_type);
}
}
$this->addSuppressedIssues($new_issues);
@@ -64,6 +64,11 @@ class IssueBuffer
*/
protected static $unused_suppressions = [];
/**
* @var array<string, array<int, bool>>
*/
protected static $already_used_suppressions = [];
/**
* @param CodeIssue $e
* @param string[] $suppressed_issues
@@ -81,6 +86,10 @@ public static function accepts(CodeIssue $e, array $suppressed_issues = [])
public static function addUnusedSuppression(string $file_path, int $offset, string $issue_type) : void
{
if (isset(self::$already_used_suppressions[$file_path][$offset])) {
return;
}
if (!isset(self::$unused_suppressions[$file_path])) {
self::$unused_suppressions[$file_path] = [];
}
@@ -109,8 +118,11 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = [])
$suppressed_issue_position = array_search($issue_type, $suppressed_issues);
if ($suppressed_issue_position !== false) {
/** @psalm-suppress MixedArrayTypeCoercion */
unset(self::$unused_suppressions[$file_path][$suppressed_issue_position]);
if (\is_int($suppressed_issue_position)) {
self::$already_used_suppressions[$file_path][$suppressed_issue_position] = true;
unset(self::$unused_suppressions[$file_path][$suppressed_issue_position]);
}
return true;
}
@@ -120,8 +132,11 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = [])
$suppressed_issue_position = array_search($parent_issue_type, $suppressed_issues);
if ($suppressed_issue_position !== false) {
/** @psalm-suppress MixedArrayTypeCoercion */
unset(self::$unused_suppressions[$file_path][$suppressed_issue_position]);
if (\is_int($suppressed_issue_position)) {
self::$already_used_suppressions[$file_path][$suppressed_issue_position] = true;
unset(self::$unused_suppressions[$file_path][$suppressed_issue_position]);
}
return true;
}
}
@@ -554,6 +569,7 @@ public static function clearCache()
self::$recorded_issues = [];
self::$console_issues = [];
self::$unused_suppressions = [];
self::$already_used_suppressions = [];
}
/**
@@ -366,7 +366,6 @@ class C implements ArrayAccess
/**
* @param array<scalar|array> $array
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedTypeCoercion
*/
public function __construct(array $array)
@@ -994,8 +994,6 @@ public function getPosters($commenter, $numToGet=10) {
foreach (["one", "two", "three"] as $key) {
/**
* @psalm-suppress EmptyArrayAccess
* @psalm-suppress InvalidOperand
* @psalm-suppress MixedOperand
*/
$a[$key] += 5;
@@ -44,9 +44,6 @@ function() use(&$data) {
$bar = ["foo", "bar"];
$bam = array_map(
/**
* @psalm-suppress MissingClosureReturnType
*/
function(string $a) {
return $a . "blah";
},
@@ -691,15 +688,6 @@ public function __invoke(int $p):bool {
acceptsIntToBool(Closure::fromCallable($anonInvokable));',
],
'noExceptionWhenSuppressingUndefinedClass' => [
'<?php
class one { public function two(string $_p): void {} }
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress InvalidArgument
*/
array_map(["two", "three"], ["one", "two"]);',
],
'callableSelfArg' => [
'<?php
class C extends B {}
@@ -871,7 +859,6 @@ function foo(callable $c) : void {
'<?php
/**
* @param callable|array $c
* @psalm-suppress MixedArgument
*/
function foo($c) : void {
if (is_array($c) && is_string($c[1])) {
@@ -197,7 +197,6 @@ function bar(string $maybeBaz) : string {
'<?php
/**
* @param array<string, object> $array
* @psalm-suppress MixedAssignment
*/
function foo(array $array, string $key) : void {
foreach ($array as $i => $item) {
@@ -16,8 +16,6 @@ public function testIssueSuppressedOnFunction()
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('UnusedPsalmSuppress');
$this->project_analyzer->trackUnusedSuppressions();
$this->addFile(
'somefile.php',
'<?php
@@ -45,8 +43,6 @@ public function testIssueSuppressedOnStatement()
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('UnusedPsalmSuppress');
$this->project_analyzer->trackUnusedSuppressions();
$this->addFile(
'somefile.php',
'<?php
@@ -100,36 +96,6 @@ public function b(): void {
'assertions' => [],
'error_levels' => ['UndefinedFunction'],
],
'crossClosureBoundariesOnFunction' => [
'<?php
/**
* @psalm-suppress MissingClosureParamType
* @psalm-suppress MissingClosureReturnType
*/
function foo(array $bar): array {
return array_map(
function ($value) {
return (string)$value;
},
$bar
);
}',
],
'crossClosureBoundariesOnReturn' => [
'<?php
function bar(array $bar): array {
/**
* @psalm-suppress MissingClosureParamType
* @psalm-suppress MissingClosureReturnType
*/
return array_map(
function ($value) {
return (string)$value;
},
$bar
);
}',
],
'suppressWithNewlineAfterComment' => [
'<?php
function foo() : void {
@@ -187,7 +187,6 @@ public function fooFoo() {
'<?php
class A {
/**
* @psalm-suppress TooManyArguments
* @return bool
*/
public function fooFoo() {
@@ -1912,8 +1912,6 @@ public function __construct(array $data) {
* @param K $property
*
* @return TData[K]
* @psalm-suppress MixedReturnStatement due to bug
* @psalm-suppress MixedInferredReturnType due to bug
*/
public function __get(string $property) {
return $this->data[$property];
@@ -2233,7 +2231,6 @@ class Collection implements ICollection {
public function __construct(array $data) {
$this->data = $data;
}
/** @psalm-suppress LessSpecificImplementedReturnType */
public function getIterator(): \Traversable {
return new \ArrayIterator($this->data);
}
@@ -392,7 +392,6 @@ class Collection {
/**
* @param Closure(TValue):bool $p
* @return Collection<TKey,TValue>
* @psalm-suppress MixedTypeCoercion
*/
public function filter(Closure $p) {
return $this;
@@ -432,7 +431,6 @@ class Collection implements ICollection {
public function __construct(array $data) {
$this->data = $data;
}
/** @psalm-suppress LessSpecificImplementedReturnType */
public function getIterator(): \Traversable {
return new \ArrayIterator($this->data);
}
@@ -162,7 +162,6 @@ public function bat(): void {
$f = getFoo();
/**
* @var mixed
* @psalm-suppress MixedAssignment
*/
$class = "hello";
@@ -470,7 +469,6 @@ public function is($x) : void {}
* @param Clazz<T2> $c
*
* @return T2
* @psalm-suppress MixedAssignment
*/
function example(Clazz $c) {
/** @var mixed */
@@ -504,7 +502,6 @@ public function is($x) : bool {
* @param Clazz<T2> $c
*
* @return T2|false
* @psalm-suppress MixedAssignment
*/
function example(Clazz $c) {
/** @var mixed */
@@ -92,7 +92,7 @@ public function addFile($file_path, $contents)
*
* @return void
*/
public function analyzeFile($file_path, \Psalm\Context $context)
public function analyzeFile($file_path, \Psalm\Context $context, bool $track_unused_suppressions = true)
{
$codebase = $this->project_analyzer->getCodebase();
$codebase->addFilesToAnalyze([$file_path => $file_path]);
@@ -105,6 +105,10 @@ public function analyzeFile($file_path, \Psalm\Context $context)
$this->project_analyzer->interpretRefactors();
}
if ($track_unused_suppressions) {
$this->project_analyzer->trackUnusedSuppressions();
}
$file_analyzer = new FileAnalyzer(
$this->project_analyzer,
$file_path,
@@ -120,7 +124,7 @@ public function analyzeFile($file_path, \Psalm\Context $context)
}
}
if ($codebase->track_unused_suppressions) {
if ($track_unused_suppressions) {
\Psalm\IssueBuffer::processUnusedSuppressions($codebase->file_provider);
}
}
@@ -159,54 +159,6 @@ function foo(int $x, int $y) : int {
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testNoThrowWhenSuppressing()
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @psalm-suppress MissingThrowsDocblock
*/
function foo() : void {
if (rand(0, 1)) {
throw new \UnexpectedValueException();
}
}'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testNoThrowWhenSuppressingInline()
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
function foo() : void {
if (rand(0, 1)) {
/** @psalm-suppress MissingThrowsDocblock */
throw new \UnexpectedValueException();
}
}'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
@@ -203,7 +203,7 @@ public function testUncaughtThrowWhenSuppressing()
$context = new Context();
$this->analyzeFile('somefile.php', $context);
$this->analyzeFile('somefile.php', $context, false);
}
/**
@@ -223,7 +223,7 @@ public function testUncaughtThrowInNamespaceWhenSuppressing()
$context = new Context();
$this->analyzeFile('somefile.php', $context);
$this->analyzeFile('somefile.php', $context, false);
}
/**
@@ -258,7 +258,7 @@ function foo(int $x, int $y) : int {
$context = new Context();
$this->analyzeFile('somefile.php', $context);
$this->analyzeFile('somefile.php', $context, false);
}
/**
@@ -294,7 +294,7 @@ function foo(int $x, int $y) : int {
$context = new Context();
$this->analyzeFile('somefile.php', $context);
$this->analyzeFile('somefile.php', $context, false);
}
/**

0 comments on commit 5d94e34

Please sign in to comment.
You can’t perform that action at this time.