Skip to content

Commit fddc61a

Browse files
committed
PHP tokenizer wasn't setting end tokens correctly, so alt syntax for switch didnt close case statements with no break (further fix for PHPCSStandards#497)
1 parent 6ac7251 commit fddc61a

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

CodeSniffer/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,7 @@ private static function _createLevelMap(&$tokens, $tokenizer, $eolChar)
23762376
reset($tokenizer->scopeOpeners[$thisType]['end']);
23772377
reset($tokenizer->scopeOpeners[$tokens[$opener]['code']]['end']);
23782378
$sameEnd = (current($tokenizer->scopeOpeners[$thisType]['end']) === current($tokenizer->scopeOpeners[$tokens[$opener]['code']]['end']));
2379+
23792380
if ($isShared === true && $sameEnd === true) {
23802381
$badToken = $opener;
23812382
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@@ -2475,8 +2476,7 @@ private static function _createLevelMap(&$tokens, $tokenizer, $eolChar)
24752476
// Make sure this closer actually belongs to us.
24762477
// Either the condition also has to think this is the
24772478
// closer, or it has to allow sharing with us.
2478-
$condition
2479-
= $tokens[$tokens[$i]['scope_condition']]['code'];
2479+
$condition = $tokens[$tokens[$i]['scope_condition']]['code'];
24802480
if ($condition !== $oldCondition) {
24812481
if (isset($tokenizer->scopeOpeners[$oldCondition]['with'][$condition]) === false) {
24822482
$badToken = $tokens[$oldOpener]['scope_condition'];

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,14 @@ switch ($this->error):
8787
Die eingetragene E-Mail-Adresse ist bereits registriert.
8888
<?php break;
8989
endswitch;
90+
91+
if ($this->allowShopping !== true):
92+
if ($this->status != Shop_Cart :: OK):
93+
switch ($this->status):
94+
case Shop_Cart :: NOT_FOUND:
95+
echo 'foo';
96+
endswitch;
97+
endif;
98+
else:
99+
echo 'foo';
100+
endif;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,14 @@ switch ($this->error):
8888
Die eingetragene E-Mail-Adresse ist bereits registriert.
8989
<?php break;
9090
endswitch;
91+
92+
if ($this->allowShopping !== true):
93+
if ($this->status != Shop_Cart :: OK):
94+
switch ($this->status):
95+
case Shop_Cart :: NOT_FOUND:
96+
echo 'foo';
97+
endswitch;
98+
endif;
99+
else:
100+
echo 'foo';
101+
endif;

CodeSniffer/Tokenizers/PHP.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ class PHP_CodeSniffer_Tokenizers_PHP
277277
*/
278278
public $endScopeTokens = array(
279279
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
280+
T_ENDIF => T_ENDIF,
281+
T_ENDFOR => T_ENDFOR,
282+
T_ENDFOREACH => T_ENDFOREACH,
283+
T_ENDWHILE => T_ENDWHILE,
284+
T_ENDSWITCH => T_ENDSWITCH,
280285
T_BREAK => T_BREAK,
281286
T_END_HEREDOC => T_END_HEREDOC,
282287
);

0 commit comments

Comments
 (0)