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

Post-assertions in loops #7143

Merged
merged 1 commit into from Dec 12, 2021
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
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