Aashaka has uploaded a new change for review. https://gerrit.wikimedia.org/r/279615
Change subject: Add sniff to check if boolean operator on same line inside 'if condition' ...................................................................... Add sniff to check if boolean operator on same line inside 'if condition' If a boolean operator is the at the end of line in a multi-line 'if condition', a warning will be issued. On fixing, the operator will be sent to the starting of the next line. Cuurently, only && and || are checked. If need be, more operators can be added. Bug: T116561 Change-Id: I15d49e511302359cac235318c6fe65a58b5c8d85 --- A MediaWiki/Sniffs/ControlStructures/OperatorInSameLineSniff.php A MediaWiki/Tests/files/ControlStructures/operator_in_same_line_fail.php A MediaWiki/Tests/files/ControlStructures/operator_in_same_line_pass.php 3 files changed, 64 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/15/279615/1 diff --git a/MediaWiki/Sniffs/ControlStructures/OperatorInSameLineSniff.php b/MediaWiki/Sniffs/ControlStructures/OperatorInSameLineSniff.php new file mode 100644 index 0000000..d2d446f --- /dev/null +++ b/MediaWiki/Sniffs/ControlStructures/OperatorInSameLineSniff.php @@ -0,0 +1,46 @@ +<?php +/** + * Verify line continuation convention + * Provide a warning for the use of operators && and || at the end of a line + * Also provide a fix to use the operator at start of next line instead + * Fail: if ( $foo === '2' && + * $bar === false ) + * Fix: if ( $foo === '2' + * && $bar === false ) + * + * Per: https://www.mediawiki.org/wiki/Manual:Coding_conventions#Line_continuation + */ +// @codingStandardsIgnoreStart +class MediaWiki_Sniffs_ControlStructures_OperatorInSameLineSniff + implements PHP_CodeSniffer_Sniff { + // @codingStandardsIgnoreEnd + + public function register() { + return array( + T_BOOLEAN_AND, + T_BOOLEAN_OR + ); + } + + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + $s = $stackPtr + 1; + while($tokens[$s]['type']=== 'T_WHITESPACE'){ + $s = $s + 1; + } + + if($tokens[$s]['line'] === $tokens[$stackPtr]['line']) { + return; + } + $content = $tokens[$stackPtr]['content']; + $code = $content . 'InNextLine'; + $fix = $phpcsFile->addFixableWarning( 'Use ' . $content .' in start of the next line', $stackPtr, $code); + + if ($fix === true ) { + $phpcsFile->fixer->addContentBefore($s, $content . ' '); + $phpcsFile->fixer->replaceToken( $stackPtr, '' ); + } + } +} + diff --git a/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_fail.php b/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_fail.php new file mode 100644 index 0000000..47cd3bc --- /dev/null +++ b/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_fail.php @@ -0,0 +1,9 @@ +<?php + +if ( $foo === 2 || + $foo === 1 ){ + +} elseif ( $foo === 2 && + $foo + 1 === 3 ) { + +} diff --git a/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_pass.php b/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_pass.php new file mode 100644 index 0000000..fae7e24 --- /dev/null +++ b/MediaWiki/Tests/files/ControlStructures/operator_in_same_line_pass.php @@ -0,0 +1,9 @@ +<?php + +if ( $foo === 2 + || $foo === 1 ){ + +} elseif ( $foo === 2 + && $foo + 1 === 3 ) { + +} -- To view, visit https://gerrit.wikimedia.org/r/279615 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I15d49e511302359cac235318c6fe65a58b5c8d85 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Aashaka <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
