Skip to content

Commit f308470

Browse files
committed
Move validation logic into Validator.
1 parent a419b8f commit f308470

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

src/Str.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,29 +197,6 @@ public static function isValidPhpVariableName($name)
197197
return (bool) preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name, $matches);
198198
}
199199

200-
public static function isValidPhpClassName(string $className): bool
201-
{
202-
$reservedKeywords = ['__halt_compiler', 'abstract', 'and', 'array',
203-
'as', 'break', 'callable', 'case', 'catch', 'class',
204-
'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
205-
'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
206-
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval',
207-
'exit', 'extends', 'final', 'for', 'foreach', 'function',
208-
'global', 'goto', 'if', 'implements', 'include',
209-
'include_once', 'instanceof', 'insteadof', 'interface', 'isset',
210-
'list', 'namespace', 'new', 'or', 'print', 'private',
211-
'protected', 'public', 'require', 'require_once', 'return',
212-
'static', 'switch', 'throw', 'trait', 'try', 'unset',
213-
'use', 'var', 'while', 'xor', '__CLASS__', '__DIR__', '__FILE__',
214-
'__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__',
215-
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void',
216-
'iterable', 'object',
217-
];
218-
219-
return !\in_array(strtolower($className), $reservedKeywords, true) &&
220-
preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $className);
221-
}
222-
223200
public static function areClassesAlphabetical(string $class1, string $class2)
224201
{
225202
$arr1 = [$class1, $class2];

src/Validator.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,35 @@ public static function validateClassName(string $className, string $errorMessage
2727
{
2828
// remove potential opening slash so we don't match on it
2929
$pieces = explode('\\', ltrim($className, '\\'));
30+
$shortClassName = Str::getShortClassName($className);
31+
32+
$reservedKeywords = ['__halt_compiler', 'abstract', 'and', 'array',
33+
'as', 'break', 'callable', 'case', 'catch', 'class',
34+
'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
35+
'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
36+
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval',
37+
'exit', 'extends', 'final', 'for', 'foreach', 'function',
38+
'global', 'goto', 'if', 'implements', 'include',
39+
'include_once', 'instanceof', 'insteadof', 'interface', 'isset',
40+
'list', 'namespace', 'new', 'or', 'print', 'private',
41+
'protected', 'public', 'require', 'require_once', 'return',
42+
'static', 'switch', 'throw', 'trait', 'try', 'unset',
43+
'use', 'var', 'while', 'xor',
44+
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void',
45+
'iterable', 'object', '__FILE__', '__LINE__', '__DIR__', '__FUNCTION__', '__CLASS__',
46+
'__METHOD__', '__NAMESPACE__', '__TRAIT__',
47+
];
3048

3149
foreach ($pieces as $piece) {
32-
if (!Str::isValidPhpClassName($piece)) {
50+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) {
3351
$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);
3452

3553
throw new RuntimeCommandException($errorMessage);
3654
}
55+
56+
if (\in_array(strtolower($shortClassName), $reservedKeywords, true)) {
57+
throw new RuntimeCommandException(sprintf('"%s" is a reserved keyword and thus cannot be used as class name in PHP.', $shortClassName));
58+
}
3759
}
3860

3961
// return original class name

tests/ValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testValidateClassName()
5959
public function testInvalidClassName()
6060
{
6161
$this->expectException(RuntimeCommandException::class);
62-
$this->expectExceptionMessage('"class" 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)');
63-
Validator::validateClassName('class');
62+
$this->expectExceptionMessage('"Class" is a reserved keyword and thus cannot be used as class name in PHP.');
63+
Validator::validateClassName('App\Entity\Class');
6464
}
65-
}
65+
}

0 commit comments

Comments
 (0)