Skip to content

Commit 841fbf3

Browse files
committed
Validators::everyIs: first argument has typehint iterable (BC break)
1 parent 0202bde commit 841fbf3

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/Utils/Validators.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function is($value, string $expected): bool
111111
{
112112
foreach (explode('|', $expected) as $item) {
113113
if (substr($item, -2) === '[]') {
114-
if (self::everyIs($value, substr($item, 0, -2))) {
114+
if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) {
115115
return true;
116116
}
117117
continue;
@@ -152,13 +152,9 @@ public static function is($value, string $expected): bool
152152

153153
/**
154154
* Finds whether all values are of expected type (separated by pipe).
155-
* @param iterable
156155
*/
157-
public static function everyIs($values, string $expected): bool
156+
public static function everyIs(iterable $values, string $expected): bool
158157
{
159-
if (!is_iterable($values)) {
160-
return false;
161-
}
162158
foreach ($values as $value) {
163159
if (!static::is($value, $expected)) {
164160
return false;

tests/Utils/Validators.everyIs().phpt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ test(function () {
2121
Assert::true(Validators::everyIs([], 'int'));
2222
Assert::true(Validators::everyIs(new ArrayIterator([]), 'int'));
2323

24-
Assert::false(Validators::everyIs(1, 'int'));
25-
Assert::false(Validators::everyIs(2.15, 'int'));
2624
Assert::true(Validators::everyIs([1, 2, 3], 'int'));
2725
Assert::false(Validators::everyIs([1, 2.15, 3], 'int'));
2826
Assert::true(Validators::everyIs([1, 2.15, 3], 'int|float'));
@@ -50,10 +48,11 @@ test(function () {
5048
yield 3;
5149
};
5250
Assert::true(Validators::everyIs($gen(), 'int'));
51+
});
52+
5353

54+
Assert::exception(function () {
5455
$var = new stdClass;
5556
$var->a = 1;
56-
$var->b = 2;
57-
$var->c = 3;
58-
Assert::false(Validators::everyIs($var, 'int'));
59-
});
57+
Validators::everyIs($var, 'int');
58+
}, TypeError::class);

0 commit comments

Comments
 (0)