Skip to content

Commit

Permalink
Support allNullOr*
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jun 6, 2022
1 parent 08d2db4 commit a7f1295
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ This extension specifies types of values passed to:
* `Assert::minLength`
* `Assert::maxLength`
* `Assert::lengthBetween`
* `nullOr*` and `all*` variants of the above methods
* `nullOr*`, `all*` and `allNullOr*` variants of the above methods


## Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^9.5",
"webmozart/assert": "^1.10.0"
"webmozart/assert": "^1.11.0"
},
"config": {
"platform": {
Expand Down
27 changes: 23 additions & 4 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ public function isStaticMethodSupported(

private static function trimName(string $name): string
{
if (substr($name, 0, 6) === 'nullOr') {
if (substr($name, 0, 9) === 'allNullOr') {
$name = substr($name, 9);
} elseif (substr($name, 0, 6) === 'nullOr') {
$name = substr($name, 6);
}
if (substr($name, 0, 3) === 'all') {
} elseif (substr($name, 0, 3) === 'all') {
$name = substr($name, 3);
}

Expand All @@ -128,6 +129,17 @@ public function specifyTypes(
TypeSpecifierContext $context
): SpecifiedTypes
{
if (substr($staticMethodReflection->getName(), 0, 9) === 'allNullOr') {
return $this->handleAll(
$staticMethodReflection->getName(),
$node,
$scope,
static function (Type $type) {
return TypeCombinator::addNull($type);
}
);
}

if (substr($staticMethodReflection->getName(), 0, 6) === 'allNot') {
return $this->handleAllNot(
$staticMethodReflection->getName(),
Expand Down Expand Up @@ -766,10 +778,14 @@ static function (Type $type) use ($valueType): Type {
throw new ShouldNotHappenException();
}

/**
* @param callable(Type): Type|null $typeModifier
*/
private function handleAll(
string $methodName,
StaticCall $node,
Scope $scope
Scope $scope,
?callable $typeModifier = null
): SpecifiedTypes
{
$args = $node->getArgs();
Expand All @@ -792,6 +808,9 @@ private function handleAll(
}

$type = TypeCombinator::remove($type, $sureNotTypes[$exprStr][1] ?? new NeverType());
if ($typeModifier !== null) {
$type = $typeModifier($type);
}

return $this->arrayOrIterable(
$scope,
Expand Down
12 changes: 12 additions & 0 deletions tests/Type/WebMozartAssert/data/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public function allCount(array $a): void
assertType('array<non-empty-array>', $a);
}

public function allNullOr(array $a, iterable $b, $c): void
{
Assert::allNullOrStringNotEmpty($a);
assertType('array<non-empty-string|null>', $a);

Assert::allNullOrIsInstanceOf($b, stdClass::class);
assertType('iterable<stdClass|null>', $b);

Assert::allNullOrScalar($c);
assertType('iterable<bool|float|int|string|null>', $c);
}

}

class CollectionFoo
Expand Down

0 comments on commit a7f1295

Please sign in to comment.