-
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
Question: Is there a sniff to detect the use of double quotes over single quotes? #509
Comments
Can you please write example of that PHP code. Examples on provided link weren't clear. |
$x = "these are " . $foo; //Warning
$y = "these are $foo"; //No warning
$z = 'these are ' . $foo; //No warning |
I saw that example, but since what's in |
Yep, indeed why not just singles quotes and yes it would be faster - anyway PHPCS could be used to enforce this? |
Then back to original question in issue description: what is string interpolation? I think sniff properly detects double quote usage, even in your example. |
Definition: In computer programming, string interpolation or variable substitution is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. String interpolation would be the example Which available sniff allows me to detect double quote usage as per |
I see, then
What warning message do you see? If you run |
This is just what I'm looking for but it's also disallowing example PHPCS warnings given in comments $x = "these are " . $foo; //String "these are " does not require double quotes; use single quotes instead
$y = "these are $foo"; //Variable "$foo" not allowed in double quote string; use concatenation instead
$z = 'these are ' . $foo; //No warning |
You can mute that specific error in your ruleset.xml file like this: <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
<severity>0</severity>
</rule> Just by coincidence, this happens to be the exact example I picked for muting an error message on the annotated ruleset wiki page: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml :) Does that do what you are after? |
Thank you both @gsherwood: thanks for answering my final question. I thought I was familiar with PHPCS but it seems I need to do some more reading! The snippet you've provided does exactly what I need it to. |
This can be achieved with the following rule
Which does not allow double quotes for plain strings but does allow them if they contain a variable. |
In php 7, interpolation is actually faster than concatenation. Is there a reverse sniff that finds usages of concats-that-could-be-interpolations? |
Maybe you want to comment on #2316 |
That one is about the quotes on an existing string. My use-case would be to detect single- or double-quoted strings that are currently concatenated and could use interpolation, which is basically a variable or class property (excludes constants, method calls). |
Yes, but it may be more useful if a sniff that checks for single quote usage instead checks for concatenation. So if a sniff is to be written, I think it's worth putting your comments there for consideration rather than on this closed issue. Issue #2259 is probably also worth watching. But I didn't answer your original question, which is no, there is no sniff to enforce the reverse that I know of. |
Will do. Thanks |
…dstartendofstatement-add-extra-qa-test File::find[Start|End]OfStatement(): add QA tests
The text was updated successfully, but these errors were encountered: