Skip to content

Commit edd1acc

Browse files
committed
Support for Assert\that* functions
1 parent 526b403 commit edd1acc

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This extension specifies types of values passed to:
3232

3333
`Assert::that`, `Assert::thatNullOr` and `Assert::thatAll` chaining methods are also supported.
3434

35+
`Assert\that`, `Assert\thatNullOr` and `Assert\thatAll` functions are supported too.
36+
3537
## Usage
3638

3739
To use this extension, require it in [Composer](https://getcomposer.org/):

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ services:
1313
class: PHPStan\Type\BeberleiAssert\AssertThatDynamicMethodReturnTypeExtension
1414
tags:
1515
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
16+
-
17+
class: PHPStan\Type\BeberleiAssert\AssertThatFunctionDynamicReturnTypeExtension
18+
tags:
19+
- phpstan.broker.dynamicFunctionReturnTypeExtension
1620

1721
-
1822
class: PHPStan\Type\BeberleiAssert\AssertTypeSpecifyingExtension
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\BeberleiAssert;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\Reflection\FunctionReflection;
7+
8+
class AssertThatFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
9+
{
10+
11+
public function isFunctionSupported(
12+
FunctionReflection $functionReflection
13+
): bool
14+
{
15+
return in_array($functionReflection->getName(), [
16+
'Assert\\that',
17+
'Assert\\thatNullOr',
18+
'Assert\\thatAll',
19+
], true);
20+
}
21+
22+
public function getTypeFromFunctionCall(
23+
FunctionReflection $functionReflection,
24+
\PhpParser\Node\Expr\FuncCall $functionCall,
25+
Scope $scope
26+
): \PHPStan\Type\Type
27+
{
28+
if (count($functionCall->args) === 0) {
29+
return $functionReflection->getReturnType();
30+
}
31+
32+
$valueExpr = $functionCall->args[0]->value;
33+
$type = new AssertThatType($valueExpr, $scope->getType($valueExpr));
34+
if ($functionReflection->getName() === 'Assert\\thatNullOr') {
35+
return $type->toNullOr();
36+
} elseif ($functionReflection->getName() === 'Assert\\thatAll') {
37+
return $type->toAll();
38+
}
39+
40+
return $type;
41+
}
42+
43+
}

tests/Type/BeberleiAssert/AssertTypeSpecifyingExtensionTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function getDynamicStaticMethodReturnTypeExtensions(): array
5757
*/
5858
public function getDynamicFunctionReturnTypeExtensions(): array
5959
{
60-
return [];
60+
return [
61+
new AssertThatFunctionDynamicReturnTypeExtension(),
62+
];
6163
}
6264

6365
public function testExtension(): void
@@ -227,6 +229,18 @@ public function testExtension(): void
227229
'Variable $f is: array<string>',
228230
145,
229231
],
232+
[
233+
'Variable $assertThatFunction is: Assert\AssertionChain<mixed>',
234+
148,
235+
],
236+
[
237+
'Variable $assertThatNullOrFunction is: Assert\AssertionChain<mixed>-nullOr',
238+
151,
239+
],
240+
[
241+
'Variable $assertThatAllFunction is: Assert\AssertionChain<mixed>-all',
242+
154,
243+
],
230244
]);
231245
}
232246

tests/Type/BeberleiAssert/data/data.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
110110
$ab;
111111
}
112112

113-
public function doBar(?int $a, $b, $c, array $d, iterable $e)
113+
public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)
114114
{
115115
$that = Assert::that($a);
116116
$that;
@@ -143,6 +143,15 @@ public function doBar(?int $a, $b, $c, array $d, iterable $e)
143143
$f = doFoo();
144144
Assert::thatAll($f)->notNull();
145145
$f;
146+
147+
$assertThatFunction = \Assert\that($g);
148+
$assertThatFunction;
149+
150+
$assertThatNullOrFunction = \Assert\thatNullOr($g);
151+
$assertThatNullOrFunction;
152+
153+
$assertThatAllFunction = \Assert\thatAll($g);
154+
$assertThatAllFunction;
146155
}
147156

148157
}

0 commit comments

Comments
 (0)