From 4ee02985587f8103942e176b81c6eb00dad24637 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 18 Apr 2018 12:01:13 -0400 Subject: [PATCH] Always panic when two types can never be the same --- .../Statements/Expression/AssertionFinder.php | 61 +++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/src/Psalm/Checker/Statements/Expression/AssertionFinder.php b/src/Psalm/Checker/Statements/Expression/AssertionFinder.php index 63f086ffc..750349d06 100644 --- a/src/Psalm/Checker/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Checker/Statements/Expression/AssertionFinder.php @@ -7,6 +7,7 @@ use Psalm\Checker\Statements\ExpressionChecker; use Psalm\Checker\TypeChecker; use Psalm\CodeLocation; use Psalm\FileSource; +use Psalm\Issue\DocblockTypeContradiction; use Psalm\Issue\RedundantCondition; use Psalm\Issue\TypeDoesNotContainNull; use Psalm\Issue\TypeDoesNotContainType; @@ -378,7 +379,9 @@ class AssertionFinder if ($var_type) { if ($var_name) { $if_types[$var_name] = '^' . $var_type; - } elseif ($other_type + } + + if ($other_type && $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical && $source instanceof StatementsSource && $project_checker @@ -394,14 +397,26 @@ class AssertionFinder $var_type, true )) { - if (IssueBuffer::accepts( - new TypeDoesNotContainType( - $var_type . ' does not contain ' . $other_type, - new CodeLocation($source, $conditional) - ), - $source->getSuppressedIssues() - )) { - // fall through + if ($var_type->from_docblock || $other_type->from_docblock) { + if (IssueBuffer::accepts( + new DocblockTypeContradiction( + $var_type . ' does not contain ' . $other_type, + new CodeLocation($source, $conditional) + ), + $source->getSuppressedIssues() + )) { + // fall through + } + } else { + if (IssueBuffer::accepts( + new TypeDoesNotContainType( + $var_type . ' does not contain ' . $other_type, + new CodeLocation($source, $conditional) + ), + $source->getSuppressedIssues() + )) { + // fall through + } } } } @@ -669,14 +684,26 @@ class AssertionFinder $var_type, true )) { - if (IssueBuffer::accepts( - new RedundantCondition( - $var_type . ' can never contain ' . $other_type, - new CodeLocation($source, $conditional) - ), - $source->getSuppressedIssues() - )) { - // fall through + if ($var_type->from_docblock || $other_type->from_docblock) { + if (IssueBuffer::accepts( + new DocblockTypeContradiction( + $var_type . ' can never contain ' . $other_type, + new CodeLocation($source, $conditional) + ), + $source->getSuppressedIssues() + )) { + // fall through + } + } else { + if (IssueBuffer::accepts( + new RedundantCondition( + $var_type . ' can never contain ' . $other_type, + new CodeLocation($source, $conditional) + ), + $source->getSuppressedIssues() + )) { + // fall through + } } } }