Skip to content

Fix BcMath\Number unary plus/minus/bitwise-not#4980

Open
mpesari wants to merge 1 commit intophpstan:2.1.xfrom
mpesari:bcmath-unary-ops
Open

Fix BcMath\Number unary plus/minus/bitwise-not#4980
mpesari wants to merge 1 commit intophpstan:2.1.xfrom
mpesari:bcmath-unary-ops

Conversation

@mpesari
Copy link
Contributor

@mpesari mpesari commented Feb 17, 2026

Fixes phpstan/phpstan#13965

Returning ObjectType('BcMath\Number') from toNumber() fixed +$x and -$x but caused other tests to fail in these operations (https://github.com/phpstan/phpstan-src/blob/2.1.x/tests/PHPStan/Analyser/nsrt/bcmath-number.php)

  • $a & $b
  • $a | $b
  • $a ^ $b
  • $a << $b
  • $a >> $b

This is fixed by checking for BcMath\Number early and returning ErrorType before calling toNumber() when handling those bitwise operations (getBitwiseAndTypeFromTypes() etc.)

I'm not sure if the *ERROR* -> *NEVER* changes in tests fix something or break something?

@VincentLanglet
Copy link
Contributor

I'm not sure it's the right way to do it see
phpstan/phpstan#13965 (comment)

Given the fact we have a BcMathNumberOperatorTypeSpecifyingExtension I don't see why we would have the unary operator hardcoded.

So maybe the best way to do it the same way is to

  • introduce UnaryOperatorTypeSpecifyingExtension
  • implements it for BCMathNumber
  • Introduce UnaryOperatorTypeSpecifyingExtensionRegistry and provider (or add a param to the construct of OperatorTypeSpecifyingExtensionRegistry)
  • Use it in the InitializerExprTypeResolver the same way it's already used.

But I'd like the confirmation from someone else

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.

2 participants