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 MethodExistsTypeSpecifyingExtension for union types #4150

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 9 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from staabm:mexists

Conversation

Copy link
Contributor

@staabm staabm commented Jul 23, 2025

@staabm staabm marked this pull request as ready for review July 23, 2025 09:01
Copy link
Collaborator

This pull request has been marked as ready for review.

Copy link
Contributor Author

staabm commented Jul 28, 2025
edited
Loading

@liamduckett I just realized that the fix of this PR produces a false-positive (see the newly added testcase).
I do not yet know how to fix the initial problem without also regressing the other example.

just to let you know, as #4153 has the same problem

liamduckett reacted with thumbs up emoji

@staabm staabm marked this pull request as draft July 28, 2025 06:30
Comment on lines +12 to +14
if (!method_exists($bar, $method)) {
throw new \Exception;
}
Copy link
Contributor Author

@staabm staabm Jul 28, 2025
edited
Loading

Choose a reason for hiding this comment

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

I think the we could build a better type in a TypeSpecifyingExtension if the TypeSpecifierContext would contain information, whether the context is always-terminating.

Copy link

@liamduckett I just realized that the fix of this PR produces a false-positive (see the newly added testcase). I do not yet know how to fix the initial problem without also regressing the other example.

just to let you know, as #4153 has the same problem

Thanks. I'm not sure I understand the new test case (probably due to my inexperience) - why would $bar being narrowed here, be a problem?

Copy link
Contributor Author

staabm commented Jul 29, 2025

the initial types were wrongly narrowed:

/**
 * @param 'quux'|'qux' $constUnion
 */
function fooBar(object $bar, string $constUnion): void
{
	if (!method_exists($bar, $constUnion)) {
		throw new \Exception;
	}
 // $bar does not have both methods, it has only either 'quux' or 'qux' but not both
}

while in the initial loop check example it is correct that $bar will have both methods at once:

foreach (['qux', 'quux'] as $method) {
		assertType("'quux'|'qux'", $method);
		if (!method_exists($bar, $method)) {
			throw new \Exception;
		}
}
liamduckett reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

dynamic function names (as variables) not recognizes in certain cases

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