-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
added return type declaration sniff #1219
Conversation
T_WHITESPACE, | ||
); | ||
|
||
$a = $this->closingParenthesisColonSpacing; |
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.
Better not to use short variable names (less than 3 symbols) to ease code reading for others.
Also the a
doesn't really tell what's stored in it.
} else if ($tokens[$nextSeparator]['code'] === T_RETURN_TYPE) { | ||
$b = $acc; | ||
$acc = ''; | ||
if (ctype_lower($tokens[$nextSeparator]['content'] === false)) { |
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.
Will ctype_lower
function exist for every PHP version/build?
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.
Hey thanks for the reply.
ctype_lower is PHP 4 >= 4.0.4, PHP 5, PHP 7. Is that acceptable?
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.
What I mean if that function always present or in some PHP builds (e.g. on CentOS) it might not be available by default.
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.
its not available on FreeBSD, so I changed it to preg_match just in case
http://php.net/manual/en/ctype.installation.php
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.
Also in if (ctype_lower($tokens[$nextSeparator]['content'] === false)) {
line you seem to have misplaced the parenthesis and $tokens[$nextSeparator]['content'] === false
goes as argument to ctype_lower
.
What is the thing you're checking with it, that it's a lowercased text? I guess you can safely do strtolower($tokens[$nextSeparator]['content']) === $tokens[$nextSeparator]['content']
instead, because:
- it's faster then
preg_match
- we can safely use non-utf8 friendly function because we know all types use english letters only
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.
alright, changed it to strtolower
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.
Good. But if none of tests were failing, then probably that lowercasing code is never used.
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.
I already added tests for it when I changed it to preg_match
@@ -33,6 +33,7 @@ | |||
<rule ref="Generic.NamingConventions.ConstructorName"/> | |||
<rule ref="Generic.PHP.DeprecatedFunctions"/> | |||
<rule ref="Generic.PHP.LowerCaseKeyword"/> | |||
<rule ref="Generic.Functions.ReturnTypeDeclaration"/> |
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.
You shouldn't change any of existing rulesets.
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.
Alright. I guess thats also the reason the builds fail
Looking forward to this one getting released 👍 |
For inspiration and already to use: https://github.com/slevomat/coding-standard/blob/master/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSpacingSniff.php |
The implementation from the pull request is also ready for use at: https://github.com/hostnet/phpcs-tool/blob/master/src/Hostnet/Sniffs/Functions/ReturnTypeDeclarationSniff.php |
Return type declarations are optional, and have a specified amount of spaces between the function's closing parenthesis and colon, and a | ||
specified amount of spaces between the colon and the return type. | ||
The default is zero spaces between closing parenthesis and colon and one space between colon and return type. | ||
The return type should be lowercase. |
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.
You may want to update this line in the description to reflect that class names don't have to be lowercase.
* | ||
* @return int position within the token stack | ||
*/ | ||
private function _getCharachterAfterReturnTypeDeclaration(PHP_COdeSniffer_File $phpcsFile, array $tokens, $stackPtr) |
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.
Incorrect spelling of "Character" in method name, method usage and docs.
Are there plans to merge this? |
This one slipped past me after the latest changes. Thanks for bumping it again. I'll add it to the 3.3.0 roadmap. |
Before this sniff can be merged, it will need to be updated for the changes in PHPCS 3.x. The code style (short vs long arrays + array indentation) will also need to be fixed. Aside from that, I personally would like to see some unit tests with comments in the function declaration / trailing comments / PHPCS 3.2.x style annotations as those may well cause fixer conflicts if these are not being taken into account by the sniff.
These is a separate PR open which addresses this - #1685 - and takes mixed case object names into account. |
I've done a bit of a review of this sniff and I don't think it is ready to merge. Apart from @jrfnl 's comment about needing some more unit tests to check what happens with auto-fixing when comments and PHPCS annotations are thrown in, I also have some other comments:
I'm going to remove this from the 3.3.0 milestone as I'm not sure how long this will take to discuss and change. I'll re-add if everything gets sorted out before the 3.3.0 release. |
So Ive updated the commit. Updated some things that changed since Ive last committed, put all the tests in 1 file and did some rewriting. The configurable space properties are now ints, and added an IgnoreNewLines property.
Not sure what you mean. Can you give me examples? |
Sometimes the auto-fixers can get confused when you randomly put comments ( After fixing, it's important that comments are not accidentally removed, or inline comments moved and are now commenting out code, or a phpcs:ignore annotation moved to a different line where it now means something else. Those are the kind of unit test example @jrfnl was talking about. I haven't had a chance to try this sniff out yet and see if those test cases are working. |
Closed this PR because I completely forgot that this was still open, and Ive opened this PR 5 years ago (!). Its been a few years since Ive coded much in PHP, so Im not really familiar with what the best practices are now. If someone else wants to pick this up, feel free to do so. |
Created according to: https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md
For return type declarations, this sniff checks the spacing between the functions closing parenthesis, and the colon. It also checks the spacing between the colon and the return type. The spacings are still configurable
It does not check if the colon and return type are on the same line as the closing parenthesis. I can also add that if required.. but maybe that has to be placed in a separate sniff?
Edit: Removed requirement that return type declaration should be lowercase, because object names can also be in the return type declaration
Update: Added fixer for return type declaration sniff