Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support for PHP tokenizing of T_INLINE_ELSE colons. Fixes bug #…

…19666 : PSR1.Files.SideEffects throws a notice Undefined index: scope_closer
  • Loading branch information...
commit 5ea94def4cb705abcc51f1adf9d116021715f935 1 parent 5369695
Greg Sherwood authored October 26, 2012
9  CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php
@@ -108,13 +108,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
108 108
             $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
109 109
         }
110 110
 
111  
-        // If there is an else in this condition, make sure it has correct spacing.
112  
-        $inlineElse = $phpcsFile->findNext(T_COLON, ($stackPtr + 1), $statementEnd, false);
113  
-        if ($inlineElse === false) {
114  
-            // No else condition.
115  
-            return;
116  
-        }
117  
-
  111
+        // Make sure the ELSE has the correct spacing.
  112
+        $inlineElse    = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
118 113
         $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
119 114
         $contentAfter  = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
120 115
 
2  CodeSniffer/Standards/Squiz/Sniffs/Objects/ObjectInstantiationSniff.php
@@ -67,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67 67
                           T_THROW,
68 68
                           T_RETURN,
69 69
                           T_INLINE_THEN,
70  
-                          T_COLON,
  70
+                          T_INLINE_ELSE,
71 71
                          );
72 72
 
73 73
         if (in_array($tokens[$prev]['code'], $allowedTokens) === false) {
9  CodeSniffer/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php
@@ -146,12 +146,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
146 146
                 return;
147 147
             }
148 148
 
149  
-            if ($tokens[$i]['code'] === T_COLON) {
150  
-                $then = $phpcsFile->findPrevious(T_INLINE_THEN, ($i - 1), null, false, null, true);
151  
-                if ($then !== false) {
152  
-                    // We reached the end of the inline ELSE statement.
153  
-                    return;
154  
-                }
  149
+            if ($tokens[$i]['code'] === T_INLINE_ELSE) {
  150
+                // We reached the end of the inline ELSE statement.
  151
+                return;
155 152
             }
156 153
 
157 154
             if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
17  CodeSniffer/Tokenizers/PHP.php
@@ -243,6 +243,9 @@ public function tokenizeString($string, $eolChar='\n')
243 243
 
244 244
         $newStackPtr = 0;
245 245
         $numTokens   = count($tokens);
  246
+
  247
+        $insideInlineIf = false;
  248
+
246 249
         for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
247 250
             $token        = $tokens[$stackPtr];
248 251
             $tokenIsArray = is_array($token);
@@ -453,8 +456,18 @@ public function tokenizeString($string, $eolChar='\n')
453 456
             } else {
454 457
                 $newToken = PHP_CodeSniffer::standardiseToken($token);
455 458
 
456  
-                // This is a special condition for T_ARRAY tokens use to
457  
-                // type hint function arguments as being arrays. We want to keep
  459
+                // Convert colons that are actually the ELSE component of an
  460
+                // inline IF statement.
  461
+                if ($newToken['code'] === T_INLINE_THEN) {
  462
+                    $insideInlineIf = true;
  463
+                } else if ($insideInlineIf === true && $newToken['code'] === T_COLON) {
  464
+                    $insideInlineIf = false;
  465
+                    $newToken['code'] = T_INLINE_ELSE;
  466
+                    $newToken['type'] = 'T_INLINE_ELSE';
  467
+                }
  468
+
  469
+                // This is a special condition for T_ARRAY tokens used for
  470
+                // type hinting function arguments as being arrays. We want to keep
458 471
                 // the parenthsis map clean, so let's tag these tokens as
459 472
                 // T_ARRAY_HINT.
460 473
                 if ($newToken['code'] === T_ARRAY) {
11  CodeSniffer/Tokens.php
@@ -24,11 +24,12 @@
24 24
 define('T_COLON', 1006);
25 25
 define('T_STRING_CONCAT', 1007);
26 26
 define('T_INLINE_THEN', 1008);
27  
-define('T_NULL', 1009);
28  
-define('T_FALSE', 1010);
29  
-define('T_TRUE', 1011);
30  
-define('T_SEMICOLON', 1012);
31  
-define('T_EQUAL', 1013);
  27
+define('T_INLINE_ELSE', 1009);
  28
+define('T_NULL', 1010);
  29
+define('T_FALSE', 1011);
  30
+define('T_TRUE', 1012);
  31
+define('T_SEMICOLON', 1013);
  32
+define('T_EQUAL', 1014);
32 33
 define('T_MULTIPLY', 1015);
33 34
 define('T_DIVIDE', 1016);
34 35
 define('T_PLUS', 1017);
2  package.xml
@@ -37,6 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
37 37
   - Sniffs inside PHP 5.3 namespaces are now supported, along with the existing underscore-style emulated namespaces
38 38
     -- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
39 39
     -- Thanks to Till Klampaeckel for the patch
  40
+  - Added support for PHP tokenizing of T_INLINE_ELSE colons, so this token type is now available
  41
+    -- Fixes bug #19666 : PSR1.Files.SideEffects throws a notice Undefined index: scope_closer
40 42
   - Added Generic DisallowSpaceIndent to check that files are indented using tabs
41 43
   - Added Generic OneClassPerFileSniff to check that only one class is defined in each file
42 44
     -- Thanks to Andy Grunwald for the contribution

0 notes on commit 5ea94de

Please sign in to comment.
Something went wrong with that request. Please try again.