Skip to content
Permalink
Browse files

Allow null return on template with null as type

Ref #2466
  • Loading branch information
muglug committed Dec 14, 2019
1 parent f40b662 commit b48021dbfdc5d7eeafb5c80915c3e62127d7aea9
@@ -568,6 +568,7 @@ public static function verifyReturnType(
if (!$ignore_nullable_issues
&& $inferred_return_type->isNullable()
&& !$declared_return_type->isNullable()
&& !$declared_return_type->hasTemplate()
&& !$declared_return_type->isVoid()
) {
if ($codebase->alter_code
@@ -375,12 +375,13 @@ public static function analyze(
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()
@@ -2168,6 +2168,40 @@ public function f(): Base {
}
}'
],
'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;
}
}'
],
];
}

0 comments on commit b48021d

Please sign in to comment.
You can’t perform that action at this time.