[MediaWiki-commits] [Gerrit] mediawiki...codesniffer[master]: Add detection for calling global functions in target classes.
jenkins-bot has submitted this change and it was merged. Change subject: Add detection for calling global functions in target classes. .. Add detection for calling global functions in target classes. - Usage of global functions like wfMessage() should be warned where target classes available and providing its context equivalents. - Usage of global Variable like $wgUser should be warned, instead, calling $this->getUser() is better. - Usage of global Variable like $wgRequest should be warned, instead, calling $this->getRequest() is better. Change-Id: Id5ad984d14eea83ad385b34a718caaef499f5570 --- A MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php A MediaWiki/Tests/files/Usage/extend_class_usage.php A MediaWiki/Tests/files/Usage/extend_class_usage.php.expect 3 files changed, 281 insertions(+), 0 deletions(-) Approvals: EBernhardson: Looks good to me, approved jenkins-bot: Verified diff --git a/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php b/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php new file mode 100644 index 000..b49dee1 --- /dev/null +++ b/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php @@ -0,0 +1,169 @@ +msg() rather than wfMessage() on ContextSource extend. + * Should use $this->getUser() rather than $wgUser() on ContextSource extend. + * Should use $this->getRequest() rather than $wgRequest on ContextSource extend. + */ + +// @codingStandardsIgnoreStart +class MediaWiki_Sniffs_Usage_ExtendClassUsageSniff implements PHP_CodeSniffer_Sniff { + // @codingStandardsIgnoreEnd + + private $eligableCls = null; + + private $eligableFunc = null; + + public static $msgMap = [ + T_FUNCTION => 'function', + T_VARIABLE => 'variable' + ]; + + public static $checkConfig = [ + // All extended class name. + 'extendsCls' => [ + 'ContextSource' => true + ], + // All details of usage need to be check. + 'checkList' => [ + // Extended class name. + 'ContextSource' => [ + [ + // The check content. + 'content' => 'wfMessage', + // The content shows on report message. + 'msg_content' => 'wfMessage()', + // The check content code. + 'code' => T_FUNCTION, + // The expected content. + 'expect_content' => '$this->msg()', + // The expected content code. + 'expect_code' => T_FUNCTION + ], + [ + 'content' => '$wgUser', + 'msg_content' => '$wgUser', + 'code' => T_VARIABLE, + 'expect_content' => '$this->getUser()', + 'expect_code' => T_FUNCTION + ], + [ + 'content' => '$wgRequest', + 'msg_content' => '$wgRequest', + 'code' => T_VARIABLE, + 'expect_content' => '$this->getRequest()', + 'expect_code' => T_FUNCTION + ] + ] + ] + ]; + /** +* @return array +*/ + public function register() { + return [ + T_CLASS, + T_EXTENDS, + T_FUNCTION, + T_VARIABLE, + T_STRING + ]; + } + + /** +* @param PHP_CodeSniffer_File $phpcsFile PHP_CodeSniffer_File object. +* @param int $stackPtr The current token index. +* @return void +*/ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $currToken = $tokens[$stackPtr]; + + if ( $currToken['code'] === T_CLASS ) { + $extendsPtr = $phpcsFile->findNext( T_EXTENDS, $stackPtr ); + if ( $extendsPtr === false ) { + // No extends token found + return; + } + $baseClsPtr = $phpcsFile->findNext( T_STRING, $extendsPtr ); + $extClsContent = $tokens[$baseClsPtr]['content']; +
[MediaWiki-commits] [Gerrit] mediawiki...codesniffer[master]: Add detection for calling global functions in target classes.
Lethexie has uploaded a new change for review. https://gerrit.wikimedia.org/r/301335 Change subject: Add detection for calling global functions in target classes. .. Add detection for calling global functions in target classes. - Usage of global functions like wfMessage() should be warned where target classes available and providing its context equivalents. Change-Id: Id5ad984d14eea83ad385b34a718caaef499f5570 --- A MediaWiki/Sniffs/Usage/WfMessageUsageSniff.php A MediaWiki/Tests/files/Usage/wf_message_usage.php A MediaWiki/Tests/files/Usage/wf_message_usage.php.expect 3 files changed, 142 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/35/301335/1 diff --git a/MediaWiki/Sniffs/Usage/WfMessageUsageSniff.php b/MediaWiki/Sniffs/Usage/WfMessageUsageSniff.php new file mode 100644 index 000..8f04f8b --- /dev/null +++ b/MediaWiki/Sniffs/Usage/WfMessageUsageSniff.php @@ -0,0 +1,96 @@ +msg() is avaliable. + * + */ + +// @codingStandardsIgnoreStart +class MediaWiki_Sniffs_Usage_WfMessageUsageSniff implements PHP_CodeSniffer_Sniff { + // @codingStandardsIgnoreEnd + + private $eligableCls = null; + + private $eligableFunc = null; + + /** +* @return array +*/ + public function register() { + return [ + T_CLASS, + T_EXTENDS, + T_FUNCTION, + T_STRING + ]; + } + + /** +* @param PHP_CodeSniffer_File $phpcsFile PHP_CodeSniffer_File object. +* @param int $stackPtr The current token index. +* @return void +*/ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $currToken = $tokens[$stackPtr]; + + if ( $currToken['code'] === T_CLASS ) { + $extendsPtr = $phpcsFile->findNext( T_EXTENDS, $stackPtr ); + $baseClsPtr = $phpcsFile->findNext( T_STRING, $extendsPtr ); + // Here should be replaced with a mechanism that check if + // the base class is in the list of restricted classes + if ( !$tokens[$baseClsPtr]['content'] === 'ContentSource' ) { + return; + } else { + // Retrieve class name + $classNamePtr = $phpcsFile->findNext( T_STRING, $stackPtr ); + $this->eligableCls = array( + 'name' => $tokens[$classNamePtr]['content'], + 'scope_start' => $currToken['scope_opener'], + 'scope_end' => $currToken['scope_closer'] + ); + } + } + + if ( !empty( $this->eligableCls ) + && $stackPtr > $this->eligableCls['scope_start'] + && $stackPtr < $this->eligableCls['scope_end'] ) { + + if ( $currToken['code'] === T_FUNCTION ) { + $methodProps = $phpcsFile->getMethodProperties( $stackPtr ); + if ( !$methodProps['is_static'] ) { + $funcNamePtr = $phpcsFile->findNext( T_STRING, $stackPtr ); + $this->eligableFunc = array( + 'name' => $tokens[$funcNamePtr]['content'], + 'scope_start' => $currToken['scope_opener'], + 'scope_end' => $currToken['scope_closer'] + ); + } + } + + if ( !empty( $this->eligableFunc ) + && $stackPtr > $this->eligableFunc['scope_start'] + && $stackPtr < $this->eligableFunc['scope_end'] ) { + + if ( $currToken['content'] === 'wfMessage' ) { + $phpcsFile->addWarning( + 'Call wfMessage() instead of calling $this->msg()', + $stackPtr, + 'WfMessageFound' + ); + } + + // if reach the end of current function, clear function info + $scopeEndPtr = $phpcsFile->findNext( T_STRING, $stackPtr ); + if ( $scopeEndPtr >