From da19b55db1baa7a2ff41f8f8b129ef3230d0215c Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 6 Apr 2017 15:36:22 -0400 Subject: [PATCH] Add special TypeDoesNotContainNull issue as distinct from TypeDoesNotContainType --- config.xsd | 1 + .../Statements/Expression/AssertionFinder.php | 3 +- src/Psalm/Checker/TypeChecker.php | 32 +++++++++++++------ src/Psalm/Issue/TypeDoesNotContainNull.php | 6 ++++ tests/TypeReconciliationTest.php | 4 +-- 5 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 src/Psalm/Issue/TypeDoesNotContainNull.php diff --git a/config.xsd b/config.xsd index 2a3136547..3739c9ab0 100644 --- a/config.xsd +++ b/config.xsd @@ -157,6 +157,7 @@ + diff --git a/src/Psalm/Checker/Statements/Expression/AssertionFinder.php b/src/Psalm/Checker/Statements/Expression/AssertionFinder.php index fac0a9f8d..bc35fac57 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\Clause; use Psalm\CodeLocation; +use Psalm\Issue\TypeDoesNotContainNull; use Psalm\Issue\TypeDoesNotContainType; use Psalm\IssueBuffer; use Psalm\StatementsSource; @@ -134,7 +135,7 @@ class AssertionFinder $source->getFileChecker() )) { if (IssueBuffer::accepts( - new TypeDoesNotContainType( + new TypeDoesNotContainNull( $var_type . ' does not contain ' . $null_type, new CodeLocation($source, $conditional) ), diff --git a/src/Psalm/Checker/TypeChecker.php b/src/Psalm/Checker/TypeChecker.php index bb129e958..cd02fb8a1 100644 --- a/src/Psalm/Checker/TypeChecker.php +++ b/src/Psalm/Checker/TypeChecker.php @@ -5,6 +5,7 @@ use PhpParser; use Psalm\Checker\Statements\ExpressionChecker; use Psalm\CodeLocation; use Psalm\Issue\FailedTypeResolution; +use Psalm\Issue\TypeDoesNotContainNull; use Psalm\Issue\TypeDoesNotContainType; use Psalm\IssueBuffer; use Psalm\StatementsSource; @@ -367,15 +368,28 @@ class TypeChecker } if (!$has_match) { - if (IssueBuffer::accepts( - new TypeDoesNotContainType( - 'Cannot resolve types for ' . $key . ' - ' . $existing_var_type . - ' does not contain ' . $new_type, - $code_location - ), - $suppressed_issues - )) { - // fall through + if ($new_var_type === 'null') { + if (IssueBuffer::accepts( + new TypeDoesNotContainNull( + 'Cannot resolve types for ' . $key . ' - ' . $existing_var_type . + ' does not contain null', + $code_location + ), + $suppressed_issues + )) { + // fall through + } + } else { + if (IssueBuffer::accepts( + new TypeDoesNotContainType( + 'Cannot resolve types for ' . $key . ' - ' . $existing_var_type . + ' does not contain ' . $new_type, + $code_location + ), + $suppressed_issues + )) { + // fall through + } } $failed_reconciliation = true; diff --git a/src/Psalm/Issue/TypeDoesNotContainNull.php b/src/Psalm/Issue/TypeDoesNotContainNull.php new file mode 100644 index 000000000..a4f8b357d --- /dev/null +++ b/src/Psalm/Issue/TypeDoesNotContainNull.php @@ -0,0 +1,6 @@ +