Skip to content

Commit 9ea81c0

Browse files
[Validator] Add encoding test to validateClassName
1 parent d262c2c commit 9ea81c0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Validator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static function validateClassName(string $className, string $errorMessage
4747
];
4848

4949
foreach ($pieces as $piece) {
50+
if (!mb_check_encoding($piece, 'UTF-8')) {
51+
$errorMessage = $errorMessage ?: sprintf('"%s" is not a UTF-8-encoded string.', $piece);
52+
53+
throw new RuntimeCommandException($errorMessage);
54+
}
55+
5056
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) {
5157
$errorMessage = $errorMessage ?: sprintf('"%s" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)', $className);
5258

tests/ValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ public function testInvalidClassName()
6262
$this->expectExceptionMessage('"Class" is a reserved keyword and thus cannot be used as class name in PHP.');
6363
Validator::validateClassName('App\Entity\Class');
6464
}
65+
66+
public function testInvalidEncodingInClassName()
67+
{
68+
$this->expectException(RuntimeCommandException::class);
69+
$this->expectExceptionMessage('"�Controller" is not a UTF-8-encoded string.');
70+
Validator::validateClassName(mb_convert_encoding('Ś', 'ISO-8859-2', 'UTF-8'));
71+
}
6572
}

0 commit comments

Comments
 (0)