diff --git a/CHANGELOG.md b/CHANGELOG.md index 575456c..7b5fda3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`0.5.1...master`][0.5.1...master]. +For a full diff see [`0.6.0...master`][0.6.0...master]. + +## [`0.6.0`][0.6.0] + +For a full diff see [`0.5.1...0.6.0`][0.5.1...0.6.0]. ### Added * Support for `willImplement()` ([#92]), by [@localheinz] +* Support for `willExtend()` ([#94]), by [@localheinz] ## [`0.5.1`][0.5.1] @@ -70,6 +75,7 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1]. [0.4.2]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.4.2 [0.5.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.0 [0.5.1]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.1 +[0.6.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.0 [afd6fd9...0.1]: https://github.com/Jan0707/phpstan-prophecy/compare/afd6fd9...0.1 [0.1...0.1.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.1...0.1.1 @@ -81,11 +87,13 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1]. [0.4.1...0.4.2]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.1...0.4.2 [0.4.2...0.5.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.2...0.5.0 [0.5.0...0.5.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.0...0.5.1 -[0.5.1...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...master +[0.5.1...0.6.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...0.6.0 +[0.6.0...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.0...master [#67]: https://github.com/Jan0707/phpstan-prophecy/pull/67 [#79]: https://github.com/Jan0707/phpstan-prophecy/pull/79 [#92]: https://github.com/Jan0707/phpstan-prophecy/pull/92 +[#94]: https://github.com/Jan0707/phpstan-prophecy/pull/94 [@localheinz]: https://github.com/localheinz [@PedroTroller]: https://github.com/PedroTroller diff --git a/src/Extension/ObjectProphecyWillImplementDynamicReturnTypeExtension.php b/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php similarity index 85% rename from src/Extension/ObjectProphecyWillImplementDynamicReturnTypeExtension.php rename to src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php index c599a2b..6990234 100644 --- a/src/Extension/ObjectProphecyWillImplementDynamicReturnTypeExtension.php +++ b/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php @@ -13,7 +13,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeWithClassName; -class ObjectProphecyWillImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +class ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension { public function getClass(): string { @@ -22,7 +22,16 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection): bool { - return 'willImplement' === $methodReflection->getName(); + $methodNames = [ + 'willImplement', + 'willExtend', + ]; + + return \in_array( + $methodReflection->getName(), + $methodNames, + true + ); } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type diff --git a/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php b/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php index e89b4dc..2b91015 100644 --- a/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php +++ b/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php @@ -39,7 +39,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method $returnType = $parametersAcceptor->getReturnType(); if (0 === \count($methodCall->args)) { - return $returnType; + return new ObjectProphecyType(); } $argumentType = $scope->getType($methodCall->args[0]->value); diff --git a/src/Type/ObjectProphecyType.php b/src/Type/ObjectProphecyType.php index eb32e78..f982ffa 100644 --- a/src/Type/ObjectProphecyType.php +++ b/src/Type/ObjectProphecyType.php @@ -13,11 +13,9 @@ class ObjectProphecyType extends ObjectType */ protected $prophesizedClasses; - public function __construct(string $prophesizedClass) + public function __construct(string ...$prophesizedClasses) { - $this->prophesizedClasses = [ - $prophesizedClass, - ]; + $this->prophesizedClasses = $prophesizedClasses; parent::__construct('Prophecy\Prophecy\ObjectProphecy'); } diff --git a/src/extension.neon b/src/extension.neon index 8da66d7..9bb61f1 100644 --- a/src/extension.neon +++ b/src/extension.neon @@ -21,7 +21,7 @@ services: - phpstan.broker.dynamicMethodReturnTypeExtension - - class: JanGregor\Prophecy\Extension\ObjectProphecyWillImplementDynamicReturnTypeExtension + class: JanGregor\Prophecy\Extension\ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/tests/Model/BaseModel.php b/tests/Model/BaseModel.php index 0fc3d93..b1aaa62 100644 --- a/tests/Model/BaseModel.php +++ b/tests/Model/BaseModel.php @@ -39,4 +39,9 @@ public function bar(Bar $bar): string { return $bar->bar(); } + + public function baz(Baz $baz): string + { + return $baz->baz(); + } } diff --git a/tests/Model/Baz.php b/tests/Model/Baz.php new file mode 100644 index 0000000..610b19d --- /dev/null +++ b/tests/Model/Baz.php @@ -0,0 +1,10 @@ +doubleTheNumber(2)); } + public function testWillExtendWorks(): void + { + $baz = $this->prophesize()->willExtend(Baz::class); + + $baz + ->baz() + ->shouldBeCalled() + ->willReturn('Hmm'); + + $subject = new BaseModel(); + + self::assertSame('Hmm', $subject->baz($baz->reveal())); + } + public function testWillImplementWorks(): void { $fooThatAlsoBars = $this->prophesize(Foo::class);