Skip to content

Commit ce87154

Browse files
committed
Generalizing extensions to accept parameters through constructor
1 parent 42eecea commit ce87154

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

extension.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ services:
33
class: PHPStan\Mockery\Reflection\StubMethodsClassReflectionExtension
44
tags:
55
- phpstan.broker.methodsClassReflectionExtension
6+
arguments:
7+
stubInterfaceName: PHPStan\Mockery\Type\Allows
68

79
-
810
class: PHPStan\Mockery\Type\StubDynamicReturnTypeExtension
911
tags:
1012
- phpstan.broker.dynamicMethodReturnTypeExtension
13+
arguments:
14+
stubInterfaceName: PHPStan\Mockery\Type\Allows
15+
stubMethodName: allows
1116

1217
-
1318
class: PHPStan\Mockery\Type\ExpectationAfterStubDynamicReturnTypeExtension
1419
tags:
1520
- phpstan.broker.dynamicMethodReturnTypeExtension
21+
arguments:
22+
stubInterfaceName: PHPStan\Mockery\Type\Allows
1623

1724
-
1825
class: PHPStan\Mockery\Type\MockDynamicReturnTypeExtension

src/Mockery/Reflection/StubMethodsClassReflectionExtension.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
namespace PHPStan\Mockery\Reflection;
44

5-
use PHPStan\Mockery\Type\Allows;
65
use PHPStan\Reflection\ClassReflection;
76
use PHPStan\Reflection\MethodReflection;
87
use PHPStan\Reflection\MethodsClassReflectionExtension;
98

109
class StubMethodsClassReflectionExtension implements MethodsClassReflectionExtension
1110
{
1211

12+
/** @var string */
13+
private $stubInterfaceName;
14+
15+
public function __construct(string $stubInterfaceName)
16+
{
17+
$this->stubInterfaceName = $stubInterfaceName;
18+
}
19+
1320
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
1421
{
15-
return $classReflection->getName() === Allows::class;
22+
return $classReflection->getName() === $this->stubInterfaceName;
1623
}
1724

1825
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection

src/Mockery/Type/ExpectationAfterStubDynamicReturnTypeExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
class ExpectationAfterStubDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1313
{
1414

15+
/** @var string */
16+
private $stubInterfaceName;
17+
18+
public function __construct(string $stubInterfaceName)
19+
{
20+
$this->stubInterfaceName = $stubInterfaceName;
21+
}
22+
1523
public function getClass(): string
1624
{
17-
return Allows::class;
25+
return $this->stubInterfaceName;
1826
}
1927

2028
public function isMethodSupported(MethodReflection $methodReflection): bool

src/Mockery/Type/StubDynamicReturnTypeExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@
1616
class StubDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1717
{
1818

19+
/** @var string */
20+
private $stubInterfaceName;
21+
22+
/** @var string */
23+
private $stubMethodName;
24+
25+
public function __construct(string $stubInterfaceName, string $stubMethodName)
26+
{
27+
$this->stubInterfaceName = $stubInterfaceName;
28+
$this->stubMethodName = $stubMethodName;
29+
}
30+
1931
public function getClass(): string
2032
{
2133
return 'Mockery\\MockInterface';
2234
}
2335

2436
public function isMethodSupported(MethodReflection $methodReflection): bool
2537
{
26-
return $methodReflection->getName() === 'allows';
38+
return $methodReflection->getName() === $this->stubMethodName;
2739
}
2840

2941
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
@@ -40,7 +52,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4052

4153
return TypeCombinator::intersect(
4254
new StubObjectType($mockedType->getClassName()),
43-
new ObjectType(Allows::class)
55+
new ObjectType($this->stubInterfaceName)
4456
);
4557
}
4658

0 commit comments

Comments
 (0)