Skip to content

Commit

Permalink
Merge pull request #7143 from klimick/allow-remove-recorded-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 12, 2021
2 parents 2a570fb + de0d3a3 commit 3627b3d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Psalm/Issue/CodeIssue.php
Expand Up @@ -86,20 +86,23 @@ public function getMessage(): string
return $this->message;
}

public static function getIssueType(): string
{
$fqcn_parts = explode('\\', static::class);
return array_pop($fqcn_parts);
}

public function toIssueData(string $severity): IssueData
{
$location = $this->code_location;
$selection_bounds = $location->getSelectionBounds();
$snippet_bounds = $location->getSnippetBounds();

$fqcn_parts = explode('\\', static::class);
$issue_type = array_pop($fqcn_parts);

return new IssueData(
$severity,
$location->getLineNumber(),
$location->getEndLineNumber(),
$issue_type,
static::getIssueType(),
$this->message,
$location->file_name,
$location->file_path,
Expand Down
20 changes: 20 additions & 0 deletions src/Psalm/IssueBuffer.php
Expand Up @@ -326,11 +326,31 @@ public static function add(CodeIssue $e, bool $is_fixable = false): bool
return true;
}

private static function removeRecordedIssue(string $issue_type, int $file_offset): void
{
$recorded_issues = self::$recorded_issues[self::$recording_level];
$filtered_issues = [];

foreach ($recorded_issues as $issue) {
[$from] = $issue->code_location->getSelectionBounds();

if ($issue::getIssueType() !== $issue_type || $from !== $file_offset) {
$filtered_issues[] = $issue;
}
}

self::$recorded_issues[self::$recording_level] = $filtered_issues;
}

/**
* This will try to remove an issue that has been added for emission
*/
public static function remove(string $file_path, string $issue_type, int $file_offset): void
{
if (self::$recording_level > 0) {
self::removeRecordedIssue($issue_type, $file_offset);
}

if (!isset(self::$issues_data[$file_path])) {
return;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Loop/ForeachTest.php
Expand Up @@ -430,6 +430,29 @@ function getStrings(): array {
'MixedAssignment',
],
],
'noMixedAssigmentWithIfAssertion' => [
'<?php
$object = new stdClass();
$reflection = new ReflectionClass($object);
foreach ($reflection->getProperties() as $property) {
$message = $property->getValue($reflection->newInstance());
if (!is_string($message)) {
throw new RuntimeException();
}
}',
],
'noMixedAssigmentWithAssertion' => [
'<?php
$object = new stdClass();
$reflection = new ReflectionClass($object);
foreach ($reflection->getProperties() as $property) {
$message = $property->getValue($reflection->newInstance());
assert(is_string($message));
}',
],
'nullToMixedWithNullCheckAndContinue' => [
'<?php
$a = null;
Expand Down

0 comments on commit 3627b3d

Please sign in to comment.