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
8 changes: 8 additions & 0 deletions src/Analyser/ExprHandler/AssignOpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PHPStan\Type\Type;
use function array_merge;
use function get_class;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -68,6 +69,13 @@ static function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $contex
$scope = $scope->filterByFalseyValue(
new BinaryOp\NotIdentical($expr->var, new ConstFetch(new Name('null'))),
);

if ($expr->var instanceof Expr\Variable && is_string($expr->var->name)) {
$context = $context->enterRightSideAssign(
$expr->var->name,
$expr->expr,
);
}
}

$exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
Expand Down
68 changes: 68 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13810.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13810;

use function PHPStan\Testing\assertType;

function doFoo(): void
{
static $isSupported;
assertType('mixed', $isSupported);
$isSupported ??= function (mixed $arg) use (&$isSupported): bool {
assertType('Closure(mixed): bool', $isSupported);
return $isSupported($arg);
};

assertType('mixed~null', $isSupported);
$isSupported('foo');
}

function doBar($isSupported): void
{
assertType('mixed', $isSupported);
$isSupported ??= function (mixed $arg) use (&$isSupported): bool {
assertType('Closure(mixed): bool', $isSupported);
return $isSupported($arg);
};

assertType('mixed~null', $isSupported);
$isSupported('foo');
}

function doFooBar(): void
{
$isSupported = null;
assertType('null', $isSupported);
$isSupported ??= function (mixed $arg) use (&$isSupported): bool {
assertType('Closure(mixed): bool', $isSupported);
return $isSupported($arg);
};

assertType('Closure(mixed): bool', $isSupported);
$isSupported('foo');
}

class HelloWorld
{
public function setValue(mixed $value): void
{
/** @var ?callable $isSupported */
static $isSupported = null;
$isSupported ??= function(mixed $arg) use (&$isSupported): bool {
if (is_array($arg)) {
foreach($arg as $value) {
if (!$isSupported($value)) {
return false;
}
}
return true;
}
return is_string($arg);
};
if (!$isSupported($value)) {
throw new InvalidArgumentException('only strings/string arrays are supported');
}
}
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,9 @@ public function testMaybeNotCallable(): void
$this->analyse([__DIR__ . '/data/maybe-not-callable.php'], $errors);
}

public function testBug13810(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-13810.php'], []);
}

}
Loading