jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/373433 )
Change subject: Prohibit some globals ...................................................................... Prohibit some globals Let's start with $wgTitle and $parserMemc. Change-Id: Ib75750212128bba70de7cd7d815b982982ed1505 --- A MediaWiki/Sniffs/VariableAnalysis/ForbiddenGlobalVariablesSniff.php A MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php A MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php.expect 3 files changed, 129 insertions(+), 0 deletions(-) Approvals: Legoktm: Looks good to me, approved jenkins-bot: Verified diff --git a/MediaWiki/Sniffs/VariableAnalysis/ForbiddenGlobalVariablesSniff.php b/MediaWiki/Sniffs/VariableAnalysis/ForbiddenGlobalVariablesSniff.php new file mode 100644 index 0000000..01bb65a --- /dev/null +++ b/MediaWiki/Sniffs/VariableAnalysis/ForbiddenGlobalVariablesSniff.php @@ -0,0 +1,84 @@ +<?php +/** + * Detect use of discouraged global variables. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +namespace MediaWiki\Sniffs\VariableAnalysis; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; + +class ForbiddenGlobalVariablesSniff implements Sniff { + + /** + * Forbidden globals + */ + private $forbiddenGlobals = [ + '$parserMemc', + '$wgTitle', + ]; + + /** + * @return array + */ + public function register() { + return [ T_FUNCTION ]; + } + + /** + * @param File $phpcsFile File object. + * @param int $stackPtr The current token index. + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + if ( !isset( $tokens[$stackPtr]['scope_opener'] ) ) { + // An interface or abstract function which doesn't have a body + return; + } + $scopeOpener = ++$tokens[$stackPtr]['scope_opener']; + $scopeCloser = $tokens[$stackPtr]['scope_closer']; + + $globalLine = 0; + $globalVariables = []; + + for ( $i = $scopeOpener; $i < $scopeCloser; $i++ ) { + if ( array_key_exists( $tokens[$i]['type'], Tokens::$emptyTokens ) ) { + continue; + } + if ( $tokens[$i]['type'] === 'T_GLOBAL' ) { + $globalLine = $tokens[$i]['line']; + } + if ( $tokens[$i]['type'] === 'T_VARIABLE' && $tokens[$i]['line'] == $globalLine ) { + $globalVariables[] = [ $tokens[$i]['content'], $i ]; + } + } + foreach ( $globalVariables as $global ) { + if ( in_array( $global[0], $this->forbiddenGlobals ) ) { + $phpcsFile->addWarning( + "Global {$global[0]} should not be used.", + $global[1], + 'ForbiddenGlobal' . $global[0] + ); + } + } + } +} diff --git a/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php b/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php new file mode 100644 index 0000000..73bf2a3 --- /dev/null +++ b/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php @@ -0,0 +1,41 @@ +<?php + +/** + * No errors should be found here + */ +function wfValidFunction() { + global $wgFoo, $wgBar; + + $wgTitle = 'fnord'; + echo $wgFoo, $wgBar, $wgTitle; +} + +/** + * This one should fail + */ +function wfInvalidFunction() { + global $wgFoo, $wgTitle; + + echo $wgTitle, $wgFoo; +} + +class Foo { + /** + * No errors should be found here + */ + public function validFunction() { + global $wgFoo, $wgBar; + + $wgTitle = 'fnord'; + echo $wgFoo, $wgBar, $wgTitle; + } + + /** + * This one should fail + */ + private function invalidFunction() { + global $wgFoo, $wgTitle; + + echo $wgFoo, $wgTitle; + } +} diff --git a/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php.expect b/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php.expect new file mode 100644 index 0000000..c909e30 --- /dev/null +++ b/MediaWiki/Tests/files/VariableAnalysis/forbidden_global_variables.php.expect @@ -0,0 +1,4 @@ + 17 | WARNING | Global $wgTitle should not be used. + | | (MediaWiki.VariableAnalysis.ForbiddenGlobalVariables.ForbiddenGlobal$wgTitle) + 37 | WARNING | Global $wgTitle should not be used. + | | (MediaWiki.VariableAnalysis.ForbiddenGlobalVariables.ForbiddenGlobal$wgTitle) -- To view, visit https://gerrit.wikimedia.org/r/373433 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ib75750212128bba70de7cd7d815b982982ed1505 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Legoktm <lego...@member.fsf.org> Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits