From 7cc74bf434b3c2190f7f4cfc08c74f7c0b65667f Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 10 Jun 2021 13:50:20 +0200 Subject: [PATCH] Fix BC promise bug --- extension.neon | 14 ------- phpcs.xml | 5 --- .../Reflection/StubMethodReflection.php | 12 +++++- .../StubMethodsClassReflectionExtension.php | 11 +++++- ...ionAfterStubDynamicReturnTypeExtension.php | 38 ------------------- tests/Mockery/DifferentFoo.php | 13 +++++++ tests/Mockery/DifferentNamespaceTest.php | 30 +++++++++++++++ 7 files changed, 63 insertions(+), 60 deletions(-) delete mode 100644 src/Mockery/Type/ExpectationAfterStubDynamicReturnTypeExtension.php create mode 100644 tests/Mockery/DifferentFoo.php create mode 100644 tests/Mockery/DifferentNamespaceTest.php diff --git a/extension.neon b/extension.neon index 87fb5a5..53777a4 100644 --- a/extension.neon +++ b/extension.neon @@ -40,20 +40,6 @@ services: stubInterfaceName: PHPStan\Mockery\Type\Expects stubMethodName: expects - - - class: PHPStan\Mockery\Type\ExpectationAfterStubDynamicReturnTypeExtension - tags: - - phpstan.broker.dynamicMethodReturnTypeExtension - arguments: - stubInterfaceName: PHPStan\Mockery\Type\Allows - - - - class: PHPStan\Mockery\Type\ExpectationAfterStubDynamicReturnTypeExtension - tags: - - phpstan.broker.dynamicMethodReturnTypeExtension - arguments: - stubInterfaceName: PHPStan\Mockery\Type\Expects - - class: PHPStan\Mockery\Type\MockDynamicReturnTypeExtension tags: diff --git a/phpcs.xml b/phpcs.xml index b69caf8..2ff7d00 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -56,11 +56,6 @@ - - - - - tests/*/data tests/tmp diff --git a/src/Mockery/Reflection/StubMethodReflection.php b/src/Mockery/Reflection/StubMethodReflection.php index 2ce8a04..749203f 100644 --- a/src/Mockery/Reflection/StubMethodReflection.php +++ b/src/Mockery/Reflection/StubMethodReflection.php @@ -4,9 +4,11 @@ use PHPStan\Reflection\ClassMemberReflection; use PHPStan\Reflection\ClassReflection; +use PHPStan\Reflection\FunctionVariant; use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\TrivialParametersAcceptor; use PHPStan\TrinaryLogic; +use PHPStan\Type\Generic\TemplateTypeMap; +use PHPStan\Type\ObjectType; class StubMethodReflection implements MethodReflection { @@ -59,7 +61,13 @@ public function getPrototype(): ClassMemberReflection public function getVariants(): array { return [ - new TrivialParametersAcceptor(), + new FunctionVariant( + TemplateTypeMap::createEmpty(), + TemplateTypeMap::createEmpty(), + [], + true, + new ObjectType('Mockery\\Expectation') + ), ]; } diff --git a/src/Mockery/Reflection/StubMethodsClassReflectionExtension.php b/src/Mockery/Reflection/StubMethodsClassReflectionExtension.php index 0589cf9..d5aea66 100644 --- a/src/Mockery/Reflection/StubMethodsClassReflectionExtension.php +++ b/src/Mockery/Reflection/StubMethodsClassReflectionExtension.php @@ -2,18 +2,24 @@ namespace PHPStan\Mockery\Reflection; +use Mockery\Expectation; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodsClassReflectionExtension; +use PHPStan\Reflection\ReflectionProvider; class StubMethodsClassReflectionExtension implements MethodsClassReflectionExtension { + /** @var ReflectionProvider */ + private $reflectionProvider; + /** @var string */ private $stubInterfaceName; - public function __construct(string $stubInterfaceName) + public function __construct(ReflectionProvider $reflectionProvider, string $stubInterfaceName) { + $this->reflectionProvider = $reflectionProvider; $this->stubInterfaceName = $stubInterfaceName; } @@ -24,6 +30,9 @@ public function hasMethod(ClassReflection $classReflection, string $methodName): public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection { + if ($this->reflectionProvider->hasClass(Expectation::class)) { + $classReflection = $this->reflectionProvider->getClass(Expectation::class); + } return new StubMethodReflection($classReflection, $methodName); } diff --git a/src/Mockery/Type/ExpectationAfterStubDynamicReturnTypeExtension.php b/src/Mockery/Type/ExpectationAfterStubDynamicReturnTypeExtension.php deleted file mode 100644 index 8903e5c..0000000 --- a/src/Mockery/Type/ExpectationAfterStubDynamicReturnTypeExtension.php +++ /dev/null @@ -1,38 +0,0 @@ -stubInterfaceName = $stubInterfaceName; - } - - public function getClass(): string - { - return $this->stubInterfaceName; - } - - public function isMethodSupported(MethodReflection $methodReflection): bool - { - return true; - } - - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type - { - return new ObjectType('Mockery\\Expectation'); - } - -} diff --git a/tests/Mockery/DifferentFoo.php b/tests/Mockery/DifferentFoo.php new file mode 100644 index 0000000..78f5b29 --- /dev/null +++ b/tests/Mockery/DifferentFoo.php @@ -0,0 +1,13 @@ +fooMock = \Mockery::mock(DifferentFoo::class); + } + + public function testWith(): void + { + $this->fooMock->expects('doFoo') + ->with(1) + ->andReturn(2); + + self::assertSame(2, $this->fooMock->doFoo(1)); + } + +}