Skip to content

Commit 676e90b

Browse files
committed
Fixed bug #333 : Nested ternary operators causing problems
1 parent bc343a4 commit 676e90b

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

CodeSniffer/File.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,10 +1836,11 @@ private static function _recurseScopeMap(
18361836

18371837
if (PHP_CODESNIFFER_VERBOSITY > 1) {
18381838
$type = $tokens[$i]['type'];
1839+
$line = $tokens[$i]['line'];
18391840
$content = PHP_CodeSniffer::prepareForOutput($tokens[$i]['content']);
18401841

18411842
echo str_repeat("\t", $depth);
1842-
echo "Process token $i [";
1843+
echo "Process token $i on line $line [";
18431844
if ($opener !== null) {
18441845
echo "opener:$opener;";
18451846
}
@@ -1851,11 +1852,30 @@ private static function _recurseScopeMap(
18511852
echo "]: $type => $content".PHP_EOL;
18521853
}//end if
18531854

1855+
if ($opener === null && $tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
1856+
$i = $tokens[$i]['parenthesis_closer'];
1857+
1858+
// Because we might have skipped a bunch of lines, we can
1859+
// keep looking a little longer.
1860+
$startLine = $tokens[$i]['line'];
1861+
1862+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1863+
$type = $tokens[$stackPtr]['type'];
1864+
echo str_repeat("\t", $depth);
1865+
echo "* skipping parenthesis *".PHP_EOL;
1866+
}
1867+
1868+
continue;
1869+
}
1870+
18541871
// Very special case for IF statements in PHP that can be defined without
18551872
// scope tokens. E.g., if (1) 1; 1 ? (1 ? 1 : 1) : 1;
18561873
// If an IF statement below this one has an opener but no
18571874
// keyword, the opener will be incorrectly assigned to this IF statement.
1858-
if (($currType === T_IF || $currType === T_ELSE) && $opener === null && $tokens[$i]['code'] === T_SEMICOLON) {
1875+
if (($currType === T_IF || $currType === T_ELSE)
1876+
&& $opener === null
1877+
&& $tokens[$i]['code'] === T_SEMICOLON
1878+
) {
18591879
if (PHP_CODESNIFFER_VERBOSITY > 1) {
18601880
$type = $tokens[$stackPtr]['type'];
18611881
echo str_repeat("\t", $depth);
@@ -1933,7 +1953,7 @@ private static function _recurseScopeMap(
19331953
if (PHP_CODESNIFFER_VERBOSITY > 1) {
19341954
$type = $tokens[$stackPtr]['type'];
19351955
echo str_repeat("\t", $depth);
1936-
echo "=> Couldn't find scope opener for $stackPtr:$type, bailing".PHP_EOL;
1956+
echo "=> Found new opening condition before scope opener for $stackPtr:$type, bailing".PHP_EOL;
19371957
}
19381958

19391959
return $stackPtr;

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ elseif ($something) :
5656
else:
5757
echo 'false';
5858
endif;
59+
60+
function test()
61+
{
62+
if ($a)
63+
$a.=' '.($b ? 'b' : ($c ? ($d ? 'd' : 'c') : ''));
64+
}

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ elseif ($something) :
5757
else:
5858
echo 'false';
5959
endif;
60+
61+
function test()
62+
{
63+
if ($a) {
64+
$a.=' '.($b ? 'b' : ($c ? ($d ? 'd' : 'c') : '')); }
65+
}

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
5959
45 => 1,
6060
46 => 1,
6161
49 => 1,
62+
62 => 1,
6263
);
6364
break;
6465
case 'InlineControlStructureUnitTest.js':

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5454
- Improved fixing of lines after cases statements in Squiz SwitchDeclarationSniff
5555
- Fixed bug #311 : Suppression of function prototype breaks checking of lines within function
5656
- Fixed bug #320 : Code sniffer identation issue
57+
- Fixed bug #333 : Nested ternary operators causing problems
5758
</notes>
5859
<contents>
5960
<dir name="/">

0 commit comments

Comments
 (0)