Skip to content
Merged
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the same check in line 186?

the change fixes the initial error, because $innerType is a IntersectionType T&null

@vojtech-dobes vojtech-dobes Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm I ran the test suite on code where only :186 is changed to isNull()->yes() check, and such code still fails. So this line :178 is definitely important.

I haven't changed :186 purely in order to be very conservative and not touch what I don't have to :). I'm happy to change it too if you see no adverse consequences.

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