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

Fix #2306 - allow nullable templated params to be compared to null

This commit is contained in:
Matthew Brown 2019-11-05 18:37:46 -05:00
parent 818f0c0985
commit b81a2d3852
2 changed files with 36 additions and 3 deletions

View File

@ -721,11 +721,21 @@ class TypeAnalyzer
return false;
}
if ($input_type_part instanceof TNull && $container_type_part instanceof TNull) {
return true;
if ($input_type_part instanceof TNull) {
if ($container_type_part instanceof TNull) {
return true;
}
if ($container_type_part instanceof TTemplateParam
&& $container_type_part->as->isNullable()
) {
return true;
}
return false;
}
if ($input_type_part instanceof TNull || $container_type_part instanceof TNull) {
if ($container_type_part instanceof TNull) {
return false;
}

View File

@ -1943,6 +1943,29 @@ class ClassTemplateTest extends TestCase
return ["foo", "baz"];
}'
],
'allowInternalNullCheck' => [
'<?php
/**
* @template TP as ?scalar
*/
class Entity
{
/**
* @var TP
*/
private $parent;
/** @param TP $parent */
public function __construct($parent) {
$this->parent = $parent;
}
public function hasNoParent() : bool
{
return $this->parent === null; // So TP does contain null
}
}'
],
];
}