Skip to content

Commit 93bd8d1

Browse files
EmilMasseyondrejmirtes
authored andcommitted
Add support for notBlank assertion
1 parent 6cd5128 commit 93bd8d1

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This extension specifies types of values passed to:
5050
* `Assertion::isJsonString`
5151
* `Assertion::keyExists`
5252
* `Assertion::keyNotExists`
53+
* `Assertion::notBlank`
5354
* `nullOr*` and `all*` variants of the above methods
5455

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

src/Type/BeberleiAssert/AssertHelper.php

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Closure;
66
use PhpParser\Node\Arg;
77
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\Array_;
9+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
810
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
911
use PhpParser\Node\Expr\BinaryOp\Identical;
1012
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
@@ -13,6 +15,7 @@
1315
use PhpParser\Node\Expr\FuncCall;
1416
use PhpParser\Node\Expr\Instanceof_;
1517
use PhpParser\Node\Name;
18+
use PhpParser\Node\Scalar\String_;
1619
use PHPStan\Analyser\Scope;
1720
use PHPStan\Analyser\SpecifiedTypes;
1821
use PHPStan\Analyser\TypeSpecifier;
@@ -403,6 +406,30 @@ private static function getExpressionResolvers(): array
403406
)
404407
);
405408
},
409+
'notBlank' => static function (Scope $scope, Arg $value): Expr {
410+
return new BooleanAnd(
411+
new BooleanAnd(
412+
new NotIdentical(
413+
$value->value,
414+
new ConstFetch(new Name('null'))
415+
),
416+
new NotIdentical(
417+
$value->value,
418+
new ConstFetch(new Name('false'))
419+
)
420+
),
421+
new BooleanAnd(
422+
new NotIdentical(
423+
$value->value,
424+
new String_('')
425+
),
426+
new NotIdentical(
427+
$value->value,
428+
new Array_()
429+
)
430+
)
431+
);
432+
},
406433
];
407434
}
408435

tests/Type/BeberleiAssert/data/data.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Foo
88
{
99

10-
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, string $n, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, ?string $ac)
10+
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, string $n, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, ?string $ac, ?string $ae, ?bool $af, array $ag)
1111
{
1212
\PHPStan\Testing\assertType('mixed', $a);
1313

@@ -138,6 +138,15 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
138138

139139
Assertion::keyNotExists($ad, 'b');
140140
\PHPStan\Testing\assertType("array{a: string}", $ad);
141+
142+
Assertion::notBlank($ae);
143+
\PHPStan\Testing\assertType('non-empty-string', $ae);
144+
145+
Assertion::notBlank($af);
146+
\PHPStan\Testing\assertType('true', $af);
147+
148+
Assertion::notBlank($ag);
149+
\PHPStan\Testing\assertType('non-empty-array', $ag);
141150
}
142151

143152
public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)

0 commit comments

Comments
 (0)