Changeset 1286
- Timestamp:
- 01/23/08 17:29:15 (10 months ago)
- Files:
-
- trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Classes/stubClassDeclarationSniff.php (modified) (5 diffs)
- trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Classes/stubClassFileNameSniff.php (added)
- trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/ControlStructures/stubSwitchDeclarationSniff.php (modified) (5 diffs)
- trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Functions/stubFunctionCallSignatureSniff.php (modified) (2 diffs)
- trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/StubblesCodingStandard.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Classes/stubClassDeclarationSniff.php
r1257 r1286 22 22 class Stubbles_Sniffs_Classes_stubClassDeclarationSniff extends PEAR_Sniffs_Classes_ClassDeclarationSniff 23 23 { 24 /* 25 * Stubbles 26 */ 27 /** 28 * exclude special classes 29 * 30 * @var array<string> 31 */ 32 protected $excludedClasses = array('stubClassNotFoundException', 33 'stubClassLoader' 34 ); 35 24 36 /** 25 37 * Returns an array of tokens this test wants to listen for. … … 34 46 ); 35 47 36 }//end register() 37 48 } 38 49 39 50 /** … … 60 71 61 72 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 } 65 84 } 66 85 … … 90 109 } 91 110 } 92 } //end if111 } 93 112 94 113 $closeBrace = $tokens[$stackPtr]['scope_closer']; … … 195 214 $nextComma = ($parents[$i] + 1); 196 215 } 197 }//end for 198 199 }//end process() 200 201 202 }//end class 203 216 } 217 } 218 } 204 219 ?> trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/ControlStructures/stubSwitchDeclarationSniff.php
r1258 r1286 31 31 } 32 32 33 34 33 /** 35 34 * Processes this test, when one of its tokens is encountered. … … 50 49 51 50 /* 51 if ($stackPtr === 53) { 52 foreach ($tokens as $token) { 53 echo $token['type'] . "\n"; 54 } 55 } 56 */ 57 58 /* 52 59 * Stubbles (no CASE statement at all should produce an error) 53 60 */ … … 173 180 No break found so far. Ensure that last line of 174 181 CASE statements are followed by RETURN/THROW 175 or comment: // break ommited182 or a comment which starts whith "// break ommited ... " 176 183 */ 177 184 … … 188 195 ); 189 196 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)) { 201 208 $error = 'CASE statement must have a BREAK, THROW or RETURN statement or the ' . 202 209 'the following comment: ' . $requiredComment; … … 209 216 */ 210 217 211 rsort($ next);212 $lastLineToken = $ next[0];218 rsort($prev); 219 $lastLineToken = $prev[0]; 213 220 $lastLine = $tokens[$lastLineToken]['line']; 214 221 $nextLine = $tokens[$tokens[$stackPtr]['scope_closer']]['line']; trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/Sniffs/Functions/stubFunctionCallSignatureSniff.php
r1173 r1286 16 16 * </ul> 17 17 * 18 * The method 'stubClassLoader::load()' is excluded for better readability.18 * Multiline Methods should close on the same column: 19 19 * So this is allowed: 20 20 * 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' 24 24 * ); 25 25 * 26 26 * ... and doesn't have to be: 27 27 * 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'); 31 31 * 32 * Copied and adapted content from PEAR/Sniffs/Functions/FunctionCallSignatureSniff 33 * because of no extending possibility (no methods/attributes to overwrite). 34 * 32 35 * @package Functions 33 36 */ 34 class Stubbles_Sniffs_Functions_stubFunctionCallSignatureSniff extends PEAR_Sniffs_Functions_FunctionCallSignatureSniff37 class Stubbles_Sniffs_Functions_stubFunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff 35 38 { 36 39 /** … … 56 59 { 57 60 $tokens = $phpcsFile->getTokens(); 61 62 // Find the next non-empty token. 63 $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); 58 64 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; 64 68 } 65 69 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 } 67 181 } 68 182 } trunk/src/main/php/org/stubbles/codeSniffer/Stubbles/StubblesCodingStandard.php
r1278 r1286 40 40 // Squiz 41 41 'Squiz/Sniffs/Arrays/ArrayBracketSpacingSniff.php', 42 'Squiz/Sniffs/Classes/ClassFileNameSniff.php',43 42 'Squiz/Sniffs/Classes/LowercaseClassKeywordsSniff.php', 44 43 'Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php',
