Skip to content

Commit 73de662

Browse files
committed
Enhancement: Add support for willExtend()
1 parent 46b4f00 commit 73de662

8 files changed

+55
-10
lines changed

CHANGELOG.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9-
For a full diff see [`0.5.1...master`][0.5.1...master].
9+
For a full diff see [`0.6.0...master`][0.6.0...master].
10+
11+
## [`0.6.0`][0.6.0]
12+
13+
For a full diff see [`0.5.1...0.6.0`][0.5.1...0.6.0].
1014

1115
### Added
1216

1317
* Support for `willImplement()` ([#92]), by [@localheinz]
18+
* Support for `willExtend()` ([#94]), by [@localheinz]
1419

1520
## [`0.5.1`][0.5.1]
1621

@@ -70,6 +75,7 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
7075
[0.4.2]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.4.2
7176
[0.5.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.0
7277
[0.5.1]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.1
78+
[0.6.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.0
7379

7480
[afd6fd9...0.1]: https://github.com/Jan0707/phpstan-prophecy/compare/afd6fd9...0.1
7581
[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].
8187
[0.4.1...0.4.2]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.1...0.4.2
8288
[0.4.2...0.5.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.2...0.5.0
8389
[0.5.0...0.5.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.0...0.5.1
84-
[0.5.1...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...master
90+
[0.5.1...0.6.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...0.6.0
91+
[0.6.0...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.0...master
8592

8693
[#67]: https://github.com/Jan0707/phpstan-prophecy/pull/67
8794
[#79]: https://github.com/Jan0707/phpstan-prophecy/pull/79
8895
[#92]: https://github.com/Jan0707/phpstan-prophecy/pull/92
96+
[#94]: https://github.com/Jan0707/phpstan-prophecy/pull/94
8997

9098
[@localheinz]: https://github.com/localheinz
9199
[@PedroTroller]: https://github.com/PedroTroller

src/Extension/ObjectProphecyWillImplementDynamicReturnTypeExtension.php renamed to src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPStan\Type\Type;
1414
use PHPStan\Type\TypeWithClassName;
1515

16-
class ObjectProphecyWillImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
16+
class ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1717
{
1818
public function getClass(): string
1919
{
@@ -22,7 +22,16 @@ public function getClass(): string
2222

2323
public function isMethodSupported(MethodReflection $methodReflection): bool
2424
{
25-
return 'willImplement' === $methodReflection->getName();
25+
$methodNames = [
26+
'willImplement',
27+
'willExtend',
28+
];
29+
30+
return \in_array(
31+
$methodReflection->getName(),
32+
$methodNames,
33+
true
34+
);
2635
}
2736

2837
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type

src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3939
$returnType = $parametersAcceptor->getReturnType();
4040

4141
if (0 === \count($methodCall->args)) {
42-
return $returnType;
42+
return new ObjectProphecyType();
4343
}
4444

4545
$argumentType = $scope->getType($methodCall->args[0]->value);

src/Type/ObjectProphecyType.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ class ObjectProphecyType extends ObjectType
1313
*/
1414
protected $prophesizedClasses;
1515

16-
public function __construct(string $prophesizedClass)
16+
public function __construct(string ...$prophesizedClasses)
1717
{
18-
$this->prophesizedClasses = [
19-
$prophesizedClass,
20-
];
18+
$this->prophesizedClasses = $prophesizedClasses;
2119

2220
parent::__construct('Prophecy\Prophecy\ObjectProphecy');
2321
}

src/extension.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
- phpstan.broker.dynamicMethodReturnTypeExtension
2222

2323
-
24-
class: JanGregor\Prophecy\Extension\ObjectProphecyWillImplementDynamicReturnTypeExtension
24+
class: JanGregor\Prophecy\Extension\ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension
2525
tags:
2626
- phpstan.broker.dynamicMethodReturnTypeExtension
2727

tests/Model/BaseModel.php

+5
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public function bar(Bar $bar): string
3939
{
4040
return $bar->bar();
4141
}
42+
43+
public function baz(Baz $baz): string
44+
{
45+
return $baz->baz();
46+
}
4247
}

tests/Model/Baz.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JanGregor\Prophecy\Test\Model;
6+
7+
abstract class Baz
8+
{
9+
abstract public function baz(): string;
10+
}

tests/Test/BaseModelTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use JanGregor\Prophecy\Test\Model\Bar;
66
use JanGregor\Prophecy\Test\Model\BaseModel;
7+
use JanGregor\Prophecy\Test\Model\Baz;
78
use JanGregor\Prophecy\Test\Model\Foo;
89
use PHPUnit\Framework\TestCase;
910
use Prophecy\Argument;
@@ -62,6 +63,20 @@ public function testProphesizedAttributesShouldAlsoWork(): void
6263
self::assertEquals(5, $subject->doubleTheNumber(2));
6364
}
6465

66+
public function testWillExtendWorks(): void
67+
{
68+
$baz = $this->prophesize()->willExtend(Baz::class);
69+
70+
$baz
71+
->baz()
72+
->shouldBeCalled()
73+
->willReturn('Hmm');
74+
75+
$subject = new BaseModel();
76+
77+
self::assertSame('Hmm', $subject->baz($baz->reveal()));
78+
}
79+
6580
public function testWillImplementWorks(): void
6681
{
6782
$fooThatAlsoBars = $this->prophesize(Foo::class);

0 commit comments

Comments
 (0)