Changeset 1286

Show
Ignore:
Timestamp:
01/23/08 17:29:15 (10 months ago)
Author:
richi
Message:

enhancement #48: adapted coding standards due to code issues

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Classes/stubClassDeclarationSniff.php

    r1257 r1286  
    2222class Stubbles_Sniffs_Classes_stubClassDeclarationSniff extends PEAR_Sniffs_Classes_ClassDeclarationSniff 
    2323{ 
     24    /* 
     25     * Stubbles 
     26     */ 
     27    /** 
     28     * exclude special classes 
     29     * 
     30     * @var array<string> 
     31     */ 
     32    protected $excludedClasses = array('stubClassNotFoundException',  
     33                                       'stubClassLoader' 
     34                                 ); 
     35     
    2436    /** 
    2537     * Returns an array of tokens this test wants to listen for. 
     
    3446               ); 
    3547 
    36     }//end register() 
    37  
     48    } 
    3849 
    3950    /** 
     
    6071 
    6172        if ($nextClass !== false) { 
    62             // We have another, so an error is thrown. 
    63             $error = 'Only one interface or class is allowed in a file'; 
    64             $phpcsFile->addError($error, $nextClass); 
     73            /* 
     74            * Stubbles 
     75            */ 
     76            $classPtr  = $phpcsFile->findNext(T_STRING, $nextClass); 
     77            $className = $tokens[$classPtr]['content']; 
     78 
     79            if (in_array($className, $this->excludedClasses) === false) { 
     80                // We have another, so an error is thrown. 
     81                $error = 'Only one interface or class is allowed in a file'; 
     82                $phpcsFile->addError($error, $nextClass); 
     83            } 
    6584        } 
    6685 
     
    90109                } 
    91110            } 
    92         }//end if 
     111        } 
    93112 
    94113        $closeBrace = $tokens[$stackPtr]['scope_closer']; 
     
    195214                $nextComma = ($parents[$i] + 1); 
    196215            } 
    197         }//end for 
    198  
    199     }//end process() 
    200  
    201  
    202 }//end class 
    203  
     216        } 
     217    } 
     218
    204219?> 
  • trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/ControlStructures/stubSwitchDeclarationSniff.php

    r1258 r1286  
    3131    } 
    3232 
    33  
    3433    /** 
    3534     * Processes this test, when one of its tokens is encountered. 
     
    5049 
    5150        /* 
     51        if ($stackPtr === 53) { 
     52            foreach ($tokens as $token) { 
     53                echo $token['type'] . "\n"; 
     54            } 
     55        } 
     56        */ 
     57         
     58        /* 
    5259         * Stubbles (no CASE statement at all should produce an error) 
    5360         */ 
     
    173180                  No break found so far. Ensure that last line of 
    174181                  CASE statements are followed by RETURN/THROW 
    175                   or comment: // break ommited 
     182                  or a comment which starts whith "// break ommited ... " 
    176183                */ 
    177184 
     
    188195                        ); 
    189196 
    190                 $next['return']  = $phpcsFile->findNext(T_RETURN, $nextCase, $caseOrDefaultAhead); 
    191                 $next['throw']   = $phpcsFile->findNext(T_THROW, $nextCase, $caseOrDefaultAhead); 
    192                 $next['comment'] = $phpcsFile->findNext(T_COMMENT, $nextCase, $caseOrDefaultAhead); 
    193  
    194                 // echo "return: ".  $next['return']  ."\n"; 
    195                 // echo "throw: ".   $next['throw']   ."\n"; 
    196                 // echo "comment: ". $next['comment'] ."\n"; 
    197  
    198                 if ($next['throw']  === false && 
    199                     $next['return'] === false && 
    200                    ($next['comment'] === false || trim($tokens[$next['comment']]['content']) !== $requiredComment)) { 
     197                $prev['return']  = $phpcsFile->findPrevious(T_RETURN, $caseOrDefaultAhead, $nextCase); 
     198                $prev['throw']   = $phpcsFile->findPrevious(T_THROW, $caseOrDefaultAhead, $nextCase); 
     199                $prev['comment'] = $phpcsFile->findPrevious(T_COMMENT, $caseOrDefaultAhead, $nextCase); 
     200 
     201                // echo "return: ".  $prev['return']  ."\n"; 
     202                // echo "throw: ".   $prev['throw']   ."\n"; 
     203                // echo "comment: ". $prev['comment'] ."\n"; 
     204 
     205                if ($prev['throw']  === false && 
     206                    $prev['return'] === false && 
     207                   ($prev['comment'] === false || substr(ltrim($tokens[$prev['comment']]['content']), 0, 16) !== $requiredComment)) { 
    201208                        $error = 'CASE statement must have a BREAK, THROW or RETURN statement or the ' . 
    202209                                 'the following comment: ' . $requiredComment; 
     
    209216                    */ 
    210217 
    211                     rsort($next); 
    212                     $lastLineToken = $next[0]; 
     218                    rsort($prev); 
     219                    $lastLineToken = $prev[0]; 
    213220                    $lastLine  = $tokens[$lastLineToken]['line']; 
    214221                    $nextLine  = $tokens[$tokens[$stackPtr]['scope_closer']]['line']; 
  • trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Functions/stubFunctionCallSignatureSniff.php

    r1173 r1286  
    1616 * </ul> 
    1717 *  
    18  * The method 'stubClassLoader::load()' is excluded for better readability. 
     18 * Multiline Methods should close on the same column: 
    1919 * So this is allowed: 
    2020 * 
    21  *    stubClassLoader::load('net.stubbles.foo.bar
    22  *                          'net.stubbles.foo.bar.foo
    23  *                          'net.stubbles.bar.foo.bar 
     21 *    stubClassLoader::load('net.stubbles.foo.bar'
     22 *                          'net.stubbles.foo.bar.foo'
     23 *                          'net.stubbles.bar.foo.bar' 
    2424 *    ); 
    2525 * 
    2626 * ... and doesn't have to be: 
    2727 * 
    28  *    stubClassLoader::load('net.stubbles.foo.bar
    29  *                          'net.stubbles.foo.bar.foo
    30  *                          'net.stubbles.bar.foo.bar); 
     28 *    stubClassLoader::load('net.stubbles.foo.bar'
     29 *                          'net.stubbles.foo.bar.foo'
     30 *                          'net.stubbles.bar.foo.bar'); 
    3131 * 
     32 * Copied and adapted content from PEAR/Sniffs/Functions/FunctionCallSignatureSniff 
     33 * because of no extending possibility (no methods/attributes to overwrite). 
     34 *  
    3235 * @package   Functions 
    3336 */ 
    34 class Stubbles_Sniffs_Functions_stubFunctionCallSignatureSniff extends PEAR_Sniffs_Functions_FunctionCallSignatureSniff 
     37class Stubbles_Sniffs_Functions_stubFunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff 
    3538{ 
    3639    /** 
     
    5659    { 
    5760        $tokens = $phpcsFile->getTokens(); 
     61         
     62        // Find the next non-empty token. 
     63        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); 
    5864 
    59         if ($tokens[$stackPtr-2]['content'] === 'stubClassLoader' && 
    60             $tokens[$stackPtr-1]['content'] === '::' && 
    61             $tokens[$stackPtr]['content']   === 'load') { 
    62           // Ignore stubClassLoader for better readability 
    63           return; 
     65        if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) { 
     66            // Not a function call. 
     67            return; 
    6468        } 
    6569 
    66         parent::process($phpcsFile, $stackPtr); 
     70        if (isset($tokens[$next]['parenthesis_closer']) === false) { 
     71            // Not a function call. 
     72            return; 
     73        } 
     74 
     75        // Find the previous non-empty token. 
     76        $previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); 
     77        if ($tokens[$previous]['code'] === T_FUNCTION) { 
     78            // It's a function definition, not a function call. 
     79            return; 
     80        } 
     81 
     82        /* 
     83         * Stubbles 
     84         */         
     85        if ($tokens[$previous]['code'] === T_NEW && $tokens[$previous-2]['code'] !== T_THROW) { 
     86            // We are creating an object, not calling a function. 
     87            // but ignore exceptions 
     88            return; 
     89        } 
     90 
     91        if (($stackPtr + 1) !== $next) { 
     92            // Checking this: $value = my_function[*](...). 
     93            $error = 'Space before opening parenthesis of function call prohibited'; 
     94            $phpcsFile->addError($error, $stackPtr); 
     95        } 
     96         
     97 
     98        if ($tokens[($next + 1)]['code'] === T_WHITESPACE) { 
     99            // Checking this: $value = my_function([*]...). 
     100            $error = 'Space after opening parenthesis of function call prohibited'; 
     101            $phpcsFile->addError($error, $stackPtr); 
     102        } 
     103 
     104        $closer = $tokens[$next]['parenthesis_closer']; 
     105 
     106        if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) { 
     107            // Checking this: $value = my_function(...[*]). 
     108            $between = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true); 
     109 
     110            // Only throw an error if there is some content between the parenthesis. 
     111            // IE. Checking for this: $value = my_function(). 
     112            // If there is no content, then we would have thrown an error in the 
     113            // previous IF statement because it would look like this: 
     114            // $value = my_function( ). 
     115             
     116            /* 
     117             * Stubbles  
     118             */             
     119             
     120            // ignore functions within function calls 
     121            if($tokens[$closer+1]['code'] !== T_SEMICOLON) { 
     122                return; 
     123            } 
     124             
     125            $nextSemicolon  = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); 
     126            if($tokens[$nextSemicolon]['line'] === $tokens[$next]['line']) { 
     127                // one line function call 
     128                if ($between !== $closer) { 
     129                    $error = 'Space before closing parenthesis of function call prohibited'; 
     130                    $phpcsFile->addError($error, $closer); 
     131                } 
     132            } else { 
     133                // multiline function call 
     134                // Checking this: $obj->my_function( 
     135                //                      .... 
     136                //     indent ->  ); 
     137                // OR 
     138                //                throw new fooException ( 
     139                //                      ... 
     140                //     indent ->  );                 
     141                // OR 
     142                //                blaStatic::my_function ( 
     143                //                      ... 
     144                //     indent ->  );                 
     145                 
     146                $closingParenthesisIndent = $tokens[$nextSemicolon-1]['column']; 
     147                $prevVar                  = $phpcsFile->findPrevious(T_VARIABLE, ($next - 1)); 
     148                $prevThrow                = $phpcsFile->findPrevious(T_THROW, ($stackPtr - 1)); 
     149                $prevDoubleColon          = $phpcsFile->findPrevious(T_DOUBLE_COLON, ($next - 1)); 
     150                 
     151                if($prevVar && $tokens[$prevVar]['line'] === $tokens[$next]['line']) { 
     152                    if($tokens[$prevVar]['column'] !== $closingParenthesisIndent) { 
     153                        $error = 'Multiline Function call: Closing paranthesis indented incorrectly'; 
     154                        $phpcsFile->addError($error, $closer); 
     155                    } 
     156                } else if($prevThrow && $tokens[$prevThrow]['line'] === $tokens[$next]['line']) { 
     157                    $exception = $phpcsFile->findNext(T_STRING, ($prevThrow + 1)); 
     158                    if($tokens[$exception]['column'] !== $closingParenthesisIndent) { 
     159                        $error = 'Multiline Function call: Closing paranthesis indented incorrectly'; 
     160                        $phpcsFile->addError($error, $closer); 
     161                    }                         
     162                } else if($prevDoubleColon && $tokens[$prevDoubleColon]['line'] === $tokens[$next]['line']) { 
     163                    $staticClass = $prevDoubleColon-1; 
     164                    if($tokens[$staticClass]['column'] !== $closingParenthesisIndent) { 
     165                        $error = 'Multiline Function call: Closing paranthesis indented incorrectly'; 
     166                        $phpcsFile->addError($error, $closer); 
     167                    } 
     168                }       
     169            } 
     170             
     171        } 
     172 
     173        $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true); 
     174 
     175        if ($tokens[$next]['code'] === T_SEMICOLON) { 
     176            if (in_array($tokens[($closer + 1)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) { 
     177                $error = 'Space after closing parenthesis of function call prohibited'; 
     178                $phpcsFile->addError($error, $closer); 
     179            } 
     180        } 
    67181   } 
    68182} 
  • trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/StubblesCodingStandard.php

    r1278 r1286  
    4040                // Squiz 
    4141                'Squiz/Sniffs/Arrays/ArrayBracketSpacingSniff.php', 
    42                 'Squiz/Sniffs/Classes/ClassFileNameSniff.php', 
    4342                'Squiz/Sniffs/Classes/LowercaseClassKeywordsSniff.php', 
    4443                'Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php',