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
25 changes: 23 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2653,7 +2653,7 @@ public function enterAnonymousFunction(
$scope->getNamespace(),
$scope->expressionTypes,
$scope->nativeExpressionTypes,
[],
$scope->conditionalExpressions,
$scope->inClosureBindScopeClasses,
$anonymousFunctionReflection,
true,
Expand Down Expand Up @@ -2703,12 +2703,14 @@ private function enterAnonymousFunctionWithoutReflection(
}

$nonRefVariableNames = [];
$useVariableNames = [];
foreach ($closure->uses as $use) {
if (!is_string($use->var->name)) {
throw new ShouldNotHappenException();
}
$variableName = $use->var->name;
$paramExprString = '$' . $use->var->name;
$useVariableNames[$paramExprString] = true;
if ($use->byRef) {
$holder = ExpressionTypeHolder::createYes($use->var, new MixedType());
$expressionTypes[$paramExprString] = $holder;
Expand Down Expand Up @@ -2774,14 +2776,33 @@ private function enterAnonymousFunctionWithoutReflection(
}
}

$filteredConditionalExpressions = [];
foreach ($this->conditionalExpressions as $conditionalExprString => $holders) {
if (!array_key_exists($conditionalExprString, $useVariableNames)) {
continue;
}
$filteredHolders = [];
foreach ($holders as $holder) {
foreach ($holder->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $useVariableNames)) {
continue 2;
}
}
$filteredHolders[] = $holder;
}
if ($filteredHolders !== []) {
$filteredConditionalExpressions[$conditionalExprString] = $filteredHolders;
}
}

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
array_merge($this->getConstantTypes(), $expressionTypes),
array_merge($this->getNativeConstantTypes(), $nativeTypes),
[],
$filteredConditionalExpressions,
$this->inClosureBindScopeClasses,
new ClosureType(),
true,
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12875.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug12875;

use function PHPStan\Testing\assertType;

interface HasFoo
{
public function foo(): int;
}

interface HasBar
{
public function bar(): int;
}

class HelloWorld
{
/**
* @param "foo"|"bar" $method
* @param ($method is "foo" ? HasFoo : HasBar) $a
* @param ($method is "foo" ? HasFoo : HasBar) $b
*/
public function add(string $method, HasFoo|HasBar $a, HasFoo|HasBar $b): void
{
assertType('int', $a->{$method}());
assertType('int', $b->{$method}());

$addInArrow = fn () => assertType('int', $a->{$method}());

$addInAnonymous = function () use ($a, $b, $method): void {
assertType('int', $a->{$method}());
assertType('int', $b->{$method}());
};
}
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3827,4 +3827,13 @@ public function testDiscussion14038(): void
$this->analyse([__DIR__ . '/data/discussion-14038.php'], []);
}

public function testBug12875(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12875.php'], []);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12875.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug12875;

interface HasFoo
{
public function foo(): int;
}

interface HasBar
{
public function bar(): int;
}

class HelloWorld
{
/**
* @param "foo"|"bar" $method
* @param ($method is "foo" ? HasFoo : HasBar) $a
* @param ($method is "foo" ? HasFoo : HasBar) $b
*/
public function add(string $method, HasFoo|HasBar $a, HasFoo|HasBar $b): void
{
$add = $a->{$method}() + $b->{$method}();
$addInArrow = fn () => $a->{$method}() + $b->{$method}();
$addInAnonymous = function () use ($a, $b, $method): int {
return $a->{$method}() + $b->{$method}();
};
}
}
Loading