Skip to content
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

Closed
wants to merge 1 commit into from
Closed

added return type declaration sniff #1219

wants to merge 1 commit into from

Conversation

akvankorlaar
Copy link

@akvankorlaar akvankorlaar commented Nov 23, 2016

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

T_WHITESPACE,
);

$a = $this->closingParenthesisColonSpacing;
Copy link
Contributor

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)) {
Copy link
Contributor

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?

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Author

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

Copy link
Contributor

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

Copy link
Author

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

Copy link
Contributor

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.

Copy link
Author

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"/>
Copy link
Contributor

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.

Copy link
Author

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

@Nicolai-
Copy link

Nicolai- commented Jan 5, 2017

Looking forward to this one getting released 👍

@TomasVotruba
Copy link
Contributor

@hboomsma
Copy link

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.
Copy link
Contributor

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)
Copy link
Contributor

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.

@josephzidell
Copy link
Contributor

Are there plans to merge this?

@gsherwood
Copy link
Member

This one slipped past me after the latest changes. Thanks for bumping it again. I'll add it to the 3.3.0 roadmap.

@gsherwood gsherwood added this to the 3.3.0 milestone Mar 12, 2018
@jrfnl
Copy link
Contributor

jrfnl commented Mar 13, 2018

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.

Edit: Removed requirement that return type declaration should be lowercase, because object names can also be in the return type declaration

These is a separate PR open which addresses this - #1685 - and takes mixed case object names into account.

@gsherwood
Copy link
Member

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:

  1. I don't know why the unit tests are across different files. I think they should be merged for easy of maintenance, unless I've missed something.
  2. The public properties that allow a ruleset to override the spacing should be ints and not strings. People are either going to want n number of spaces or a newline, but not a specific string.
  3. There should be an ignoreNewlines public property like some of the others sniffs have. This solves the problem where you weren't sure if you should report an error if they are not on the same line. A good example of how sniffs use this property, and generate error messages that include "n spaces or "newline" is: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php and is documented here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericwhitespacearbitraryparenthesesspacing
  4. Just some coding standard fixes needed as well as things have changed since this was first written.

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.

@akvankorlaar
Copy link
Author

akvankorlaar commented Mar 23, 2018

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.

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.

Not sure what you mean. Can you give me examples?

@gsherwood
Copy link
Member

Can you give me examples?

Sometimes the auto-fixers can get confused when you randomly put comments (//, /* */ and /** */ styles) throughout the function declaration. You can also place comments like // phpcs:ignore on lines to ignore all coding standards on that line.

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.

@akvankorlaar
Copy link
Author

akvankorlaar commented Nov 24, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants