Skip to content
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

Handle BcMath\Number operators for simple cases #3787

Merged
merged 3 commits into from
Mar 2, 2025

Conversation

schlndh
Copy link
Contributor

@schlndh schlndh commented Jan 19, 2025

Partially fixes: phpstan/phpstan#12099
Fixes: phpstan/phpstan#7937, phpstan/phpstan#8555
It's loosely based on #3660
Here is a demonstration of how the operators behave with various types: https://3v4l.org/jYX5J#v8.4.3

I skipped the complicated stuff. The main issue is unions. IMO it's too difficult to handle unions correctly in the extension. It would probably make more sense for PHPStan to handle the unions itself and offload only non-unions to extensions (e.g. X|int + X|float needs to know what int + float is).

I also ignored a few preexisting bugs/inconsistencies (null and never types). They can be solved later.

assertType('true', $a or $b);
}

public function bcVsNever(Number $a): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a bunch of inconsistencies in how never type is handled. I assume that all of these operations should result in *NEVER*, but I'm ignoring it for this PR.

@schlndh schlndh marked this pull request as draft January 19, 2025 09:47
@schlndh schlndh marked this pull request as ready for review January 19, 2025 11:51
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes force-pushed the feature-bcmathNumberOperators branch from 90193e2 to e7063f3 Compare March 2, 2025 09:47
@ondrejmirtes
Copy link
Member

Hi, I'm sorry for taking such a long time to review this. I've separated part of this PR (253903a) because it's not really related to BcMath.


$this->analyse([__DIR__ . '/data/bcmath-number.php'], [
[
'Binary operation "<<" between BcMath\\Number and BcMath\\Number results in an error.',
Copy link
Member

Choose a reason for hiding this comment

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

Personally I'd remove this method altogether (testBcMathNumber). We don't need to test all of these situations "results in an error" because presumably it's handled by a single code path. If you still want to test this error, please do it with a separate test file only with a single or a few expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - it's sufficiently covered by NSRT. I removed the test case from InvalidBinaryOperationRuleTest and moved the file to NSRT directory.

@ondrejmirtes ondrejmirtes merged commit 6b0d8c3 into phpstan:2.1.x Mar 2, 2025
416 of 417 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@schlndh schlndh deleted the feature-bcmathNumberOperators branch March 2, 2025 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants