Skip to content

Commit 76853c5

Browse files
committed
Fixed bug #529 : Squiz.Strings.EchoedStrings gives false positive when echo'ing using an inline condition
1 parent 30c618e commit 76853c5

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

CodeSniffer/Standards/Squiz/Sniffs/Strings/EchoedStringsSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5757
{
5858
$tokens = $phpcsFile->getTokens();
5959

60-
$firstContent = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
60+
$firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
6161
// If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
6262
if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) {
6363
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
@@ -73,6 +73,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7373
return;
7474
}
7575

76+
// If the parenthesis don't match, then we are not concerned.
77+
if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) {
78+
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
79+
return;
80+
}
81+
7682
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes');
7783

7884
if (($phpcsFile->findNext(PHP_CodeSniffer_Tokens::$operators, $stackPtr, $end, false)) === false) {

CodeSniffer/Standards/Squiz/Tests/Strings/EchoedStringsUnitTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ echo('hello');
66
echo($hello);
77
echo ('hello');
88
echo ($hello);
9+
echo ($loan_device->returndate == 0) ? 'Not returned' : date('d/m/Y', $loan_device->returndate);
910
?>
1011
<p><?php echo ($foo) ? 'true' : 'false' ?></p>
1112
<p><?php echo ($foo) ?></p>

CodeSniffer/Standards/Squiz/Tests/Strings/EchoedStringsUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getErrorList()
4747
6 => 1,
4848
7 => 1,
4949
8 => 1,
50-
11 => 1,
50+
12 => 1,
5151
);
5252

5353
}//end getErrorList()

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4545
- Fixed bug #515 : Spaces in JS block incorrectly flagged as indentation error
4646
- Fixed bug #523 : Generic ScopeIndent errors for IF in FINALLY
4747
- Fixed bug #527 : Closure inside IF statement is not tokenized correctly
48+
- Fixed bug #529 : Squiz.Strings.EchoedStrings gives false positive when echo'ing using an inline condition
4849
- Fixed bug #543 : SWITCH with closure in condition generates inline control structure error
4950
- Fixed bug #551 : Multiple catch blocks not checked in Squiz.ControlStructures.ControlSignature sniff
5051
- Fixed bug #554 : ScopeIndentSniff causes errors when encountering an unmatched parenthesis

0 commit comments

Comments
 (0)