Skip to content

Fix #5020: phpstan could handle $class::someMethod syntax better#5000

Open
staabm wants to merge 12 commits intophpstan:2.1.xfrom
staabm:bug5020
Open

Fix #5020: phpstan could handle $class::someMethod syntax better#5000
staabm wants to merge 12 commits intophpstan:2.1.xfrom
staabm:bug5020

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Feb 19, 2026

inspired by #4911

Fixes phpstan/phpstan#5020

ondrejmirtes and others added 2 commits February 19, 2026 18:28
- When $expr->class is an Expr (variable), resolve method reflection and
  parameters acceptor using getObjectTypeOrClassStringObjectType() so
  by-reference parameters and other argument analysis work correctly
- Guard $this invalidation and constructor property initialization blocks
  with $expr->class instanceof Name to prevent $other::__construct()
  from incorrectly affecting $this
- New regression test in tests/PHPStan/Analyser/nsrt/bug-5020.php
@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

@staabm staabm changed the base branch from 2.2.x to 2.1.x February 19, 2026 18:40
Comment on lines 45 to 48
[
'Call to static method DateTimeImmutable::createFromFormat() on a separate line has no effect.',
13,
],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense, that we see the error on line 13. I feel this one was missing before

public function doFoo(\DateTimeImmutable $dt)
{
DateTimeImmutable::createFromFormat('Y-m-d', '2019-07-24');
$dt::createFromFormat('Y-m-d', '2019-07-24');
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't related to the fact you can extend \DateTimeImmutable and override createFromFormat with impure method ?

Maybe not for DateTimeImmutable (dunno if it's final), but it seems possible for any non final class no ?
So we should add a test with a random non final class (?)

Copy link
Contributor Author

@staabm staabm Feb 19, 2026

Choose a reason for hiding this comment

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

I think you are right.

the code atm is understood as "static call to DateTimeImmutable::createFromFormat()" but not as a "late static binding call to $x::createFromFormat()".

and DateTimeImmutable::createFromFormat() did not throw on PHP7.x - therefore we get the test-error only on PHP 7.4

@staabm staabm marked this pull request as ready for review February 19, 2026 19:10
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

phpstan could handle $class::someMethod syntax better

4 participants

Comments