-
Notifications
You must be signed in to change notification settings - Fork 555
Call operator type specifying extensions for bitwise and arithmetic operators #5226
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
Merged
VincentLanglet
merged 2 commits into
phpstan:2.1.x
from
Firehed:operator-extension-resolver-fix
Mar 19, 2026
+185
−6
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/PHPStan/Analyser/OperatorTypeSpecifyingExtensionTypeInferenceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Analyser; | ||
|
|
||
| use PHPStan\Testing\TypeInferenceTestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| class OperatorTypeSpecifyingExtensionTypeInferenceTest extends TypeInferenceTestCase | ||
| { | ||
|
|
||
| public static function dataAsserts(): iterable | ||
| { | ||
| yield from self::gatherAssertTypes(__DIR__ . '/data/operator-type-specifying-extension.php'); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed ...$args | ||
| */ | ||
| #[DataProvider('dataAsserts')] | ||
| public function testAsserts( | ||
| string $assertType, | ||
| string $file, | ||
| ...$args, | ||
| ): void | ||
| { | ||
| $this->assertFileAsserts($assertType, $file, ...$args); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [ | ||
| __DIR__ . '/operator-type-specifying-extension.neon', | ||
| ]; | ||
| } | ||
|
|
||
| } |
65 changes: 65 additions & 0 deletions
65
tests/PHPStan/Analyser/data/operator-type-specifying-extension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace OperatorExtensionTest; | ||
|
|
||
| use PHPStan\Fixture\TestBitwiseOperand; | ||
| use PHPStan\Fixture\TestDecimal; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // ============================================================================= | ||
VincentLanglet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Bitwise operator extension tests | ||
| // ============================================================================= | ||
|
|
||
| function testBitwiseAnd(TestBitwiseOperand $a, TestBitwiseOperand $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestBitwiseOperand', $a & $b); | ||
| } | ||
|
|
||
| function testBitwiseOr(TestBitwiseOperand $a, TestBitwiseOperand $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestBitwiseOperand', $a | $b); | ||
| } | ||
|
|
||
| function testBitwiseXor(TestBitwiseOperand $a, TestBitwiseOperand $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestBitwiseOperand', $a ^ $b); | ||
| } | ||
|
|
||
| function testShiftLeft(TestBitwiseOperand $a, TestBitwiseOperand $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestBitwiseOperand', $a << $b); | ||
| } | ||
|
|
||
| function testShiftRight(TestBitwiseOperand $a, TestBitwiseOperand $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestBitwiseOperand', $a >> $b); | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // Arithmetic operator extension tests (via TestDecimal) | ||
| // ============================================================================= | ||
|
|
||
| function testArithmeticAdd(TestDecimal $a, TestDecimal $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestDecimal', $a + $b); | ||
| } | ||
|
|
||
| function testArithmeticSub(TestDecimal $a, TestDecimal $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestDecimal', $a - $b); | ||
| } | ||
|
|
||
| function testArithmeticMul(TestDecimal $a, TestDecimal $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestDecimal', $a * $b); | ||
| } | ||
|
|
||
| function testArithmeticDiv(TestDecimal $a, TestDecimal $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestDecimal', $a / $b); | ||
| } | ||
|
|
||
| function testArithmeticPow(TestDecimal $a, TestDecimal $b): void | ||
| { | ||
| assertType('PHPStan\Fixture\TestDecimal', $a ** $b); | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
tests/PHPStan/Analyser/operator-type-specifying-extension.neon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| services: | ||
| - | ||
| class: PHPStan\Type\TestBitwiseOperatorTypeSpecifyingExtension | ||
| tags: | ||
| - phpstan.broker.operatorTypeSpecifyingExtension | ||
| - | ||
| class: PHPStan\Type\TestDecimalOperatorTypeSpecifyingExtension | ||
| tags: | ||
| - phpstan.broker.operatorTypeSpecifyingExtension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Fixture; | ||
|
|
||
| /** | ||
| * Test fixture for verifying bitwise operator type specifying extensions. | ||
| */ | ||
| final class TestBitwiseOperand | ||
| { | ||
|
|
||
| } |
28 changes: 28 additions & 0 deletions
28
tests/PHPStan/Type/TestBitwiseOperatorTypeSpecifyingExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type; | ||
|
|
||
| use PHPStan\Fixture\TestBitwiseOperand; | ||
| use function in_array; | ||
|
|
||
| /** | ||
| * Test extension for verifying that bitwise operators call type specifying extensions. | ||
| */ | ||
| final class TestBitwiseOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension | ||
| { | ||
|
|
||
| public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool | ||
| { | ||
| $testType = new ObjectType(TestBitwiseOperand::class); | ||
|
|
||
| return in_array($operatorSigil, ['&', '|', '^', '<<', '>>'], true) | ||
| && $testType->isSuperTypeOf($leftSide)->yes() | ||
| && $testType->isSuperTypeOf($rightSide)->yes(); | ||
| } | ||
|
|
||
| public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type | ||
| { | ||
| return new ObjectType(TestBitwiseOperand::class); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.