Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static function validateClassName(string $className, string $errorMessage
];

foreach ($pieces as $piece) {
if (!mb_check_encoding($piece, 'UTF-8')) {
$errorMessage = $errorMessage ?: sprintf('"%s" is not a UTF-8-encoded string.', $piece);

throw new RuntimeCommandException($errorMessage);
}

if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) {
$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);

Expand Down
7 changes: 7 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ public function testInvalidClassName()
$this->expectExceptionMessage('"Class" is a reserved keyword and thus cannot be used as class name in PHP.');
Validator::validateClassName('App\Entity\Class');
}

public function testInvalidEncodingInClassName()
{
$this->expectException(RuntimeCommandException::class);
$this->expectExceptionMessage('"�Controller" is not a UTF-8-encoded string.');
Validator::validateClassName(mb_convert_encoding('Ś', 'ISO-8859-2', 'UTF-8'));
}
}