Skip to content

Commit 53e25ea

Browse files
committed
Fix lost type when assigning variable in method argument
1 parent e7da4dc commit 53e25ea

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public function check(
100100
$functionParametersMaxCount = -1;
101101
}
102102

103+
if (!$scope instanceof MutatingScope) {
104+
throw new ShouldNotHappenException();
105+
}
106+
103107
/** @var array<int, array{Expr, Type|null, bool, string|null, int}> $arguments */
104108
$arguments = [];
105109
/** @var array<int, Node\Arg> $args */
@@ -114,6 +118,10 @@ public function check(
114118
$argumentName = $arg->name->toString();
115119
}
116120

121+
if ($arg->value instanceof Expr\Assign) {
122+
$scope = $scope->assignExpression($arg->value->var, $scope->getType($arg->value->expr), $scope->getNativeType($arg->value->expr));
123+
}
124+
117125
if ($hasNamedArguments && $arg->unpack) {
118126
$errors[] = RuleErrorBuilder::message('Named argument cannot be followed by an unpacked (...) argument.')
119127
->identifier('argument.unpackAfterNamed')
@@ -318,14 +326,9 @@ public function check(
318326
}
319327

320328
if ($argumentValueType === null) {
321-
if ($scope instanceof MutatingScope) {
322-
$scope = $scope->pushInFunctionCall(null, $parameter);
323-
}
329+
$scope = $scope->pushInFunctionCall(null, $parameter);
324330
$argumentValueType = $scope->getType($argumentValue);
325-
326-
if ($scope instanceof MutatingScope) {
327-
$scope = $scope->popInFunctionCall();
328-
}
331+
$scope = $scope->popInFunctionCall();
329332
}
330333

331334
if (!$acceptsNamedArguments->yes()) {

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,4 +2317,11 @@ public function testBug12317(): void
23172317
]);
23182318
}
23192319

2320+
public function testBug12234(): void
2321+
{
2322+
$this->checkExplicitMixed = true;
2323+
$this->checkImplicitMixed = true;
2324+
$this->analyse([__DIR__ . '/data/bug-12234.php'], []);
2325+
}
2326+
23202327
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12234;
4+
5+
class Foo {
6+
public function getSize(): int {
7+
return 0;
8+
}
9+
}
10+
11+
function bar(Foo $foo, int $b): true {
12+
return true;
13+
}
14+
15+
$test = bar(
16+
$foo = new Foo(),
17+
$foo->getSize(),
18+
);

0 commit comments

Comments
 (0)