Skip to content

Commit 0918f2c

Browse files
EmilMasseyondrejmirtes
authored andcommitted
Support Assertion::allNotBlank() and thatAll($value)->notBlank() chaining
1 parent 93bd8d1 commit 0918f2c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Diff for: src/Type/BeberleiAssert/AssertHelper.php

+24
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
use PHPStan\Analyser\TypeSpecifierContext;
2323
use PHPStan\ShouldNotHappenException;
2424
use PHPStan\Type\ArrayType;
25+
use PHPStan\Type\Constant\ConstantArrayType;
2526
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
27+
use PHPStan\Type\Constant\ConstantBooleanType;
28+
use PHPStan\Type\Constant\ConstantStringType;
2629
use PHPStan\Type\IterableType;
2730
use PHPStan\Type\MixedType;
2831
use PHPStan\Type\NeverType;
32+
use PHPStan\Type\NullType;
2933
use PHPStan\Type\ObjectType;
3034
use PHPStan\Type\StringType;
3135
use PHPStan\Type\Type;
3236
use PHPStan\Type\TypeCombinator;
37+
use PHPStan\Type\UnionType;
3338
use ReflectionObject;
3439
use function array_key_exists;
3540
use function count;
@@ -168,6 +173,25 @@ static function (Type $type) use ($valueType): Type {
168173
);
169174
}
170175

176+
if ($assertName === 'notBlank') {
177+
return self::allArrayOrIterable(
178+
$typeSpecifier,
179+
$scope,
180+
$args[0]->value,
181+
static function (Type $type): Type {
182+
return TypeCombinator::remove(
183+
$type,
184+
new UnionType([
185+
new NullType(),
186+
new ConstantBooleanType(false),
187+
new ConstantStringType(''),
188+
new ConstantArrayType([], []),
189+
])
190+
);
191+
}
192+
);
193+
}
194+
171195
throw new ShouldNotHappenException();
172196
}
173197

Diff for: src/Type/BeberleiAssert/AssertTypeSpecifyingExtension.php

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function isStaticMethodSupported(
4242
'allNotIsInstanceOf' => 2,
4343
'allNotNull' => 1,
4444
'allNotSame' => 2,
45+
'allNotBlank' => 1,
4546
];
4647
return array_key_exists($staticMethodReflection->getName(), $methods)
4748
&& count($node->getArgs()) >= $methods[$staticMethodReflection->getName()];

Diff for: tests/Type/BeberleiAssert/data/data.php

+10
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
147147

148148
Assertion::notBlank($ag);
149149
\PHPStan\Testing\assertType('non-empty-array', $ag);
150+
151+
/** @var (string|null|bool|array)[] $notBlankValues */
152+
$notBlankValues = doFoo();
153+
Assertion::allNotBlank($notBlankValues);
154+
\PHPStan\Testing\assertType('array<non-empty-array|non-empty-string|true>', $notBlankValues);
150155
}
151156

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

191+
/** @var (string|null|bool|array)[] $notBlankValues */
192+
$notBlankValues = doFoo();
193+
Assert::thatAll($notBlankValues)->notBlank();
194+
\PHPStan\Testing\assertType('array<non-empty-array|non-empty-string|true>', $notBlankValues);
195+
186196
$assertThatFunction = \Assert\that($g);
187197
\PHPStan\Testing\assertType('Assert\AssertionChain<mixed>', $assertThatFunction);
188198

0 commit comments

Comments
 (0)