Skip to content

Commit

Permalink
Support Assertion::allNotBlank() and thatAll($value)->notBlank()
Browse files Browse the repository at this point in the history
…chaining
  • Loading branch information
EmilMassey authored and ondrejmirtes committed Sep 4, 2023
1 parent 93bd8d1 commit 0918f2c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Type/BeberleiAssert/AssertHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use ReflectionObject;
use function array_key_exists;
use function count;
Expand Down Expand Up @@ -168,6 +173,25 @@ static function (Type $type) use ($valueType): Type {
);
}

if ($assertName === 'notBlank') {
return self::allArrayOrIterable(
$typeSpecifier,
$scope,
$args[0]->value,
static function (Type $type): Type {
return TypeCombinator::remove(
$type,
new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantStringType(''),
new ConstantArrayType([], []),
])
);
}
);
}

throw new ShouldNotHappenException();
}

Expand Down
1 change: 1 addition & 0 deletions src/Type/BeberleiAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function isStaticMethodSupported(
'allNotIsInstanceOf' => 2,
'allNotNull' => 1,
'allNotSame' => 2,
'allNotBlank' => 1,
];
return array_key_exists($staticMethodReflection->getName(), $methods)
&& count($node->getArgs()) >= $methods[$staticMethodReflection->getName()];
Expand Down
10 changes: 10 additions & 0 deletions tests/Type/BeberleiAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,

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

/** @var (string|null|bool|array)[] $notBlankValues */
$notBlankValues = doFoo();
Assertion::allNotBlank($notBlankValues);
\PHPStan\Testing\assertType('array<non-empty-array|non-empty-string|true>', $notBlankValues);
}

public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)
Expand Down Expand Up @@ -183,6 +188,11 @@ public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)
Assert::thatAll($f)->notNull();
\PHPStan\Testing\assertType('array<string>', $f);

/** @var (string|null|bool|array)[] $notBlankValues */
$notBlankValues = doFoo();
Assert::thatAll($notBlankValues)->notBlank();
\PHPStan\Testing\assertType('array<non-empty-array|non-empty-string|true>', $notBlankValues);

$assertThatFunction = \Assert\that($g);
\PHPStan\Testing\assertType('Assert\AssertionChain<mixed>', $assertThatFunction);

Expand Down

0 comments on commit 0918f2c

Please sign in to comment.