Call operator type specifying extensions for bitwise and arithmetic operators#5226
Call operator type specifying extensions for bitwise and arithmetic operators#5226Firehed wants to merge 2 commits intophpstan:2.1.xfrom
Conversation
…perators - Add extension calls to getBitwiseAndType, getBitwiseOrType, getBitwiseXorType - Move extension call to top of resolveCommonMath (before integer range optimization) - Remove duplicate extension call later in resolveCommonMath - Add TestBitwiseOperatorTypeSpecifyingExtension for testing bitwise extension calls - Add OperatorTypeSpecifyingExtensionTypeInferenceTest with tests for both bitwise (TestBitwiseOperand) and arithmetic (TestDecimal) operators This ensures operator type specifying extensions are called consistently for all supported operators, allowing custom types to specify operator return types. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
I believe the two failing tests are unrelated to this change based on other PRs I'm seeing and their associated error messages. |
| $leftType = $getTypeCallback($left); | ||
| $rightType = $getTypeCallback($right); | ||
|
|
||
| $specifiedTypes = $this->operatorTypeSpecifyingExtensionRegistryProvider->getRegistry() |
There was a problem hiding this comment.
this change only makes sense when getBitwiseAndTypeFromTypes is no longer public because otherwise you can by-pass extensions.
but it seems like getBitwiseAndTypeFromTypes is not called from anywhere outside this class and since InitializerExprTypeResolver is not @api-annotated, I think we can reduce visibility for such methods to private.
affects multiple methods in this class
There was a problem hiding this comment.
Please don’t change this, I’m pretty sure I use these in my refactoring at #5224
| use PHPStan\Fixture\TestDecimal; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // ============================================================================= |
There was a problem hiding this comment.
misses tests for shift left/right
|
I can get some extra tests added for bit-shifting. @staabm & @ondrejmirtes Just to make sure I'm following your comments correctly, should the logic in InitializerExprTypeResolver stay as-is? Sounds like changing method visibility per the original suggestion is out. |
|
Leave the visibility as is , right |
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
This PR ensures
OperatorTypeSpecifyingExtensionimplementations are called consistently for all supported binary operators:getBitwiseAndType,getBitwiseOrType,getBitwiseXorTyperesolveCommonMath(before integer range optimization)resolveCommonMathThis is prerequisite work for phpstan/phpstan#14288 (GMP operator type inference), split out per review feedback on #5223.
Testing approach
Per @ondrejmirtes' feedback:
This PR creates synthetic test extensions independent of any specific type (like GMP):
TestBitwiseOperand- A dummy fixture classTestBitwiseOperatorTypeSpecifyingExtension- ReturnsTestBitwiseOperandfor&,|,^operatorsTestDecimal+TestDecimalOperatorTypeSpecifyingExtensionfor arithmetic operatorsThe test verifies the resolver correctly invokes extensions for both bitwise and arithmetic operators. This ensures the resolver infrastructure remains tested even if specific extensions (like a future GMP extension) are removed.
Test plan
OperatorTypeSpecifyingExtensionTypeInferenceTestwith 8 assertionsGenerated with Claude Code