Umherirrender has uploaded a new change for review. https://gerrit.wikimedia.org/r/60199
Change subject: Add a OneLineStatementSniff ...................................................................... Add a OneLineStatementSniff This sniffs looks for curly braces around ifs and report missing braces Feel free to expand or rename it. Change-Id: I9a73087e6733732d0b8cb8a2fbec368e594f78b8 --- A MediaWiki/Sniffs/NamingConventions/OneLineStatementSniff.php 1 file changed, 48 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/99/60199/1 diff --git a/MediaWiki/Sniffs/NamingConventions/OneLineStatementSniff.php b/MediaWiki/Sniffs/NamingConventions/OneLineStatementSniff.php new file mode 100644 index 0000000..2345f0c --- /dev/null +++ b/MediaWiki/Sniffs/NamingConventions/OneLineStatementSniff.php @@ -0,0 +1,48 @@ +<?php +/** + * Check, if all control structure have curly braces around the body + * Some one line ifs omitted these. + */ +class MediaWiki_Sniffs_NamingConventions_OneLineStatementSniff implements PHP_CodeSniffer_Sniff { + + public function register() { + return array( + T_IF, + T_ELSEIF, + T_ELSE, + T_FOR, + T_FOREACH, + T_WHILE, + T_DO, + T_SWITCH, + T_TRY, + T_CATCH, + ); + } + + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + if ( !$this->isWhileOk( $tokens, $stackPtr ) && ( + !isset( $tokens[$stackPtr]['scope_opener'] ) || + !isset( $tokens[$stackPtr]['scope_closer'] ) + ) ) { + $error = 'Token \'%s\' without curly braces'; + $type = 'noCurlyBraces'; + $data = array( $tokens[$stackPtr]['content'] ); + $phpcsFile->addError( $error, $stackPtr, $type, $data ); + } + } + + /** + * by a do while the scope is at the do, so the ending while is reported as error, + * but the missing scope is okay, when the while has a T_SEMICOLON after it + */ + private function isWhileOk( $tokens, $stackPtr ) { + if ( $tokens[$stackPtr]['code'] !== T_WHILE ) { + return false; + } + $closer = $tokens[$stackPtr]['parenthesis_closer']; + return $closer < count( $tokens ) && $tokens[$closer + 1]['code'] === T_SEMICOLON; + } +} -- To view, visit https://gerrit.wikimedia.org/r/60199 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9a73087e6733732d0b8cb8a2fbec368e594f78b8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Umherirrender <umherirrender_de...@web.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits