Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: add sniffs to check formatting of increment/decrement operators #2130

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
</properties>
</rule>

<!-- Covers rule: When using increment or decrement operators, there should be
no spaces between the operator and the variable it applies to. -->
<rule ref="Generic.WhiteSpace.IncrementDecrementSpacing"/>


<!--
#############################################################################
Expand Down Expand Up @@ -468,6 +472,19 @@
<rule ref="WordPress.PHP.NoSilencedErrors"/>


<!--
#############################################################################
Handbook: Operators - Increment/decrement operators.
Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators
#############################################################################
-->
<!-- Covers rule: Pre-increment/decrement should be favoured over post-increment/decrement
for stand-alone statements. -->
<rule ref="Universal.Operators.DisallowStandalonePostIncrementDecrement">
<type>warning</type>
</rule>


<!--
#############################################################################
Handbook: Database - Database Queries.
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ protected function get_last_ptr_on_line( $stackPtr ) {
$nextPtr = ( $stackPtr + 1 );

while ( isset( $tokens[ $nextPtr ] ) && $tokens[ $nextPtr ]['line'] === $currentLine ) {
$nextPtr++;
++$nextPtr;
// Do nothing, we just want the last token of the line.
}

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ protected function process_multi_line_array( $stackPtr, $opener, $closer ) {
&& substr( rtrim( $this->tokens[ $end_of_comment ]['content'] ), -2 ) !== '*/'
&& ( $end_of_comment + 1 ) < $end_of_this_item
) {
$end_of_comment++;
++$end_of_comment;
}

if ( $this->tokens[ $end_of_comment ]['line'] !== $this->tokens[ $end_of_last_item ]['line'] ) {
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function process_token( $stackPtr ) {

if ( \T_WHITESPACE === $this->tokens[ $i ]['code'] ) {
if ( $this->tokens[ $i ]['content'] === $this->phpcsFile->eolChar ) {
$newlines++;
++$newlines;
} else {
$spaces += $this->tokens[ $i ]['length'];
}
Expand Down
6 changes: 3 additions & 3 deletions WordPress/Sniffs/Arrays/MultipleStatementAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ protected function process_multi_line_array( $stackPtr, $items, $opener, $closer
$items[ $key ]['single_line'] = true;
} else {
$items[ $key ]['single_line'] = false;
$multi_line_count++;
++$multi_line_count;
}

if ( ( $index_end_position + 2 ) <= $this->maxColumn ) {
Expand All @@ -368,7 +368,7 @@ protected function process_multi_line_array( $stackPtr, $items, $opener, $closer
if ( ! isset( $double_arrow_cols[ $this->tokens[ $double_arrow ]['column'] ] ) ) {
$double_arrow_cols[ $this->tokens[ $double_arrow ]['column'] ] = 1;
} else {
$double_arrow_cols[ $this->tokens[ $double_arrow ]['column'] ]++;
++$double_arrow_cols[ $this->tokens[ $double_arrow ]['column'] ];
}
}
unset( $key, $item, $double_arrow, $has_array_opener, $last_index_token );
Expand Down Expand Up @@ -413,7 +413,7 @@ protected function process_multi_line_array( $stackPtr, $items, $opener, $closer
if ( ! isset( $double_arrow_cols[ $this->tokens[ $item['operatorPtr'] ]['column'] ] ) ) {
$double_arrow_cols[ $this->tokens[ $item['operatorPtr'] ]['column'] ] = 1;
} else {
$double_arrow_cols[ $this->tokens[ $item['operatorPtr'] ]['column'] ]++;
++$double_arrow_cols[ $this->tokens[ $item['operatorPtr'] ]['column'] ];
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions WordPress/Sniffs/NamingConventions/ValidHookNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
}

if ( $string !== $case_transform ) {
$case_errors++;
++$case_errors;
}
if ( $string !== $punct_transform ) {
$underscores++;
++$underscores;
}
}

Expand Down Expand Up @@ -270,18 +270,18 @@ protected function transform_complex_string( $string, $regex, $transform_type =
$is_variable = true;
if ( '{' === $part ) {
$has_braces = true;
$braces++;
++$braces;
}
continue;
}

if ( true === $is_variable ) {
if ( '[' === $part ) {
$has_braces = true;
$braces++;
++$braces;
}
if ( \in_array( $part, array( '}', ']' ), true ) ) {
$braces--;
--$braces;
}
if ( false === $has_braces && ' ' === $part ) {
$is_variable = false;
Expand Down
4 changes: 2 additions & 2 deletions WordPress/Sniffs/Security/EscapeOutputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function process_token( $stackPtr ) {
}

// Ignore the function itself.
$stackPtr++;
++$stackPtr;

$in_cast = false;

Expand Down Expand Up @@ -315,7 +315,7 @@ public function process_token( $stackPtr ) {

// Handle arrays for those functions that accept them.
if ( \T_ARRAY === $this->tokens[ $i ]['code'] ) {
$i++; // Skip the opening parenthesis.
++$i; // Skip the opening parenthesis.
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected function process_global_statement( $stackPtr, $in_function_scope ) {
}
}

$ptr++;
++$ptr;
}
unset( $var );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function process_token( $stackPtr ) {
$i = ( $scopeCloser + 1 );
while ( $this->tokens[ $i ]['line'] !== $this->tokens[ $trailingContent ]['line'] ) {
$this->phpcsFile->fixer->replaceToken( $i, '' );
$i++;
++$i;
}

// TODO: Instead a separate error should be triggered when content comes right after closing brace.
Expand Down