-
Notifications
You must be signed in to change notification settings - Fork 53
Add support for createMockForIntersectionOfInterfaces #272
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
ondrejmirtes
merged 7 commits into
phpstan:2.0.x
from
VincentLanglet:createMockForIntersectionOfInterfaces
Feb 10, 2026
+212
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd1a5b1
Support for createMockForIntersectionOfInterfaces
VincentLanglet 0a159e2
Fix
VincentLanglet fa3fa4b
Improve
VincentLanglet 78e8820
Test
VincentLanglet bcaf46c
Identifier
VincentLanglet c88dbd0
Remove strtolower
VincentLanglet a4578dc
Nit
VincentLanglet 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
91 changes: 91 additions & 0 deletions
91
src/Type/PHPUnit/MockForIntersectionDynamicReturnTypeExtension.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,91 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\PHPUnit; | ||
|
|
||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\MockObject\Stub; | ||
| use PHPUnit\Framework\TestCase; | ||
| use function count; | ||
|
|
||
| class MockForIntersectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicStaticMethodReturnTypeExtension | ||
| { | ||
|
|
||
| public function getClass(): string | ||
| { | ||
| return TestCase::class; | ||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->getName() === 'createMockForIntersectionOfInterfaces'; | ||
| } | ||
|
|
||
| public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->getName() === 'createStubForIntersectionOfInterfaces'; | ||
| } | ||
|
|
||
| public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
| { | ||
| return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope); | ||
| } | ||
|
|
||
| public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
| { | ||
| return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<Arg> $args | ||
| */ | ||
| private function getTypeFromCall(MethodReflection $methodReflection, array $args, Scope $scope): ?Type | ||
| { | ||
| if (!isset($args[0])) { | ||
| return null; | ||
| } | ||
|
|
||
| $interfaces = $scope->getType($args[0]->value); | ||
| $constantArrays = $interfaces->getConstantArrays(); | ||
| if (count($constantArrays) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $constantArray = $constantArrays[0]; | ||
| if (count($constantArray->getOptionalKeys()) > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| $result = []; | ||
| if ($methodReflection->getName() === 'createMockForIntersectionOfInterfaces') { | ||
| $result[] = new ObjectType(MockObject::class); | ||
| } else { | ||
| $result[] = new ObjectType(Stub::class); | ||
| } | ||
|
|
||
| foreach ($constantArray->getValueTypes() as $valueType) { | ||
| if (!$valueType->isClassString()->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| $values = $valueType->getConstantScalarValues(); | ||
| if (count($values) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $result[] = new ObjectType((string) $values[0]); | ||
VincentLanglet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return TypeCombinator::intersect(...$result); | ||
| } | ||
|
|
||
| } | ||
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
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
42 changes: 42 additions & 0 deletions
42
tests/Type/PHPUnit/MockForIntersectionDynamicReturnTypeExtensionTest.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,42 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\PHPUnit; | ||
|
|
||
| use PHPStan\Testing\TypeInferenceTestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use function method_exists; | ||
|
|
||
| class MockForIntersectionDynamicReturnTypeExtensionTest extends TypeInferenceTestCase | ||
| { | ||
|
|
||
| /** @return mixed[] */ | ||
| public static function dataFileAsserts(): iterable | ||
| { | ||
| if (method_exists(TestCase::class, 'createMockForIntersectionOfInterfaces')) { // @phpstan-ignore function.alreadyNarrowedType | ||
| yield from self::gatherAssertTypes(__DIR__ . '/data/mock-for-intersection.php'); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider dataFileAsserts | ||
| * @param mixed ...$args | ||
| */ | ||
| #[DataProvider('dataFileAsserts')] | ||
| public function testFileAsserts( | ||
| string $assertType, | ||
| string $file, | ||
| ...$args | ||
| ): void | ||
| { | ||
| $this->assertFileAsserts($assertType, $file, ...$args); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [__DIR__ . '/../../../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,37 @@ | ||
| <?php | ||
|
|
||
| namespace MockForIntersection; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo extends TestCase | ||
| { | ||
|
|
||
| public function testFoo(bool $bool): void | ||
| { | ||
| assertType( | ||
| 'MockForIntersection\BarInterface&MockForIntersection\FooInterface&PHPUnit\Framework\MockObject\MockObject', | ||
| $this->createMockForIntersectionOfInterfaces([FooInterface::class, BarInterface::class]), | ||
| ); | ||
| assertType( | ||
| 'MockForIntersection\BarInterface&MockForIntersection\FooInterface&PHPUnit\Framework\MockObject\Stub', | ||
| self::createStubForIntersectionOfInterfaces([FooInterface::class, BarInterface::class]), | ||
| ); | ||
|
|
||
|
|
||
| assertType( | ||
| 'PHPUnit\Framework\MockObject\MockObject', | ||
| $this->createMockForIntersectionOfInterfaces($bool ? [FooInterface::class, BarInterface::class] : [FooInterface::class]), | ||
| ); | ||
| assertType( | ||
| 'PHPUnit\Framework\MockObject\MockObject', | ||
| $this->createMockForIntersectionOfInterfaces($bool ? [FooInterface::class] : [BarInterface::class]), | ||
| ); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| interface FooInterface {} | ||
| interface BarInterface {} |
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.