Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Possible fix for bug #20151 : Problem handling "if(): ... else: ... endif;" syntax

View gist:8275236
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
$ git diff
diff --git a/CodeSniffer/File.php b/CodeSniffer/File.php
index f45e307..e629fed 100644
--- a/CodeSniffer/File.php
+++ b/CodeSniffer/File.php
@@ -1446,7 +1446,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$i]['type'];
$content = str_replace($eolChar, '\n', $tokens[$i]['content']);
- echo "\tStart scope map at $i: $type => $content".PHP_EOL;
+ echo "\tStart scope map at $i:$type => $content".PHP_EOL;
}
$i = self::_recurseScopeMap(
@@ -1531,12 +1531,63 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
- echo "=> Found semicolon before scope opener for $stackPtr ($type), bailing".PHP_EOL;
+ echo "=> Found semicolon before scope opener for $stackPtr:$type, bailing".PHP_EOL;
}
return $i;
}
+ if (in_array($tokenType, $tokenizer->scopeOpeners[$currType]['end']) === true
+ && $opener !== null
+ ) {
+ if ($ignore > 0 && $tokenType === T_CLOSE_CURLY_BRACKET) {
+ // The last opening bracket must have been for a string
+ // offset or alike, so let's ignore it.
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ echo str_repeat("\t", $depth);
+ echo '* finished ignoring curly brace *'.PHP_EOL;
+ }
+
+ $ignore--;
+ continue;
+ } else if ($tokens[$opener]['code'] === T_OPEN_CURLY_BRACKET
+ && $tokenType !== T_CLOSE_CURLY_BRACKET
+ ) {
+ // The opener is a curly bracket so the closer must be a curly bracket as well.
+ // We ignore this closer to handle cases such as T_ELSE or T_ELSEIF being considered
+ // a closer of T_IF when it should not.
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ $type = $tokens[$stackPtr]['type'];
+ echo str_repeat("\t", $depth);
+ echo "=> Ignoring non-culry scope closer for $stackPtr:$type".PHP_EOL;
+ }
+ } else {
+ if (PHP_CODESNIFFER_VERBOSITY > 1) {
+ $type = $tokens[$stackPtr]['type'];
+ $closerType = $tokens[$i]['type'];
+ echo str_repeat("\t", $depth);
+ echo "=> Found scope closer ($i:$closerType) for $stackPtr:$type".PHP_EOL;
+ }
+
+ foreach (array($stackPtr, $opener, $i) as $token) {
+ $tokens[$token]['scope_condition'] = $stackPtr;
+ $tokens[$token]['scope_opener'] = $opener;
+ $tokens[$token]['scope_closer'] = $i;
+ }
+
+ if ($tokenizer->scopeOpeners[$tokens[$stackPtr]['code']]['shared'] === true) {
+ // As we are going back to where we started originally, restore
+ // the ignore value back to its original value.
+ $ignore = $originalIgnore;
+ return $opener;
+ } else if (isset($tokenizer->scopeOpeners[$tokenType]) === true) {
+ return ($i - 1);
+ } else {
+ return $i;
+ }
+ }//end if
+ }//end if
+
// Is this an opening condition ?
if (isset($tokenizer->scopeOpeners[$tokenType]) === true) {
if ($opener === null) {
@@ -1545,7 +1596,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
- echo "=> Couldn't find scope opener for $stackPtr ($type), bailing".PHP_EOL;
+ echo "=> Couldn't find scope opener for $stackPtr:$type, bailing".PHP_EOL;
}
return $stackPtr;
@@ -1647,45 +1698,11 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
- echo "=> Found scope opener for $stackPtr ($type)".PHP_EOL;
+ echo "=> Found scope opener for $stackPtr:$type".PHP_EOL;
}
$opener = $i;
}
- } else if (in_array($tokenType, $tokenizer->scopeOpeners[$currType]['end']) === true
- && $opener !== null
- ) {
- if ($ignore > 0 && $tokenType === T_CLOSE_CURLY_BRACKET) {
- // The last opening bracket must have been for a string
- // offset or alike, so let's ignore it.
- if (PHP_CODESNIFFER_VERBOSITY > 1) {
- echo str_repeat("\t", $depth);
- echo '* finished ignoring curly brace *'.PHP_EOL;
- }
-
- $ignore--;
- } else {
- if (PHP_CODESNIFFER_VERBOSITY > 1) {
- $type = $tokens[$stackPtr]['type'];
- echo str_repeat("\t", $depth);
- echo "=> Found scope closer for $stackPtr ($type)".PHP_EOL;
- }
-
- foreach (array($stackPtr, $opener, $i) as $token) {
- $tokens[$token]['scope_condition'] = $stackPtr;
- $tokens[$token]['scope_opener'] = $opener;
- $tokens[$token]['scope_closer'] = $i;
- }
-
- if ($tokenizer->scopeOpeners[$tokens[$stackPtr]['code']]['shared'] === true) {
- // As we are going back to where we started originally, restore
- // the ignore value back to its original value.
- $ignore = $originalIgnore;
- return $opener;
- } else {
- return $i;
- }
- }//end if
} else if ($tokenType === T_OPEN_PARENTHESIS) {
if (isset($tokens[$i]['parenthesis_owner']) === true) {
$owner = $tokens[$i]['parenthesis_owner'];
@@ -1736,13 +1753,13 @@ class PHP_CodeSniffer_File
$type = $tokens[$stackPtr]['type'];
$lines = ($tokens[$i]['line'] - $startLine);
echo str_repeat("\t", $depth);
- echo "=> Still looking for $stackPtr ($type) scope opener after $lines lines".PHP_EOL;
+ echo "=> Still looking for $stackPtr:$type scope opener after $lines lines".PHP_EOL;
}
} else {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
- echo "=> Couldn't find scope opener for $stackPtr ($type), bailing".PHP_EOL;
+ echo "=> Couldn't find scope opener for $stackPtr:$type, bailing".PHP_EOL;
}
return $stackPtr;
@@ -1769,7 +1786,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
- echo "=> Found (unexpected) scope closer for $stackPtr ($type)".PHP_EOL;
+ echo "=> Found (unexpected) scope closer for $stackPtr:$type".PHP_EOL;
}
foreach (array($stackPtr, $opener) as $token) {
@@ -1844,7 +1861,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", ($level + 1));
- echo "=> Found scope opener for $stackPtr ($type)".PHP_EOL;
+ echo "=> Found scope opener for $stackPtr:$type".PHP_EOL;
}
$stackPtr = $tokens[$i]['scope_condition'];
@@ -1874,7 +1891,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$badToken]['type'];
echo str_repeat("\t", ($level + 1));
- echo "* shared closer, cleaning up $badToken ($type) *".PHP_EOL;
+ echo "* shared closer, cleaning up $badToken:$type *".PHP_EOL;
}
for ($x = $tokens[$i]['scope_condition']; $x <= $i; $x++) {
@@ -1900,7 +1917,7 @@ class PHP_CodeSniffer_File
$newLevel = $tokens[$x]['level'];
echo str_repeat("\t", ($level + 1));
- echo "* cleaned $x ($type) *".PHP_EOL;
+ echo "* cleaned $x:$type *".PHP_EOL;
echo str_repeat("\t", ($level + 2));
echo "=> level changed from $oldLevel to $newLevel".PHP_EOL;
echo str_repeat("\t", ($level + 2));
@@ -1912,7 +1929,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$badToken]['type'];
echo str_repeat("\t", ($level + 1));
- echo "* token $badToken ($type) removed from conditions array *".PHP_EOL;
+ echo "* token $badToken:$type removed from conditions array *".PHP_EOL;
}
unset ($openers[$lastOpener]);
@@ -1935,14 +1952,14 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", ($level + 1));
- echo "* token $stackPtr ($type) added to conditions array *".PHP_EOL;
+ echo "* token $stackPtr:$type added to conditions array *".PHP_EOL;
}
$lastOpener = $tokens[$i]['scope_opener'];
if ($lastOpener !== null) {
$openers[$lastOpener] = $lastOpener;
}
- } else if ($tokens[$i]['scope_closer'] === $i) {
+ } else if ($lastOpener !== null && $tokens[$lastOpener]['scope_closer'] === $i) {
foreach (array_reverse($openers) as $opener) {
if ($tokens[$opener]['scope_closer'] === $i) {
$oldOpener = array_pop($openers);
@@ -1956,7 +1973,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$oldOpener]['type'];
echo str_repeat("\t", ($level + 1));
- echo "=> Found scope closer for $oldOpener ($type)".PHP_EOL;
+ echo "=> Found scope closer for $oldOpener:$type".PHP_EOL;
}
$oldCondition = array_pop($conditions);
@@ -1977,7 +1994,7 @@ class PHP_CodeSniffer_File
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = token_name($oldCondition);
echo str_repeat("\t", ($level + 1));
- echo "* scope closer was bad, cleaning up $badToken ($type) *".PHP_EOL;
+ echo "* scope closer was bad, cleaning up $badToken:$type *".PHP_EOL;
}
for ($x = ($oldOpener + 1); $x <= $i; $x++) {
@@ -2003,7 +2020,7 @@ class PHP_CodeSniffer_File
$newLevel = $tokens[$x]['level'];
echo str_repeat("\t", ($level + 1));
- echo "* cleaned $x ($type) *".PHP_EOL;
+ echo "* cleaned $x:$type *".PHP_EOL;
echo str_repeat("\t", ($level + 2));
echo "=> level changed from $oldLevel to $newLevel".PHP_EOL;
echo str_repeat("\t", ($level + 2));
diff --git a/CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc b/CodeSniffer/Standards/Generic/Tests
index bc0d4b6..918c2db 100644
--- a/CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc
+++ b/CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc
@@ -39,4 +39,12 @@ foreach ($array as $element) :
echo 'hello';
endforeach;
+if ($foo) :
+ echo 'true';
+elseif ($something) :
+ echo 'foo';
+else:
+ echo 'false';
+endif;
+
?>
diff --git a/CodeSniffer/Tokenizers/PHP.php b/CodeSniffer/Tokenizers/PHP.php
index a9da6de..6c92029 100644
--- a/CodeSniffer/Tokenizers/PHP.php
+++ b/CodeSniffer/Tokenizers/PHP.php
@@ -45,10 +45,15 @@ class PHP_CodeSniffer_Tokenizers_PHP
'end' => array(
T_CLOSE_CURLY_BRACKET,
T_ENDIF,
+ T_ELSE,
+ T_ELSEIF,
),
'strict' => false,
'shared' => false,
- 'with' => array(),
+ 'with' => array(
+ T_ELSE,
+ T_ELSEIF,
+ ),
),
T_TRY => array(
'start' => array(T_OPEN_CURLY_BRACKET),
@@ -72,18 +77,38 @@ class PHP_CodeSniffer_Tokenizers_PHP
'with' => array(),
),
T_ELSE => array(
- 'start' => array(T_OPEN_CURLY_BRACKET),
- 'end' => array(T_CLOSE_CURLY_BRACKET),
+ 'start' => array(
+ T_OPEN_CURLY_BRACKET,
+ T_COLON,
+ ),
+ 'end' => array(
+ T_CLOSE_CURLY_BRACKET,
+ T_ENDIF,
+ ),
'strict' => false,
'shared' => false,
- 'with' => array(),
+ 'with' => array(
+ T_IF,
+ T_ELSEIF,
+ ),
),
T_ELSEIF => array(
- 'start' => array(T_OPEN_CURLY_BRACKET),
- 'end' => array(T_CLOSE_CURLY_BRACKET),
+ 'start' => array(
+ T_OPEN_CURLY_BRACKET,
+ T_COLON,
+ ),
+ 'end' => array(
+ T_CLOSE_CURLY_BRACKET,
+ T_ENDIF,
+ T_ELSE,
+ T_ELSEIF,
+ ),
'strict' => false,
'shared' => false,
- 'with' => array(),
+ 'with' => array(
+ T_IF,
+ T_ELSE,
+ ),
),
T_FOR => array(
'start' => array(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.