-
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
Generic/FunctionCallArgumentSpacing: minor tweaks + bug fix for anonymous classes #2387
Generic/FunctionCallArgumentSpacing: minor tweaks + bug fix for anonymous classes #2387
Conversation
#### Make the sniff more efficient by sniffing for fewer tokens The `Tokens::$functionNameTokens` array includes a number of tokens which will never trigger errors from this sniff as they only expect one parameter. Think: `empty()`, `exit()`. However, as the tokens are registered for the sniff, the sniff will be triggered each time those tokens are encountered. I have changed the `register()` method to only register the tokens which will/could actually trigger errors, making the sniff more efficient. #### Confirm correct behaviour with PHP 7.3 trailing commas in function calls Added an extra unit test to confirm that the sniff handles PHP 7.3 trailing commas in function calls correctly.
Spacing around equal operators within anonymous classes should be ignored by this sniff, but wasn't. Includes unit test.
I've added a second commit to this PR containing a bug fix and have updated the PR description with info on this commit. |
That's a really good point :) I'm not sure why that code is in this sniff - maybe I did a copy/paste from one of the function declaration sniffs back in the day. |
@gsherwood I'd be happy to send in a PR to remove the "equal sign spacing" check from the sniff, especially as it probably also causes fixer conflicts with sniffs which just and only check spacing around assignment operators and where the default spacing is not The reason for suggesting a separate PR is that this PR is marked for the |
If the errors can never be generated because they are checking for code that can't exist, I'm happy to include those removals in a bug fix release. |
Thanks for this change. |
@gsherwood The errors can exist, code like The code in effect is the same as the below: $a = 100;
function_call( $a ); I.e. it sets the variable within the scope and that variable will be available after the function call as well, and then it passes the variable to the function call. |
@gsherwood I've done a little digging and with the existing standards, these codes at least don't cause any fixer conflicts, though depending on the standard, they do cause duplicate notices.
All in all, I think removing the errorcodes is appropriate as either the As neither of these sniffs are included in the |
PR #2399 takes care of removing the assignment operator checks. |
Make the sniff more efficient by sniffing for fewer tokens
The
Tokens::$functionNameTokens
array includes a number of tokens which will never trigger errors from this sniff as they only expect one parameter. Think:empty()
,exit()
.However, as the tokens are registered for the sniff, the sniff will be triggered each time those tokens are encountered.
I have changed the
register()
method to only register the tokens which will/could actually trigger errors, making the sniff more efficient.Confirm correct behaviour with PHP 7.3 trailing commas in function calls
Added an extra unit test to confirm that the sniff handles PHP 7.3 trailing commas in function calls correctly.
Improve handling of anonymous classes
Spacing around equal operators within anonymous classes should be ignored by this sniff, but wasn't.
This fixes false positives being thrown by this sniff and most likely also fixes a fixer conflict, though I haven't gone looking for specifics, but I imagine that the false positives caused a fixer conflict with a function declaration sniff which demands no spaces around the equal sign for parameter default values. (as is used in the PHPCS native standard)
Includes unit test.
As a sidenote, I wonder whether the check for spacing around the equal operator should even be included in this sniff as "default values" in function calls is not a thing.