From 4de6f5c4ec90a1ac649a755fe283c97b7ce2757e Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 8 Feb 2020 23:01:45 -0500 Subject: [PATCH] Fix #2442 - using asserting after a `MixedAssignment` removes that error --- .../Expression/Call/FunctionCallAnalyzer.php | 8 ++++++++ .../Statements/Expression/CallAnalyzer.php | 8 ++++++++ src/Psalm/IssueBuffer.php | 17 +++++++++++++++++ tests/JsonOutputTest.php | 9 +++++++++ 4 files changed, 42 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index c4249e8fa..93d3ae596 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -682,6 +682,14 @@ class FunctionCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expressio ); foreach ($changed_var_ids as $var_id => $_) { + if ($first_appearance = $statements_analyzer->getFirstAppearance($var_id)) { + IssueBuffer::remove( + $statements_analyzer->getFilePath(), + 'MixedAssignment', + $first_appearance->raw_file_start + ); + } + if (isset($op_vars_in_scope[$var_id])) { $op_vars_in_scope[$var_id]->from_docblock = true; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index 19f121af6..9b988c79f 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -3596,6 +3596,14 @@ class CallAnalyzer foreach ($changed_var_ids as $var_id => $_) { if (isset($op_vars_in_scope[$var_id])) { + if ($first_appearance = $statements_analyzer->getFirstAppearance($var_id)) { + IssueBuffer::remove( + $statements_analyzer->getFilePath(), + 'MixedAssignment', + $first_appearance->raw_file_start + ); + } + $op_vars_in_scope[$var_id]->from_docblock = true; foreach ($op_vars_in_scope[$var_id]->getAtomicTypes() as $changed_atomic_type) { diff --git a/src/Psalm/IssueBuffer.php b/src/Psalm/IssueBuffer.php index afa0b8b59..8e78c6a66 100644 --- a/src/Psalm/IssueBuffer.php +++ b/src/Psalm/IssueBuffer.php @@ -240,6 +240,23 @@ class IssueBuffer return true; } + public static function remove(string $file_path, string $issue_type, int $file_offset) : void + { + if (!isset(self::$issues_data[$file_path])) { + return; + } + + $filtered_issues = []; + + foreach (self::$issues_data[$file_path] as $issue) { + if ($issue['type'] !== $issue_type || $issue['from'] !== $file_offset) { + $filtered_issues[] = $issue; + } + } + + self::$issues_data[$file_path] = $filtered_issues; + } + public static function addFixableIssue(string $issue_type) : void { if (isset(self::$fixable_issue_counts[$issue_type])) { diff --git a/tests/JsonOutputTest.php b/tests/JsonOutputTest.php index 51b2867d9..c6888a2e0 100644 --- a/tests/JsonOutputTest.php +++ b/tests/JsonOutputTest.php @@ -242,6 +242,15 @@ echo $a;'; 'line' => 6, 'error' => '"hello"', ], + 'assertCancelsMixedAssignment' => [ + ' "Found a redundant condition when evaluating docblock-defined type \$a and trying to reconcile type 'int' to int", + 'line' => 4, + 'error' => 'is_int($a)', + ], ]; } }