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 $x and $z are equivalent, then why not just use single quotes. Also theoretically such string should work faster.
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 $y whereby double quotes would need to be used and PHPCS should give no warning.This is as well as $z giving no warning.
Which available sniff allows me to detect double quote usage as per $x?
I see, then Squiz.Strings.DoubleQuoteUsage is the one you need.
$x = "these are " . $foo; //warning
What warning message do you see? If you run phpcs with -s option, then also sniff code will be displayed.
This is just what I'm looking for but it's also disallowing example $y. Anyway to tone down how strict PHPCS is and allow $y but prevent $x?
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
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired" />
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
Most helpful comment
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.