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 0000000..8f04f8b
--- /dev/null
+++ b/MediaWiki/Sniffs/Usage/WfMessageUsageSniff.php
@@ -0,0 +1,96 @@
+<?php
+/**
+ * Report warnings when wfMessage() is called but $this->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 >
$this->eligableFunc['scope_end'] ) {
+ $this->eligableFunc = null;
+ }
+ }
+
+ // if reach the end of current class, clear class
information
+ $scopeEndPtr = $phpcsFile->findNext( T_STRING,
$stackPtr );
+ if ( $scopeEndPtr > $this->eligableCls['scope_end'] ) {
+ $this->eligableCls = null;
+ }
+ }
+ }
+}
diff --git a/MediaWiki/Tests/files/Usage/wf_message_usage.php
b/MediaWiki/Tests/files/Usage/wf_message_usage.php
new file mode 100644
index 0000000..2dbc647
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/wf_message_usage.php
@@ -0,0 +1,35 @@
+<?php
+
+class TestOne extends ContextSource {
+
+ /**
+ * @return void
+ */
+ public function test() {
+ echo 'function test one';
+ }
+
+ /**
+ * @return string the unescaped message text.
+ */
+ public static function testTwo() {
+ return wfMessage( 'Test two' );
+ }
+
+ /**
+ * @return string the unescaped message text.
+ */
+ public function testThree() {
+ return wfMessage( 'Test three' );
+ }
+}
+
+class TestTwo extends ContextSource {
+
+ /**
+ * @return string the unescaped message text.
+ */
+ public function test() {
+ return wfMessage( 'Test message' );
+ }
+}
diff --git a/MediaWiki/Tests/files/Usage/wf_message_usage.php.expect
b/MediaWiki/Tests/files/Usage/wf_message_usage.php.expect
new file mode 100644
index 0000000..8c709d9
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/wf_message_usage.php.expect
@@ -0,0 +1,11 @@
+
+FILE: ...iki-codesniffer/MediaWiki/Tests/files/Usage/wf_message_usage.php
+----------------------------------------------------------------------
+FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
+----------------------------------------------------------------------
+ 23 | WARNING | Call wfMessage() instead of calling $this->msg()
+ 33 | WARNING | Call wfMessage() instead of calling $this->msg()
+----------------------------------------------------------------------
+
+Time: 24ms; Memory: 3.75Mb
+
--
To view, visit https://gerrit.wikimedia.org/r/301335
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5ad984d14eea83ad385b34a718caaef499f5570
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Lethexie <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits