Skip to content

Commit 193c0fa

Browse files
committed
Type-specifying extension for asserts (function and instance/static method calls)
1 parent 3e538dd commit 193c0fa

5 files changed

+394
-0
lines changed

extension.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ parameters:
66
- markTestSkipped
77

88
services:
9+
-
10+
class: PHPStan\Type\PHPUnit\Assert\AssertFunctionTypeSpecifyingExtension
11+
tags:
12+
- phpstan.typeSpecifier.functionTypeSpecifyingExtension
13+
-
14+
class: PHPStan\Type\PHPUnit\Assert\AssertMethodTypeSpecifyingExtension
15+
tags:
16+
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
17+
-
18+
class: PHPStan\Type\PHPUnit\Assert\AssertStaticMethodTypeSpecifyingExtension
19+
tags:
20+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
921
-
1022
class: PHPStan\Type\PHPUnit\CreateMockDynamicReturnTypeExtension
1123
tags:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\PHPUnit\Assert;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifier;
9+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
10+
use PHPStan\Analyser\TypeSpecifierContext;
11+
use PHPStan\Reflection\FunctionReflection;
12+
use PHPStan\Type\FunctionTypeSpecifyingExtension;
13+
14+
class AssertFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
15+
{
16+
17+
/** @var TypeSpecifier */
18+
private $typeSpecifier;
19+
20+
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
21+
{
22+
$this->typeSpecifier = $typeSpecifier;
23+
}
24+
25+
public function isFunctionSupported(
26+
FunctionReflection $functionReflection,
27+
FuncCall $node,
28+
TypeSpecifierContext $context
29+
): bool
30+
{
31+
return AssertTypeSpecifyingExtensionHelper::isSupported(
32+
$functionReflection->getName(),
33+
$node->args
34+
);
35+
}
36+
37+
public function specifyTypes(
38+
FunctionReflection $functionReflection,
39+
FuncCall $node,
40+
Scope $scope,
41+
TypeSpecifierContext $context
42+
): SpecifiedTypes
43+
{
44+
return AssertTypeSpecifyingExtensionHelper::specifyTypes(
45+
$this->typeSpecifier,
46+
$scope,
47+
$functionReflection->getName(),
48+
$node->args
49+
);
50+
}
51+
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\PHPUnit\Assert;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifier;
9+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
10+
use PHPStan\Analyser\TypeSpecifierContext;
11+
use PHPStan\Reflection\MethodReflection;
12+
use PHPStan\Type\MethodTypeSpecifyingExtension;
13+
14+
class AssertMethodTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
15+
{
16+
17+
/** @var TypeSpecifier */
18+
private $typeSpecifier;
19+
20+
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
21+
{
22+
$this->typeSpecifier = $typeSpecifier;
23+
}
24+
25+
public function getClass(): string
26+
{
27+
return \PHPUnit\Framework\TestCase::class;
28+
}
29+
30+
public function isMethodSupported(
31+
MethodReflection $methodReflection,
32+
MethodCall $node,
33+
TypeSpecifierContext $context
34+
): bool
35+
{
36+
return AssertTypeSpecifyingExtensionHelper::isSupported(
37+
$methodReflection->getName(),
38+
$node->args
39+
);
40+
}
41+
42+
public function specifyTypes(
43+
MethodReflection $functionReflection,
44+
MethodCall $node,
45+
Scope $scope,
46+
TypeSpecifierContext $context
47+
): SpecifiedTypes
48+
{
49+
return AssertTypeSpecifyingExtensionHelper::specifyTypes(
50+
$this->typeSpecifier,
51+
$scope,
52+
$functionReflection->getName(),
53+
$node->args
54+
);
55+
}
56+
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\PHPUnit\Assert;
4+
5+
use PhpParser\Node\Expr\StaticCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifier;
9+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
10+
use PHPStan\Analyser\TypeSpecifierContext;
11+
use PHPStan\Reflection\MethodReflection;
12+
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
13+
14+
class AssertStaticMethodTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
15+
{
16+
17+
/** @var TypeSpecifier */
18+
private $typeSpecifier;
19+
20+
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
21+
{
22+
$this->typeSpecifier = $typeSpecifier;
23+
}
24+
25+
public function getClass(): string
26+
{
27+
return \PHPUnit\Framework\TestCase::class;
28+
}
29+
30+
public function isStaticMethodSupported(
31+
MethodReflection $methodReflection,
32+
StaticCall $node,
33+
TypeSpecifierContext $context
34+
): bool
35+
{
36+
return AssertTypeSpecifyingExtensionHelper::isSupported(
37+
$methodReflection->getName(),
38+
$node->args
39+
);
40+
}
41+
42+
public function specifyTypes(
43+
MethodReflection $functionReflection,
44+
StaticCall $node,
45+
Scope $scope,
46+
TypeSpecifierContext $context
47+
): SpecifiedTypes
48+
{
49+
return AssertTypeSpecifyingExtensionHelper::specifyTypes(
50+
$this->typeSpecifier,
51+
$scope,
52+
$functionReflection->getName(),
53+
$node->args
54+
);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)