Umherirrender has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/400243 )
Change subject: Rework ExtendClassUageSniff to avoid private class member
......................................................................
Rework ExtendClassUageSniff to avoid private class member
Class member are unreliable in PHPCS 3.0+ and should be avoided.
Instead of register individual token to process,
a loop was added to jump from token to the next token.
The private member are now local variables inside the process.
Bug: T179753
Change-Id: Ic5def61874c9d583aa213328df8156076ad55648
---
M MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php
1 file changed, 44 insertions(+), 65 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer
refs/changes/43/400243/1
diff --git a/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php
b/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php
index 6881351..7837571 100644
--- a/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php
+++ b/MediaWiki/Sniffs/Usage/ExtendClassUsageSniff.php
@@ -13,10 +13,6 @@
class ExtendClassUsageSniff implements Sniff {
- private $eligableCls = null;
-
- private $eligableFunc = null;
-
public static $msgMap = [
T_FUNCTION => 'function',
T_VARIABLE => 'variable'
@@ -65,11 +61,7 @@
*/
public function register() {
return [
- T_CLASS,
- T_EXTENDS,
- T_FUNCTION,
- T_VARIABLE,
- T_STRING
+ T_CLASS
];
}
@@ -82,67 +74,57 @@
$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'];
- // Here should be replaced with a mechanism that check
if
- // the base class is in the list of restricted classes
- if ( !isset(
self::$checkConfig['extendsCls'][$extClsContent] ) ) {
- return;
- } else {
- // Retrieve class name.
- $classNamePtr = $phpcsFile->findNext( T_STRING,
$stackPtr );
- $this->eligableCls = [
- 'name' =>
$tokens[$classNamePtr]['content'],
- 'extendsCls' => $extClsContent,
- 'scope_start' =>
$currToken['scope_opener'],
- 'scope_end' =>
$currToken['scope_closer']
- ];
- }
+ $extendsPtr = $phpcsFile->findNext( T_EXTENDS, $stackPtr );
+ if ( $extendsPtr === false ) {
+ // No extends token found
+ return;
}
-
- if ( !empty( $this->eligableCls )
- && $stackPtr > $this->eligableCls['scope_start']
- && $stackPtr < $this->eligableCls['scope_end']
- ) {
- if ( $currToken['code'] === T_FUNCTION ) {
+ $baseClsPtr = $phpcsFile->findNext( T_STRING, $extendsPtr );
+ $extClsContent = $tokens[$baseClsPtr]['content'];
+ // Here should be replaced with a mechanism that check if
+ // the base class is in the list of restricted classes
+ if ( !isset( self::$checkConfig['extendsCls'][$extClsContent] )
) {
+ return;
+ }
+ $extClsCheckList = self::$checkConfig['checkList'][
$extClsContent ];
+ // Loop over all tokens of the class to check each function
+ $i = $currToken['scope_opener'];
+ $end = $currToken['scope_closer'];
+ $eligableFunc = null;
+ while ( $i !== false && $i < $end ) {
+ $iToken = $tokens[$i];
+ if ( $iToken['code'] === T_FUNCTION ) {
// If this is a function, make sure it's
eligible
// (i.e. not static or abstract, and has a
body).
- $methodProps = $phpcsFile->getMethodProperties(
$stackPtr );
+ $methodProps = $phpcsFile->getMethodProperties(
$i );
$isStaticOrAbstract = $methodProps['is_static']
|| $methodProps['is_abstract'];
- $hasBody = isset( $currToken['scope_opener'] )
- && isset( $currToken['scope_closer'] );
+ $hasBody = isset( $iToken['scope_opener'] )
+ && isset( $iToken['scope_closer'] );
if ( !$isStaticOrAbstract && $hasBody ) {
- $funcNamePtr = $phpcsFile->findNext(
T_STRING, $stackPtr );
- $this->eligableFunc = [
+ $funcNamePtr = $phpcsFile->findNext(
T_STRING, $i );
+ $eligableFunc = [
'name' =>
$tokens[$funcNamePtr]['content'],
- 'scope_start' =>
$currToken['scope_opener'],
- 'scope_end' =>
$currToken['scope_closer']
+ 'scope_start' =>
$iToken['scope_opener'],
+ 'scope_end' =>
$iToken['scope_closer']
];
}
}
- if ( !empty( $this->eligableFunc )
- && $stackPtr >
$this->eligableFunc['scope_start']
- && $stackPtr < $this->eligableFunc['scope_end']
+ if ( !empty( $eligableFunc )
+ && $i > $eligableFunc['scope_start']
+ && $i < $eligableFunc['scope_end']
) {
- // extend class name.
- $extClsContent =
$this->eligableCls['extendsCls'];
- $extClsCheckList =
self::$checkConfig['checkList'][ $extClsContent ];
+ // Inside a eligable function,
+ // check the current token against the checklist
foreach ( $extClsCheckList as $key => $value ) {
$condition = false;
if ( $value['code'] === T_FUNCTION
- && strtolower(
$currToken['content'] ) === strtolower( $value['content'] )
+ && strtolower(
$iToken['content'] ) === strtolower( $value['content'] )
) {
$condition = true;
}
if ( $value['code'] === T_VARIABLE
- && $currToken['content'] ===
$value['content']
+ && $iToken['content'] ===
$value['content']
) {
$condition = true;
}
@@ -152,26 +134,23 @@
$codeMsg = self::$msgMap[
$value['code'] ];
$phpcsFile->addWarning(
$warning,
- $stackPtr,
+ $i,
'FunctionVarUsage',
[ $expectCodeMsg,
$value['expect_content'], $codeMsg, $value['msg_content'] ]
);
- return;
}
}
-
- // 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;
+ // Jump to the next function
+ if ( $eligableFunc === null
+ || $i >= $eligableFunc['scope_end']
+ ) {
+ $start = $eligableFunc === null ? $i :
$eligableFunc['scope_end'];
+ $i = $phpcsFile->findNext( T_FUNCTION, $start +
1, $end );
+ continue;
}
+ // Find next token to work with
+ $i = $phpcsFile->findNext( [ T_STRING, T_VARIABLE,
T_FUNCTION ], $i + 1, $end );
}
}
}
--
To view, visit https://gerrit.wikimedia.org/r/400243
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic5def61874c9d583aa213328df8156076ad55648
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits