Skip to content

Commit

Permalink
Merge pull request #7130 from AndrolGenhald/bugfix/fix-class-property…
Browse files Browse the repository at this point in the history
…-suppression

Class property issue suppression fixes.
  • Loading branch information
orklah committed Dec 11, 2021
2 parents 858be40 + 51d9652 commit 0ba5b0b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1581,7 +1581,7 @@ private function checkForMissingPropertyType(
new CodeLocation($source, $stmt->props[0]->name),
$property_id
),
$this->source->getSuppressedIssues()
$this->source->getSuppressedIssues() + $property_storage->suppressed_issues
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Expand Up @@ -194,8 +194,10 @@ public static function arrayToDocblocks(
|| isset($parsed_docblock->tags['readonly'])
|| isset($parsed_docblock->tags['psalm-readonly'])
|| isset($parsed_docblock->tags['psalm-readonly-allow-private-mutation'])
|| isset($parsed_docblock->tags['psalm-allow-private-mutation'])
|| isset($parsed_docblock->tags['psalm-taint-escape'])
|| isset($parsed_docblock->tags['psalm-internal'])
|| isset($parsed_docblock->tags['psalm-suppress'])
|| $parsed_docblock->description)
) {
$var_comment = new VarDocblockComment();
Expand Down Expand Up @@ -245,7 +247,11 @@ private static function decorateVarDocblockComment(
}

if (isset($parsed_docblock->tags['psalm-suppress'])) {
$var_comment->suppressed_issues = $parsed_docblock->tags['psalm-suppress'];
foreach ($parsed_docblock->tags['psalm-suppress'] as $offset => $suppress_entry) {
foreach (DocComment::parseSuppressList($suppress_entry) as $issue_offset => $suppressed_issue) {
$var_comment->suppressed_issues[$issue_offset + $offset] = $suppressed_issue;
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Expand Up @@ -2159,7 +2159,7 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v
}
} elseif (IssueBuffer::accepts(
$issue,
$classlike_storage->suppressed_issues
$classlike_storage->suppressed_issues + $property_storage->suppressed_issues
)) {
// fall through
}
Expand Down Expand Up @@ -2189,7 +2189,7 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v
}
} elseif (IssueBuffer::accepts(
$issue,
$classlike_storage->suppressed_issues
$classlike_storage->suppressed_issues + $property_storage->suppressed_issues
)) {
// fall through
}
Expand Down
57 changes: 56 additions & 1 deletion tests/IssueSuppressionTest.php
Expand Up @@ -4,6 +4,7 @@
use Psalm\Config;
use Psalm\Context;
use Psalm\Exception\CodeException;
use Psalm\IssueBuffer;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

Expand Down Expand Up @@ -197,6 +198,50 @@ public function testUncaughtThrowInGlobalScopeSuppressedWithoutThrow(): void
$this->analyzeFile(getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php', $context);
}

public function testPossiblyUnusedPropertySuppressedOnClass(): void
{
$this->project_analyzer->getCodebase()->find_unused_code = "always";

$file_path = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php';
$this->addFile(
$file_path,
'<?php
/** @psalm-suppress PossiblyUnusedProperty */
class Foo {
public string $bar = "baz";
}
$foo = new Foo();
'
);

$this->analyzeFile($file_path, new Context(), false);
$this->project_analyzer->consolidateAnalyzedData();
IssueBuffer::processUnusedSuppressions($this->project_analyzer->getCodebase()->file_provider);
}

public function testPossiblyUnusedPropertySuppressedOnProperty(): void
{
$this->project_analyzer->getCodebase()->find_unused_code = "always";

$file_path = getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php';
$this->addFile(
$file_path,
'<?php
class Foo {
/** @psalm-suppress PossiblyUnusedProperty */
public string $bar = "baz";
}
$foo = new Foo();
'
);

$this->analyzeFile($file_path, new Context(), false);
$this->project_analyzer->consolidateAnalyzedData();
IssueBuffer::processUnusedSuppressions($this->project_analyzer->getCodebase()->file_provider);
}

/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
Expand Down Expand Up @@ -315,7 +360,17 @@ public function func(string $var) {
}
}
',
]
],
'missingPropertyTypeAtPropertyLevel' => [
'<?php
class Foo {
/**
* @psalm-suppress MissingPropertyType
*/
public $bar = "baz";
}
',
],
];
}

Expand Down

0 comments on commit 0ba5b0b

Please sign in to comment.