-
Notifications
You must be signed in to change notification settings - Fork 15
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
Support marking some function arguments as unused #21
Comments
Obviously, using an argument that's been marked as unused, should raise a warning. |
🤔 this is a really interesting idea. I'm going to look around for prior art in other linters (thanks for the rubocop link) to see what patterns exist. In the mean time, you could just disable the rule for that line (although I realize that also disables it for the other arguments): public function render($request, \Exception $e) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
// ...
} |
This appears to be handled in eslint by an option on the rule. Alternatively, there's an option which makes sure at least the last param is used.
I like both these options better than prefixing the variable with a static string, since that requires altering the code for the sake of the linter, rather than the other way around. |
Hey! It turns out this option already exists! I'm going to improve the documentation for it and add tests in #22, but you should be able to do something like the following in your config file (the example below will ignore variables named
|
Will this work with multiple ignored arguments? What about the following case? function foobar($ignored, $ignored, $usedArg) {
} |
When I do this I get a fatal because I'm redefining an argument.
In my code I have a bunch of cases, where I'm implementing some contract and that I don't care about 2 or 3 arguments. Using the I guess that I could call the arguments |
That's a good point. As you said you could do something like <property name="validUnusedVariableNames" value="ignoredFirst ignoredSecond ignoredThird"/> and then do function foobar($ignoredFirst, $ignoredSecond, $ignoredThird, $usedArg); But that's pretty cumbersome. It'd be nicer if the parameter was a regexp which would match parts of variable names. I considered changing Then you could do this: <property name="validUnusedVariablePatern" value="ignored"/> function foobar($ignoredFirst, $ignoredSecond, $ignoredThird, $usedArg); Or, if you prefer the underscores (note the caret which means "start of string"): <property name="validUnusedVariablePatern" value="^_"/> function foobar($_something, $_another, $_yetAnother, $usedArg); |
@sirbrillig I think that having a Related question: Does the linter complain if you use a variable that's configured as unused? (through |
For the record: Having this option would also be great for other uses cases where variables need to be defined but not necessarily used: Exceptions: try {
// Some code that only throws a single type of exception
} catch (Exception $_e) {
// I know what's happening, I really don't need to use $_e here
}
foreach ($list as $key => $_value) {
// Use $key, ignore $_value
} |
Take the following code as an example:
The
render
function takes 2 arguments. A request and an exception. We have to accept these two arguments because we're overwriting a method and we want to match it's signature.In our implementation, we don't actually care about the
$request
param. PHPCS gives me a warning because$request
is unused.I want to be able to tell PHPCS that this argument is unused and that this is OK.
With Rubocop (a ruby linter), I can prefix an argument with
_
to mark it as unused and to let the linter know that this is OK. Here's the relevant doc: https://rubocop.readthedocs.io/en/latest/cops_lint/#lintunusedmethodargumentWe could have something like this:
This would not generate any warnings since we've explicitly said that the argument should be unused.
The text was updated successfully, but these errors were encountered: