Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix lost type when assigning variable in method argument #4293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
staabm wants to merge 3 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from staabm:bug12234
Draft
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
17 changes: 10 additions & 7 deletions src/Rules/FunctionCallParametersCheck.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public function check(
$functionParametersMaxCount = -1;
}

if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

/** @var array<int, array{Expr, Type|null, bool, string|null, int}> $arguments */
$arguments = [];
/** @var array<int, Node\Arg> $args */
Expand All @@ -114,6 +118,10 @@ public function check(
$argumentName = $arg->name->toString();
}

if ($arg->value instanceof Expr\Assign) {
$scope = $scope->assignExpression($arg->value->var, $scope->getType($arg->value->expr), $scope->getNativeType($arg->value->expr));
}

if ($hasNamedArguments && $arg->unpack) {
$errors[] = RuleErrorBuilder::message('Named argument cannot be followed by an unpacked (...) argument.')
->identifier('argument.unpackAfterNamed')
Expand Down Expand Up @@ -318,14 +326,9 @@ public function check(
}

if ($argumentValueType === null) {
if ($scope instanceof MutatingScope) {
$scope = $scope->pushInFunctionCall(null, $parameter);
}
$scope = $scope->pushInFunctionCall(null, $parameter);
$argumentValueType = $scope->getType($argumentValue);

if ($scope instanceof MutatingScope) {
$scope = $scope->popInFunctionCall();
}
$scope = $scope->popInFunctionCall();
}

if (!$acceptsNamedArguments->yes()) {
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2317,4 +2317,11 @@ public function testBug12317(): void
]);
}

public function testBug12234(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12234.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12234.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug12234;

class Foo {
public function getSize(): int {
return 0;
}
}

function bar(Foo $foo, int $b): true {
return true;
}

$test = bar(
$foo = new Foo(),
$foo->getSize(),
);
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3656,4 +3656,26 @@ public function testBug3396(): void
$this->analyse([__DIR__ . '/data/bug-3396.php'], []);
}

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

$this->analyse([__DIR__ . '/data/bug-12735.php'], []);
}

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

$this->analyse([__DIR__ . '/data/bug-12735b.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12735.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bug12735;

class HelloWorld
{
public function test(): void
{
$this->foo(
$now = new DateTimeImmutable(),
$now,
);

$this->foo($now, $now);
}

private function foo(DateTimeImmutable $a, DateTimeImmutable $b): void {}
}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12735b.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Bug12735b;

use DateTimeImmutable;

class HelloWorld
{
public function test(): void
{
$now = null;
if (rand(0,1)) {
$now = new DateTimeImmutable();
}

$this->foo(
$now ??= new DateTimeImmutable(),
$now,
);

$this->foo($now, $now);
}

private function foo(DateTimeImmutable $a, DateTimeImmutable $b): void {}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /