Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Double quote usage #18

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/book/v2/ruleset.md
Original file line number Diff line number Diff line change
Expand Up @@ -953,25 +953,25 @@ Force whitespace before and after concatenation

### Squiz.Strings.DoubleQuoteUsage
#### Squiz.Strings.DoubleQuoteUsage.ContainsVar
Double quote strings may only be used if they contain variables. SQL queries containing single quotes are an exception
Double quote strings may only be used if they contain variables, control characters or if using single quotes would
result in more escaped characters than necessary. For instance, SQL queries containing single quotes are an exception
to the rule.

*Valid: Double quote strings are only used when it contains a variable.*
*Valid: Double quote strings are only used when it contains a variable, control characters or single quotes.*
```php
<?php
$string = "Hello There\r\n";
$string = "Hello $there";
$string = 'Hello There';
$string = 'Hello'.' There'."\n";
$string = '\$var';
$query = "SELECT * FROM table WHERE name =''";
```

*Invalid: There are no variables inside double quote strings.*
*Invalid: There are no variables or control characters inside double quote strings.*
```php
<?php
$string = "Hello There";
$string = "Hello"." There"."\n";
$string = "Hello" . " There" . "\n";
$string = "\$var";
```

Expand Down
5 changes: 2 additions & 3 deletions src/ZendCodingStandard/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@
</properties>
</rule>
<!-- Forbid strings in `"` unless necessary -->
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
<message>Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead</message>
<rule ref="Squiz.Strings.DoubleQuoteUsage">
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"></exclude>
</rule>
<!-- Forbid braces around string in `echo` -->
<rule ref="Squiz.Strings.EchoedStrings"/>
Expand Down
7 changes: 4 additions & 3 deletions test/expected-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test/fixable/class-properties.php 6 0
test/fixable/classes-traits-interfaces.php 17 1
test/fixable/closures.php 19 0
test/fixable/commenting.php 26 0
test/fixable/concatenation-spacing.php 21 0
test/fixable/concatenation-spacing.php 19 0
test/fixable/control-structures.php 6 0
test/fixable/example-class.php 29 0
test/fixable/extends-and-implements-multiline.php 13 0
Expand All @@ -30,16 +30,17 @@ test/fixable/return-type-on-methods.php 17 0
test/fixable/semicolon-spacing.php 5 0
test/fixable/spacing.php 18 1
test/fixable/statement-alignment.php 19 0
test/fixable/strings-double-quote-usage.php 6 0
test/fixable/test-case.php 4 0
test/fixable/traits-uses.php 9 0
test/fixable/UnusedVariables.php 1 0
test/fixable/useless-semicolon.php 8 0
test/fixable/variable-names.php 5 2
test/fixable/visibility-declaration.php 1 0
----------------------------------------------------------------------
A TOTAL OF 441 ERRORS AND 6 WARNINGS WERE FOUND IN 33 FILES
A TOTAL OF 445 ERRORS AND 6 WARNINGS WERE FOUND IN 34 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 377 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 381 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


16 changes: 16 additions & 0 deletions test/fixable/strings-double-quote-usage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

// valid
$there = 'There';
$string = "Hello There\r\n";
$string = "Hello $there";
$string = 'Hello There';
$string = '\$var';
$query = "SELECT * FROM table WHERE name =''";

// invalid
$string = "Hello There";
$string = "Hello" . " There" . "\n";
$string = "\$var";
16 changes: 16 additions & 0 deletions test/fixed/strings-double-quote-usage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

// valid
$there = 'There';
$string = "Hello There\r\n";
$string = "Hello $there";
$string = 'Hello There';
$string = '\$var';
$query = "SELECT * FROM table WHERE name =''";

// invalid
$string = 'Hello There';
$string = 'Hello' . ' There' . "\n";
$string = '$var';