-
-
Notifications
You must be signed in to change notification settings - Fork 431
Checking on reserved keywords for class name validity. #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checking on reserved keywords for class name validity. #306
Conversation
ca02938
to
d712b11
Compare
src/Str.php
Outdated
'use', 'var', 'while', 'xor', '__CLASS__', '__DIR__', '__FILE__', | ||
'__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__', | ||
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void', | ||
'iterable', 'object', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void',
'iterable', 'object',
This words are allowed to use in Namespace, anyway it will be very strange to use it :-)
src/Validator.php
Outdated
@@ -29,7 +29,7 @@ public static function validateClassName(string $className, string $errorMessage | |||
$pieces = explode('\\', ltrim($className, '\\')); | |||
|
|||
foreach ($pieces as $piece) { | |||
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) { | |||
if (!Str::isValidPhpClassName($piece)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe will be better not to change this check, but add a new check with new error message, to show the user what is wrong with classname?
40aa3ca
to
00cf423
Compare
@sadikoff Thanks for your review! Wdyt about my current fix? |
@SerkanYildiz It's great that you have done it so fast, but I was talking about something else. I suggested not to split namespace and class validator, but leave old validation in place and add new one which will validate reserved words foreach ($pieces as $piece) {
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);
throw new RuntimeCommandException($errorMessage);
}
if (Str::isPhpReservedWord($piece)) {
throw new RuntimeCommandException(sprintf('"%s" is a reserved keyword and thus cannot be used as class name in PHP.', $className));
}
} |
Yea... I think I agree. So remove But otherwise, this is awesome! I like tightening things up :) |
Ping @SerkanYildiz! I think we need just one more small change. Does my description above make sense? Cheers! |
Pong @weaverryan! Will fix this today, thanks for helping! |
00cf423
to
6cebac0
Compare
@weaverryan Pushed the requested changes. Does it look good now? |
3d40de6
to
535d287
Compare
@weaverryan Tests are failing due another error. Should I do something? |
535d287
to
0b90e3c
Compare
0b90e3c
to
f308470
Compare
Thank you @SerkanYildiz! |
…SerkanYildiz) This PR was squashed before being merged into the 1.0-dev branch (closes #306). Discussion ---------- Checking on reserved keywords for class name validity. Validator should also check if the given class name is a reserved keyword or not. However we have to come up with a better exception message, since this one does not reflect the error. Any suggestion? Thanks for your help @weaverryan during #SymfonyConHackDay2018 Fixes #305 Commits ------- f308470 Move validation logic into Validator. a419b8f Checking on reserved keywords for class name validity.
Validator should also check if the given class name is a reserved keyword or not.
However we have to come up with a better exception message, since this one does not reflect the error. Any suggestion?
Thanks for your help @weaverryan during #SymfonyConHackDay2018
Fixes #305