1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add special TypeDoesNotContainNull issue as distinct from TypeDoesNotContainType

This commit is contained in:
Matt Brown 2017-04-06 15:36:22 -04:00
parent cb3f6b14ba
commit da19b55db1
5 changed files with 34 additions and 12 deletions

View File

@ -157,6 +157,7 @@
<xs:element name="TooFewArguments" type="IssueHandlerType" minOccurs="0" /> <xs:element name="TooFewArguments" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TooManyArguments" type="IssueHandlerType" minOccurs="0" /> <xs:element name="TooManyArguments" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TypeCoercion" type="IssueHandlerType" minOccurs="0" /> <xs:element name="TypeCoercion" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TypeDoesNotContainNull" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TypeDoesNotContainType" type="IssueHandlerType" minOccurs="0" /> <xs:element name="TypeDoesNotContainType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedClass" type="IssueHandlerType" minOccurs="0" /> <xs:element name="UndefinedClass" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedConstant" type="IssueHandlerType" minOccurs="0" /> <xs:element name="UndefinedConstant" type="IssueHandlerType" minOccurs="0" />

View File

@ -7,6 +7,7 @@ use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\TypeChecker; use Psalm\Checker\TypeChecker;
use Psalm\Clause; use Psalm\Clause;
use Psalm\CodeLocation; use Psalm\CodeLocation;
use Psalm\Issue\TypeDoesNotContainNull;
use Psalm\Issue\TypeDoesNotContainType; use Psalm\Issue\TypeDoesNotContainType;
use Psalm\IssueBuffer; use Psalm\IssueBuffer;
use Psalm\StatementsSource; use Psalm\StatementsSource;
@ -134,7 +135,7 @@ class AssertionFinder
$source->getFileChecker() $source->getFileChecker()
)) { )) {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new TypeDoesNotContainType( new TypeDoesNotContainNull(
$var_type . ' does not contain ' . $null_type, $var_type . ' does not contain ' . $null_type,
new CodeLocation($source, $conditional) new CodeLocation($source, $conditional)
), ),

View File

@ -5,6 +5,7 @@ use PhpParser;
use Psalm\Checker\Statements\ExpressionChecker; use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\CodeLocation; use Psalm\CodeLocation;
use Psalm\Issue\FailedTypeResolution; use Psalm\Issue\FailedTypeResolution;
use Psalm\Issue\TypeDoesNotContainNull;
use Psalm\Issue\TypeDoesNotContainType; use Psalm\Issue\TypeDoesNotContainType;
use Psalm\IssueBuffer; use Psalm\IssueBuffer;
use Psalm\StatementsSource; use Psalm\StatementsSource;
@ -367,6 +368,18 @@ class TypeChecker
} }
if (!$has_match) { if (!$has_match) {
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( if (IssueBuffer::accepts(
new TypeDoesNotContainType( new TypeDoesNotContainType(
'Cannot resolve types for ' . $key . ' - ' . $existing_var_type . 'Cannot resolve types for ' . $key . ' - ' . $existing_var_type .
@ -377,6 +390,7 @@ class TypeChecker
)) { )) {
// fall through // fall through
} }
}
$failed_reconciliation = true; $failed_reconciliation = true;
} }

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class TypeDoesNotContainNull extends CodeError
{
}

View File

@ -366,7 +366,7 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
/** /**
* @expectedException \Psalm\Exception\CodeException * @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TypeDoesNotContainType * @expectedExceptionMessage TypeDoesNotContainNull
* @return void * @return void
*/ */
public function testMakeNonNullableNull() public function testMakeNonNullableNull()
@ -437,7 +437,7 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
/** /**
* @expectedException \Psalm\Exception\CodeException * @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TypeDoesNotContainType * @expectedExceptionMessage TypeDoesNotContainNull
* @return void * @return void
*/ */
public function testStringIsNotNull() public function testStringIsNotNull()