Skip to content

Commit 9009135

Browse files
herndlmondrejmirtes
authored andcommitted
Support string assertions resulting in non-empty-string
1 parent 11ecf41 commit 9009135

File tree

3 files changed

+126
-12
lines changed

3 files changed

+126
-12
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,22 @@ This extension specifies types of values passed to:
6969
* `Assert::methodExists`
7070
* `Assert::propertyExists`
7171
* `Assert::isArrayAccessible`
72+
* `Assert::unicodeLetters`
73+
* `Assert::alpha`
74+
* `Assert::digits`
75+
* `Assert::alnum`
76+
* `Assert::lower`
77+
* `Assert::upper`
7278
* `Assert::length`
7379
* `Assert::minLength`
7480
* `Assert::maxLength`
7581
* `Assert::lengthBetween`
82+
* `Assert::uuid`
83+
* `Assert::ip`
84+
* `Assert::ipv4`
85+
* `Assert::ipv6`
86+
* `Assert::email`
87+
* `Assert::notWhitespaceOnly`
7688
* `nullOr*` and `all*` variants of the above methods
7789

7890

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+42-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
class AssertTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
2929
{
3030

31+
private const ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING = [
32+
'stringNotEmpty',
33+
'unicodeLetters',
34+
'alpha',
35+
'digits',
36+
'alnum',
37+
'lower',
38+
'upper',
39+
'uuid',
40+
'ip',
41+
'ipv4',
42+
'ipv6',
43+
'email',
44+
'notWhitespaceOnly',
45+
];
46+
3147
/** @var \Closure[] */
3248
private static $resolvers;
3349

@@ -50,6 +66,10 @@ public function isStaticMethodSupported(
5066
TypeSpecifierContext $context
5167
): bool
5268
{
69+
if (in_array($staticMethodReflection->getName(), self::ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING, true)) {
70+
return true;
71+
}
72+
5373
if (substr($staticMethodReflection->getName(), 0, 6) === 'allNot') {
5474
$methods = [
5575
'allNotInstanceOf' => 2,
@@ -144,6 +164,11 @@ private static function createExpression(
144164
): ?\PhpParser\Node\Expr
145165
{
146166
$trimmedName = self::trimName($name);
167+
168+
if (in_array($trimmedName, self::ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING, true)) {
169+
return self::createIsNonEmptyStringExpression($args);
170+
}
171+
147172
$resolvers = self::getExpressionResolvers();
148173
$resolver = $resolvers[$trimmedName];
149174
$expression = $resolver($scope, ...$args);
@@ -195,18 +220,6 @@ private static function getExpressionResolvers(): array
195220
[$value]
196221
);
197222
},
198-
'stringNotEmpty' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
199-
return new BooleanAnd(
200-
new \PhpParser\Node\Expr\FuncCall(
201-
new \PhpParser\Node\Name('is_string'),
202-
[$value]
203-
),
204-
new NotIdentical(
205-
$value->value,
206-
new String_('')
207-
)
208-
);
209-
},
210223
'float' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
211224
return new \PhpParser\Node\Expr\FuncCall(
212225
new \PhpParser\Node\Name('is_float'),
@@ -674,4 +687,21 @@ private function arrayOrIterable(
674687
);
675688
}
676689

690+
/**
691+
* @param \PhpParser\Node\Arg[] $args
692+
*/
693+
private static function createIsNonEmptyStringExpression(array $args): \PhpParser\Node\Expr
694+
{
695+
return new BooleanAnd(
696+
new \PhpParser\Node\Expr\FuncCall(
697+
new \PhpParser\Node\Name('is_string'),
698+
[$args[0]]
699+
),
700+
new NotIdentical(
701+
$args[0]->value,
702+
new String_('')
703+
)
704+
);
705+
}
706+
677707
}

tests/Type/WebMozartAssert/data/string.php

+72
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,76 @@ public function lengthBetween(string $a, string $b, string $c, string $d): void
4949
\PHPStan\Testing\assertType('non-empty-string', $d);
5050
}
5151

52+
public function unicodeLetters($a): void
53+
{
54+
Assert::unicodeLetters($a);
55+
\PHPStan\Testing\assertType('non-empty-string', $a);
56+
}
57+
58+
public function alpha($a): void
59+
{
60+
Assert::alpha($a);
61+
\PHPStan\Testing\assertType('non-empty-string', $a);
62+
}
63+
64+
public function digits(string $a): void
65+
{
66+
Assert::digits($a);
67+
\PHPStan\Testing\assertType('non-empty-string', $a);
68+
}
69+
70+
public function alnum(string $a): void
71+
{
72+
Assert::alnum($a);
73+
\PHPStan\Testing\assertType('non-empty-string', $a);
74+
}
75+
76+
public function lower(string $a): void
77+
{
78+
Assert::lower($a);
79+
\PHPStan\Testing\assertType('non-empty-string', $a);
80+
}
81+
82+
public function upper(string $a): void
83+
{
84+
Assert::upper($a);
85+
\PHPStan\Testing\assertType('non-empty-string', $a);
86+
}
87+
88+
public function uuid(string $a): void
89+
{
90+
Assert::uuid($a);
91+
\PHPStan\Testing\assertType('non-empty-string', $a);
92+
}
93+
94+
public function ip($a): void
95+
{
96+
Assert::ip($a);
97+
\PHPStan\Testing\assertType('non-empty-string', $a);
98+
}
99+
100+
public function ipv4($a): void
101+
{
102+
Assert::ipv4($a);
103+
\PHPStan\Testing\assertType('non-empty-string', $a);
104+
}
105+
106+
public function ipv6($a): void
107+
{
108+
Assert::ipv6($a);
109+
\PHPStan\Testing\assertType('non-empty-string', $a);
110+
}
111+
112+
public function email($a): void
113+
{
114+
Assert::email($a);
115+
\PHPStan\Testing\assertType('non-empty-string', $a);
116+
}
117+
118+
public function notWhitespaceOnly(string $a): void
119+
{
120+
Assert::notWhitespaceOnly($a);
121+
\PHPStan\Testing\assertType('non-empty-string', $a);
122+
}
123+
52124
}

0 commit comments

Comments
 (0)