Comments (10)
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
loops:
foreach ($list as $key => $_value) {
// Use $key, ignore $_value
}
from phpcs-variable-analysis.
Obviously, using an argument that's been marked as unused, should raise a warning.
from phpcs-variable-analysis.
🤔 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
// ...
}
from phpcs-variable-analysis.
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.
after-used - only the last argument must be used. This allows you, for instance, to have two named parameters to a function and as long as you use the second argument, ESLint will not warn you about the first. This is the default setting.
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.
from phpcs-variable-analysis.
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 $ignored
):
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
<properties>
<!--
Include the following property if you want to have a whitelist of variable names
that are commonly used for placeholder "junk" values that ignored and that you
don't want to provoke an unused variable warning for.
Format is "variableName" without a leading $ with whitespace delimiting names.
-->
<property name="validUnusedVariableNames" value="ignored"/>
</properties>
</rule>
from phpcs-variable-analysis.
Will this work with multiple ignored arguments?
What about the following case?
function foobar($ignored, $ignored, $usedArg) {
}
from phpcs-variable-analysis.
When I do this I get a fatal because I'm redefining an argument.
PHP Fatal error: Redefinition of parameter $ignored in /tmp/test.php on line 3
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 validUnusedVariableNames
seems to be awkward in those cases? I don't want to maintain a list of ignored variables.
I guess that I could call the arguments ignored1
, ignored2
and so on, but I'd have to keep adding them to option as I start hitting cases where I don't care about 3, 4, 5 arguments and so on.
from phpcs-variable-analysis.
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 validUnusedVariableNames
to be a regexp instead (it'd be pretty easy) but that would be a breaking change. But I certainly could add a new parameter for a regexp, like validUnusedVariablePattern
.
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);
from phpcs-variable-analysis.
@sirbrillig I think that having a validUnusedVariablePattern
would be the best solution.
Related question: Does the linter complain if you use a variable that's configured as unused? (through validUnusedVariableNames
or validUnusedVariablePattern
)
from phpcs-variable-analysis.
@loilo thanks for the reminder about this. #22 should take care of this.
from phpcs-variable-analysis.
Related Issues (20)
- using extract on trusted data will show undefined HOT 2
- Add validUndefinedVariableNamesInFileScope HOT 1
- Improve handling of initial values in initializers HOT 1
- UndefinedVariable first assigned in for loop, used in loop expression HOT 1
- Inline if-clause with short-list false positive HOT 5
- Support of array desctructing HOT 2
- Incorrect warning for arguments to static methods that return "new static()" HOT 3
- Incorrect warning for arguments are thrown on static arrow functions HOT 1
- False-positive `UndefinedVariable` for static variable inside an anonymous function HOT 4
- Incorrect warning about redeclaration of function parameter as static variable HOT 2
- Variable $this is undefined in enum HOT 1
- Support constructor promotion with readonly properties (UnusedVariable) HOT 5
- method call called 'enum' causes an "Cannot find enum start at position" error HOT 2
- Unused function parameter in lambda function HOT 4
- Ternary operator inside arrow function HOT 3
- Incorrect warning Variable $var is undefined. HOT 6
- Undefined variable error introduced in v2.11.15 HOT 2
- Feature Request: don't report on unused parameters for deprecated functions
- Variable reported as unused on an assignment by reference HOT 2
- Assignment of an array element counts as a read
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpcs-variable-analysis.