Skip to content

Commit d7725d4

Browse files
committed
bug #387 [Validator] Add encoding test to validateClassName (przemyslaw-bogusz)
This PR was merged into the 1.0-dev branch. Discussion ---------- [Validator] Add encoding test to validateClassName | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Fixed tickets | #249 | Deprecations? | no | License | MIT This checks if the input string is a UTF-8-encoded, and throws an exception if not. Commits ------- 9ea81c0 [Validator] Add encoding test to validateClassName
2 parents 6732a15 + 9ea81c0 commit d7725d4

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)