Skip to content

Commit f58b46d

Browse files
Improve
1 parent fe51050 commit f58b46d

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ services:
5151
class: PHPStan\Type\PHPUnit\MockForIntersectionDynamicReturnTypeExtension
5252
tags:
5353
- phpstan.broker.dynamicMethodReturnTypeExtension
54+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
5455
-
5556
class: PHPStan\Rules\PHPUnit\CoversHelper
5657
-

src/Type/PHPUnit/MockForIntersectionDynamicReturnTypeExtension.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace PHPStan\Type\PHPUnit;
44

55
use PhpParser\Node\Expr\MethodCall;
6+
use PhpParser\Node\Expr\StaticCall;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
911
use PHPStan\Type\ObjectType;
1012
use PHPStan\Type\Type;
1113
use PHPStan\Type\TypeCombinator;
@@ -15,7 +17,7 @@
1517
use function count;
1618
use function in_array;
1719

18-
class MockForIntersectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
20+
class MockForIntersectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicStaticMethodReturnTypeExtension
1921
{
2022

2123
public function getClass(): string
@@ -35,9 +37,18 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3537
);
3638
}
3739

40+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
41+
{
42+
return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope);
43+
}
44+
3845
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3946
{
40-
$args = $methodCall->getArgs();
47+
return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope);
48+
}
49+
50+
private function getTypeFromCall(MethodReflection $methodReflection, array $args, Scope $scope): ?Type
51+
{
4152
if (!isset($args[0])) {
4253
return null;
4354
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Type\PHPUnit;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
use function method_exists;
9+
10+
class MockForIntersectionDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
11+
{
12+
13+
/** @return mixed[] */
14+
public static function dataFileAsserts(): iterable
15+
{
16+
if (method_exists(TestCase::class, 'createMockForIntersectionOfInterfaces')) { // @phpstan-ignore-line
17+
yield from self::gatherAssertTypes(__DIR__ . '/data/mock-for-intersection.php');
18+
}
19+
20+
return [];
21+
}
22+
23+
/**
24+
* @dataProvider dataFileAsserts
25+
* @param mixed ...$args
26+
*/
27+
#[DataProvider('dataFileAsserts')]
28+
public function testFileAsserts(
29+
string $assertType,
30+
string $file,
31+
...$args
32+
): void
33+
{
34+
$this->assertFileAsserts($assertType, $file, ...$args);
35+
}
36+
37+
public static function getAdditionalConfigFiles(): array
38+
{
39+
return [__DIR__ . '/../../../extension.neon'];
40+
}
41+
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace MockForIntersection;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class Foo extends TestCase
10+
{
11+
12+
public function inheritedAssertMethodsNarrowType(?string $s): void
13+
{
14+
assertType(
15+
'MockForIntersection\BarInterface&MockForIntersection\FooInterface&PHPUnit\Framework\MockObject\MockObject',
16+
$this->createMockForIntersectionOfInterfaces([FooInterface::class, BarInterface::class]),
17+
);
18+
assertType(
19+
'MockForIntersection\BarInterface&MockForIntersection\FooInterface&PHPUnit\Framework\MockObject\Stub',
20+
self::createStubForIntersectionOfInterfaces([FooInterface::class, BarInterface::class]),
21+
);
22+
}
23+
24+
}
25+
26+
interface FooInterface {}
27+
interface BarInterface {}

0 commit comments

Comments
 (0)