Skip to content

Commit

Permalink
Add support for notBlank assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilMassey authored and ondrejmirtes committed Sep 1, 2023
1 parent 6cd5128 commit 93bd8d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This extension specifies types of values passed to:
* `Assertion::isJsonString`
* `Assertion::keyExists`
* `Assertion::keyNotExists`
* `Assertion::notBlank`
* `nullOr*` and `all*` variants of the above methods

`Assert::that`, `Assert::thatNullOr` and `Assert::thatAll` chaining methods are also supported.
Expand Down
27 changes: 27 additions & 0 deletions src/Type/BeberleiAssert/AssertHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Closure;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
Expand All @@ -13,6 +15,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
Expand Down Expand Up @@ -403,6 +406,30 @@ private static function getExpressionResolvers(): array
)
);
},
'notBlank' => static function (Scope $scope, Arg $value): Expr {
return new BooleanAnd(
new BooleanAnd(
new NotIdentical(
$value->value,
new ConstFetch(new Name('null'))
),
new NotIdentical(
$value->value,
new ConstFetch(new Name('false'))
)
),
new BooleanAnd(
new NotIdentical(
$value->value,
new String_('')
),
new NotIdentical(
$value->value,
new Array_()
)
)
);
},
];
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Type/BeberleiAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Foo
{

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)
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)
{
\PHPStan\Testing\assertType('mixed', $a);

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

Assertion::keyNotExists($ad, 'b');
\PHPStan\Testing\assertType("array{a: string}", $ad);

Assertion::notBlank($ae);
\PHPStan\Testing\assertType('non-empty-string', $ae);

Assertion::notBlank($af);
\PHPStan\Testing\assertType('true', $af);

Assertion::notBlank($ag);
\PHPStan\Testing\assertType('non-empty-array', $ag);
}

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

0 comments on commit 93bd8d1

Please sign in to comment.