Skip to content
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

Closed
u01jmg3 opened this issue Mar 9, 2015 · 16 comments

Comments

@u01jmg3
Copy link

u01jmg3 commented Mar 9, 2015

@aik099
Copy link
Contributor

aik099 commented Mar 9, 2015

Can you please write example of that PHP code. Examples on provided link weren't clear.

@u01jmg3
Copy link
Author

u01jmg3 commented Mar 9, 2015

$x = "these are " . $foo;   //Warning

$y = "these are $foo";      //No warning
$z = 'these are ' . $foo;   //No warning

@aik099
Copy link
Contributor

aik099 commented Mar 9, 2015

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.

@u01jmg3
Copy link
Author

u01jmg3 commented Mar 9, 2015

Yep, indeed why not just singles quotes and yes it would be faster - anyway PHPCS could be used to enforce this?

@aik099
Copy link
Contributor

aik099 commented Mar 9, 2015

Then back to original question in issue description: what is string interpolation?

I think sniff properly detects double quote usage, even in your example.

@u01jmg3
Copy link
Author

u01jmg3 commented Mar 9, 2015

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?

@aik099
Copy link
Contributor

aik099 commented Mar 9, 2015

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.

@u01jmg3
Copy link
Author

u01jmg3 commented Mar 9, 2015

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

@gsherwood
Copy link
Member

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?

@u01jmg3
Copy link
Author

u01jmg3 commented Mar 10, 2015

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.

@u01jmg3 u01jmg3 closed this as completed Mar 10, 2015
@dbaltas
Copy link

dbaltas commented Jun 23, 2017

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.

@josephzidell
Copy link
Contributor

In php 7, interpolation is actually faster than concatenation. Is there a reverse sniff that finds usages of concats-that-could-be-interpolations?

@gsherwood
Copy link
Member

Maybe you want to comment on #2316

@josephzidell
Copy link
Contributor

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).

@gsherwood
Copy link
Member

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.

@josephzidell
Copy link
Contributor

Will do. Thanks

jrfnl added a commit to jrfnl/PHP_CodeSniffer that referenced this issue Jul 29, 2024
…dstartendofstatement-add-extra-qa-test

File::find[Start|End]OfStatement(): add QA tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants