Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ parameters:
-
rawMessage: 'Doing instanceof PHPStan\Type\NullType is error-prone and deprecated. Use Type::isNull() instead.'
identifier: phpstanApi.instanceofType
count: 2
count: 1
path: src/Type/TypeCombinator.php

-
Expand Down
2 changes: 1 addition & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function containsNull(Type $type): bool
{
if ($type instanceof UnionType) {
foreach ($type->getTypes() as $innerType) {
if ($innerType instanceof NullType) {
if ($innerType->isNull()->yes()) {
return true;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,15 @@ public function testNullSafe(): void
]);
}

public function testNullSafeOnTemplateTypeNarrowedAgainstNull(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;

$this->analyse([__DIR__ . '/data/nullsafe-method-call-template-type.php'], []);
}

#[RequiresPhp('< 8.0.0')]
public function testDisallowNamedArguments(): void
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.0

namespace NullsafeMethodCallTemplateType;

class Foo
{

public function getValue(): ?string
{
return null;
}

}

/**
* @template T
* @param T $foo
*/
function foo($foo)
{
if ($foo !== null && !$foo instanceof Foo) {
throw new \Exception();
}

return $foo?->getValue();
}
Loading