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

Class property issue suppression fixes. #7130

Merged
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
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