-
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
Add new core.ruleset.xml excluding extra sniffs, and other fixes
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,23 +46,24 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) | |
if ( false == $phpcsFile->findNext( array( T_OBJECT_OPERATOR ), $stackPtr + 1, null, null, null, true ) ) | ||
return; // This is not a call to the wpdb object | ||
|
||
// Check for whitelisting comment | ||
$whitelisted = false; | ||
$whitelist_pattern = '/db call\W*(ok|pass|clear|whitelist)/i'; | ||
// Check for whitelisting comments | ||
$endOfStatement = $phpcsFile->findNext( array( T_SEMICOLON ), $stackPtr + 1, null, null, null, true ); | ||
$endOfLineComment = ''; | ||
for ( $i = $endOfStatement + 1; $i < count( $tokens ); $i++ ) { | ||
|
||
if ( in_array( $tokens[$i]['code'], array( T_WHITESPACE ) ) ) { | ||
continue; | ||
} | ||
|
||
if ( $tokens[$i]['code'] != T_COMMENT ) { | ||
if ( $tokens[$i]['line'] !== $tokens[$endOfStatement]['line'] ) { | ||
break; | ||
} | ||
|
||
if ( preg_match( $whitelist_pattern, $tokens[$i]['content'], $matches ) > 0 ) { | ||
$whitelisted = true; | ||
if ( $tokens[$i]['code'] === T_COMMENT ) { | ||
$endOfLineComment .= $tokens[$i]['content']; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
westonruter
Author
Member
|
||
} | ||
|
||
} | ||
|
||
$whitelisted_db_call = false; | ||
if ( preg_match( '/db call\W*(ok|pass|clear|whitelist)/i', $endOfLineComment, $matches ) ) { | ||
$whitelisted_db_call = true; | ||
} | ||
|
||
// Check for Database Schema Changes | ||
|
@@ -75,13 +76,17 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) | |
} | ||
|
||
// Flag instance if not whitelisted | ||
if ( ! $whitelisted ) { | ||
if ( ! $whitelisted_db_call ) { | ||
$message = 'Usage of a direct database call is discouraged.'; | ||
$this->add_unique_message( $phpcsFile, 'warning', $stackPtr, $tokens[$stackPtr]['line'], $message ); | ||
} | ||
|
||
$whitelisted_cache = false; | ||
$cached = false; | ||
if ( ! empty( $tokens[$stackPtr]['conditions'] ) ) { | ||
if ( preg_match( '/cache\s+(ok|pass|clear|whitelist)/i', $endOfLineComment, $matches ) ) { | ||
$whitelisted_cache = true; | ||
} | ||
if ( ! $whitelisted_cache && ! empty( $tokens[$stackPtr]['conditions'] ) ) { | ||
$conditions = $tokens[$stackPtr]['conditions']; | ||
$scope_function = null; | ||
foreach ( $conditions as $condPtr => $condType ) { | ||
|
@@ -104,7 +109,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) | |
|
||
} | ||
|
||
if ( ! $cached ) { | ||
if ( ! $cached && ! $whitelisted_cache ) { | ||
$message = 'Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.'; | ||
$this->add_unique_message( $phpcsFile, 'error', $stackPtr, $tokens[$stackPtr]['line'], $message ); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,13 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) | |
$instance = $tokens[$stackPtr]; | ||
$varName = $instance['content']; | ||
|
||
// If we're overriding a superglobal with an assignment, no need to test | ||
$semicolon_position = $phpcsFile->findNext( array( T_SEMICOLON ), $stackPtr + 1, null, null, null, true ); | ||
$assignment_position = $phpcsFile->findNext( array( T_EQUAL ), $stackPtr + 1, null, null, null, true ); | ||
if ( $semicolon_position !== false && $assignment_position !== false && $assignment_position < $semicolon_position ) { | ||
This comment has been minimized.
Sorry, something went wrong.
shadyvb
Contributor
|
||
return; | ||
} | ||
|
||
if ( ! isset( $instance['nested_parenthesis'] ) ) { | ||
$phpcsFile->addError( 'Detected usage of a non-sanitized input variable: %s', $stackPtr, null, array( $tokens[$stackPtr]['content'] ) ); | ||
return; | ||
|
@@ -67,7 +74,6 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) | |
$varKey = $this->getArrayIndexKey( $phpcsFile, $tokens, $stackPtr ); | ||
|
||
if ( empty( $varKey ) ) { | ||
$phpcsFile->addWarning( 'Detected access of super global var %s without targeting a member variable.', $stackPtr, null, array( $varName ) ); | ||
return; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ public function getErrorList() | |
{ | ||
return array( | ||
4 => 2, | ||
17 => 1, | ||
); | ||
|
||
}//end getErrorList() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="WordPress Core"> | ||
<description>Non-controversial generally-agreed upon WordPress Coding Standards</description> | ||
|
||
<rule ref="WordPress.Arrays.ArrayDeclaration"/> | ||
<rule ref="WordPress.Classes.ValidClassName"/> | ||
<rule ref="WordPress.Files.FileName"/> | ||
<rule ref="WordPress.Formatting.MultipleStatementAlignment"/> | ||
<rule ref="WordPress.Functions.FunctionCallSignature"/> | ||
<rule ref="WordPress.Functions.FunctionDeclarationArgumentSpacing"/> | ||
<rule ref="WordPress.NamingConventions.ValidFunctionName"/> | ||
<rule ref="WordPress.PHP.DiscouragedFunctions"/> | ||
<rule ref="WordPress.Strings.DoubleQuoteUsage"/> | ||
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing"/> | ||
<rule ref="WordPress.WhiteSpace.OperatorSpacing"/> | ||
<rule ref="WordPress.WhiteSpace.PhpIndent"/> | ||
<rule ref="WordPress.WhiteSpace.ScopeIndent"/> | ||
|
||
</ruleset> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="WordPress"> | ||
<description>A custom coding standard.</description> | ||
<description>WordPress Coding Standards</description> | ||
</ruleset> |
@westonruter I really wish there was a
findNextAll
likepreg_match_all
, would make life a lot easier developing sniffs.Do you think i should submit a patch for PHPCS to include that ?
Maybe introduce a regex check instead of the regular textual matching, in the fourth ( i think ) parameter ?