Skip to content

Commit cf0052b

Browse files
committed
Fix some errors detected by PHPStan
This also made me realise that we had a bug in that rule.
1 parent 0e8ac18 commit cf0052b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

library/Rules/Cnh.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use function is_scalar;
1717
use function mb_strlen;
18+
use function preg_match;
1819
use function preg_replace;
1920

2021
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
@@ -34,7 +35,11 @@ public function isValid(mixed $input): bool
3435
$input = (string) preg_replace('{\D}', '', (string) $input);
3536

3637
// Validate length and invalid numbers
37-
if (mb_strlen($input) != 11 || ((int) $input === 0)) {
38+
if (mb_strlen($input) != 11) {
39+
return false;
40+
}
41+
42+
if (preg_match('/^(\d)\1{10}/', $input) > 0) {
3843
return false;
3944
}
4045

tests/unit/Rules/CnhTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Respect\Validation\Test\RuleTestCase;
1515
use stdClass;
1616

17+
use function str_repeat;
18+
1719
#[Group('rule')]
1820
#[CoversClass(Cnh::class)]
1921
final class CnhTest extends RuleTestCase
@@ -132,6 +134,16 @@ public static function providerForInvalidInput(): iterable
132134
[$rule, '0034172483001'],
133135
[$rule, '00006700431345'],
134136
[$rule, '0032926944005'],
137+
[$rule, str_repeat('0', 11)],
138+
[$rule, str_repeat('1', 11)],
139+
[$rule, str_repeat('2', 11)],
140+
[$rule, str_repeat('3', 11)],
141+
[$rule, str_repeat('4', 11)],
142+
[$rule, str_repeat('5', 11)],
143+
[$rule, str_repeat('6', 11)],
144+
[$rule, str_repeat('7', 11)],
145+
[$rule, str_repeat('8', 11)],
146+
[$rule, str_repeat('9', 11)],
135147
];
136148
}
137149
}

0 commit comments

Comments
 (0)