1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Allow null return on template with null as type

Ref #2466
This commit is contained in:
Matthew Brown 2019-12-13 21:56:43 -05:00
parent f40b6625eb
commit b48021dbfd
3 changed files with 38 additions and 2 deletions

View File

@ -568,6 +568,7 @@ class ReturnTypeAnalyzer
if (!$ignore_nullable_issues
&& $inferred_return_type->isNullable()
&& !$declared_return_type->isNullable()
&& !$declared_return_type->hasTemplate()
&& !$declared_return_type->isVoid()
) {
if ($codebase->alter_code

View File

@ -375,12 +375,13 @@ class ReturnAnalyzer
if (!$stmt_type->ignore_nullable_issues
&& $inferred_type->isNullable()
&& !$local_return_type->isNullable()
&& !$local_return_type->hasTemplate()
) {
if (IssueBuffer::accepts(
new NullableReturnStatement(
'The declared return type \'' . $local_return_type . '\' for '
'The declared return type \'' . $local_return_type->getId() . '\' for '
. $cased_method_id . ' is not nullable, but the function returns \''
. $inferred_type . '\'',
. $inferred_type->getId() . '\'',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()

View File

@ -2168,6 +2168,40 @@ class ClassTemplateTest extends TestCase
}
}'
],
'nullableTemplateAs' => [
'<?php
/**
* @template T of null|array
*/
class Foo
{
private ?\ArrayObject $arrayObject;
public function __construct(?\ArrayObject $arrayObject)
{
$this->arrayObject = $arrayObject;
}
/**
* @psalm-assert-if-true Foo<array> $this
* @psalm-assert-if-true ArrayObject $this->arrayObject
*/
public function hasArray(): bool
{
return $this->arrayObject instanceof \ArrayObject;
}
/** @return T */
public function toMaybeArray()
{
if ($this->hasArray()) {
return $this->arrayObject->getArrayCopy();
}
return null;
}
}'
],
];
}