From fb3a31e7effac46cd74d0da5120433810c0c548f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 21 Jul 2017 02:43:24 +0200 Subject: [PATCH 01/37] Unit tests: Prevent bleed through between unit tests. --- .../Tests/Arrays/ArrayAssignmentRestrictionsUnitTest.php | 8 ++++++++ WordPress/Tests/DB/RestrictedClassesUnitTest.php | 8 ++++++++ .../Tests/Variables/VariableRestrictionsUnitTest.php | 8 ++++++++ WordPress/Tests/WP/I18nUnitTest.php | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/WordPress/Tests/Arrays/ArrayAssignmentRestrictionsUnitTest.php b/WordPress/Tests/Arrays/ArrayAssignmentRestrictionsUnitTest.php index d4d82e9210..f219b3a94b 100644 --- a/WordPress/Tests/Arrays/ArrayAssignmentRestrictionsUnitTest.php +++ b/WordPress/Tests/Arrays/ArrayAssignmentRestrictionsUnitTest.php @@ -33,6 +33,14 @@ protected function setUp() { ); } + /** + * Reset the $groups property. + */ + protected function tearDown() { + WordPress_AbstractArrayAssignmentRestrictionsSniff::$groups = array(); + parent::tearDown(); + } + /** * Returns the lines where errors should occur. * diff --git a/WordPress/Tests/DB/RestrictedClassesUnitTest.php b/WordPress/Tests/DB/RestrictedClassesUnitTest.php index bef641cf3a..94a8adef9d 100644 --- a/WordPress/Tests/DB/RestrictedClassesUnitTest.php +++ b/WordPress/Tests/DB/RestrictedClassesUnitTest.php @@ -37,6 +37,14 @@ protected function setUp() { ); } + /** + * Reset the $groups property. + */ + protected function tearDown() { + WordPress_AbstractFunctionRestrictionsSniff::$unittest_groups = array(); + parent::tearDown(); + } + /** * Skip this test on PHP 5.2 as otherwise testing the namespace resolving would fail. * diff --git a/WordPress/Tests/Variables/VariableRestrictionsUnitTest.php b/WordPress/Tests/Variables/VariableRestrictionsUnitTest.php index 354ab05187..4a2c217c95 100644 --- a/WordPress/Tests/Variables/VariableRestrictionsUnitTest.php +++ b/WordPress/Tests/Variables/VariableRestrictionsUnitTest.php @@ -57,6 +57,14 @@ protected function setUp() { ); } + /** + * Reset the $groups property. + */ + protected function tearDown() { + WordPress_AbstractVariableRestrictionsSniff::$groups = array(); + parent::tearDown(); + } + /** * Returns the lines where errors should occur. * diff --git a/WordPress/Tests/WP/I18nUnitTest.php b/WordPress/Tests/WP/I18nUnitTest.php index a2864005b8..a2de453114 100644 --- a/WordPress/Tests/WP/I18nUnitTest.php +++ b/WordPress/Tests/WP/I18nUnitTest.php @@ -23,6 +23,14 @@ protected function setUp() { PHP_CodeSniffer::setConfigData( 'text_domain', 'my-slug,default', true ); } + /** + * Reset the $groups property. + */ + protected function tearDown() { + PHP_CodeSniffer::setConfigData( 'text_domain', null, true ); + parent::tearDown(); + } + /** * Returns the lines where errors should occur. * From 06d67332dd73654e40f2f5063df9dd90f85157a4 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 00:09:14 +0200 Subject: [PATCH 02/37] Remove ArrayDeclaration sniff: move checks for spacing before and between parentheses #### `WordPress.Arrays.ArrayDeclaration.SpaceAfterKeyword` This check has been moved to the `ArrayDeclarationSpacing` sniff. While doing this, I also discovered a bug in the upstream code (which we were using). While the specific situation is quite obscure, it should still be addressed, so this has been fixed in the new check in `ArrayDeclarationSpacing` and I've also reported it upstream squizlabs/PHP_CodeSniffer/issues/1570 #### `WordPress.Arrays.ArrayDeclaration.SpaceInEmptyArray` This check has been moved to the `ArrayDeclarationSpacing` sniff. --- .../Arrays/ArrayDeclarationSpacingSniff.php | 59 ++++++++++++++++++- .../ArrayDeclarationSpacingUnitTest.inc | 33 +++++++++++ .../ArrayDeclarationSpacingUnitTest.inc.fixed | 31 ++++++++++ .../ArrayDeclarationSpacingUnitTest.php | 11 +++- 4 files changed, 130 insertions(+), 4 deletions(-) diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php index 06c18ea03a..17ea100e3c 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php @@ -13,6 +13,8 @@ * WordPress specific checks which are not covered by the `WordPress.Arrays.ArrayDeclaration`/ * `Squiz.Arrays.ArrayDeclaration` sniff. * + * - Check for no space between array keyword and array opener. + * - Check for no space between the parentheses of an empty array. * - Checks for one space after the array opener / before the array closer in single-line arrays. * - Checks that associative arrays are multi-line. * @@ -79,10 +81,63 @@ public function process_token( $stackPtr ) { $closer = $array_open_close['closer']; unset( $array_open_close ); - // This array is empty, so the below checks aren't necessary. - if ( ( $opener + 1 ) === $closer ) { + /* + * Long arrays only: Check for space between the array keyword and the open parenthesis. + */ + if ( T_ARRAY === $this->tokens[ $stackPtr ]['code'] ) { + + if ( ( $stackPtr + 1 ) !== $opener ) { + $error = 'There must be no space between the "array" keyword and the opening parenthesis'; + $error_code = 'SpaceAfterKeyword'; + + $nextNonWhitespace = $this->phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), ( $opener + 1 ), true ); + if ( $nextNonWhitespace !== $opener ) { + // Don't auto-fix: Something other than whitespace found between keyword and open parenthesis. + $this->phpcsFile->addError( $error, $stackPtr, $error_code ); + } else { + + $fix = $this->phpcsFile->addFixableError( $error, $stackPtr, $error_code ); + + if ( true === $fix ) { + $this->phpcsFile->fixer->beginChangeset(); + for ( $i = ( $stackPtr + 1 ); $i < $opener; $i++ ) { + $this->phpcsFile->fixer->replaceToken( $i, '' ); + } + $this->phpcsFile->fixer->endChangeset(); + unset( $i ); + } + } + unset( $error, $error_code, $nextNonWhitespace, $fix ); + } + } + + /* + * Check for empty arrays. + */ + $nextNonWhitespace = $this->phpcsFile->findNext( T_WHITESPACE, ( $opener + 1 ), ( $closer + 1 ), true ); + if ( $nextNonWhitespace === $closer ) { + + if ( ( $opener + 1 ) !== $closer ) { + $fix = $this->phpcsFile->addFixableError( + 'Empty array declaration must have no space between the parentheses', + $stackPtr, + 'SpaceInEmptyArray' + ); + + if ( true === $fix ) { + $this->phpcsFile->fixer->beginChangeset(); + for ( $i = ( $opener + 1 ); $i < $closer; $i++ ) { + $this->phpcsFile->fixer->replaceToken( $i, '' ); + } + $this->phpcsFile->fixer->endChangeset(); + unset( $i ); + } + } + + // This array is empty, so the below checks aren't necessary. return; } + unset( $nextNonWhitespace ); // We're only interested in single-line arrays. if ( $this->tokens[ $opener ]['line'] !== $this->tokens[ $closer ]['line'] ) { diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc index fbdc75748a..c61818b885 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc @@ -27,3 +27,36 @@ $bad = array( 'key1' => 'value1', /* comment */ 'key2' => 'value2' ); // Bad. // Test for (not) fixing non-associative array with a nested associative array which *will* be fixed. $bad = array( 'value1', 'value2', [ 'sub1' => 1, 'sub2' => 2 ], 'value4' ); // Bad. + +/* + * Test spacing between array keyword and open parenthesis. + */ +$a = array(); // OK. +$b = array (); // Bad. +$train = array +( + true, + 'aaa' +); // Bad - space between keyword and opener. + +$a = array +// Bad. +( 'a', 'b' ); + +$a = array /* Bad. */ ( 'a', 'b' ); + +/* + * Tests for empty array with space between parentheses. + */ +// OK. +$a = array(); +$value = array( /* comment */ ); +$x = array( + // comment + ); + +// Bad. +$value = array ( ); +$value = array( ); +$x = array( + ); diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed index a514cde186..c2a6a615fc 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed @@ -52,3 +52,34 @@ $bad = array( $bad = array( 'value1', 'value2', [ 'sub1' => 1, 'sub2' => 2 ], 'value4' ); // Bad. + +/* + * Test spacing between array keyword and open parenthesis. + */ +$a = array(); // OK. +$b = array(); // Bad. +$train = array( + true, + 'aaa' +); // Bad - space between keyword and opener. + +$a = array +// Bad. +( 'a', 'b' ); + +$a = array /* Bad. */ ( 'a', 'b' ); + +/* + * Tests for empty array with space between parentheses. + */ +// OK. +$a = array(); +$value = array( /* comment */ ); +$x = array( + // comment + ); + +// Bad. +$value = array(); +$value = array(); +$x = array(); diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php index dd4e6ce23e..fa7b95a10f 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php @@ -22,8 +22,8 @@ class WordPress_Tests_Arrays_ArrayDeclarationSpacingUnitTest extends AbstractSni */ public function getErrorList() { return array( - 5 => 2, - 8 => 2, + 5 => 2, + 8 => 2, 10 => 1, 13 => 4, 17 => 2, @@ -31,6 +31,13 @@ public function getErrorList() { 23 => 1, 26 => 1, 29 => 1, + 35 => 1, + 36 => 1, + 42 => 1, + 46 => 1, + 59 => 2, + 60 => 1, + 61 => 1, ); } From 2a9fd60b98807381a62868ddc30944a6b0407e2b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 00:11:54 +0200 Subject: [PATCH 03/37] Remove ArrayDeclaration sniff: move checks for array item/closing brace on new line #### `WordPress.Arrays.ArrayDeclaration.CloseBraceNewLine` This check has been moved to the `ArrayDeclarationSpacing` sniff. #### `WordPress.Arrays.ArrayDeclaration.FirstIndexNoNewline/FirstValueNoNewline/IndexNoNewline/ValueNoNewline` These checks have been moved to the `ArrayDeclarationSpacing` sniff and merged into one. I don't see any particular added value in having different error codes for the first line and subsequent lines, nor dependent on whether the array is associative or not. Includes some adjustments to the unit test case `fixed` file to account for these fixes being made to existing unit test cases. --- .../Arrays/ArrayDeclarationSpacingSniff.php | 139 +++++++++++++++++- .../ArrayDeclarationSpacingUnitTest.inc | 38 ++++- .../ArrayDeclarationSpacingUnitTest.inc.fixed | 85 +++++++++-- .../ArrayDeclarationSpacingUnitTest.php | 9 ++ 4 files changed, 256 insertions(+), 15 deletions(-) diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php index 17ea100e3c..b42646f17b 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php @@ -17,6 +17,8 @@ * - Check for no space between the parentheses of an empty array. * - Checks for one space after the array opener / before the array closer in single-line arrays. * - Checks that associative arrays are multi-line. + * - Checks that each array item in a multi-line array starts on a new line. + * - Checks that the array closer in a multi-line array is on a new line. * * @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#indentation * @@ -139,11 +141,27 @@ public function process_token( $stackPtr ) { } unset( $nextNonWhitespace ); - // We're only interested in single-line arrays. - if ( $this->tokens[ $opener ]['line'] !== $this->tokens[ $closer ]['line'] ) { - return; + // Pass off to either the single line or multi-line array analysis. + if ( $this->tokens[ $opener ]['line'] === $this->tokens[ $closer ]['line'] ) { + $this->process_single_line_array( $stackPtr, $opener, $closer ); + } else { + $this->process_multi_line_array( $stackPtr, $opener, $closer ); } + } + /** + * Process a single-line array. + * + * @since 0.13.0 The actual checks contained in this method used to + * be in the `process()` method. + * + * @param int $stackPtr The position of the current token in the stack. + * @param int $opener The position of the array opener. + * @param int $closer The position of the array closer. + * + * @return void + */ + protected function process_single_line_array( $stackPtr, $opener, $closer ) { /* * Check that associative arrays are always multi-line. */ @@ -219,7 +237,7 @@ public function process_token( $stackPtr ) { $this->phpcsFile->fixer->endChangeset(); } - // No need to check for spacing around parentheses as this array should be multi-line. + // No need to check for spacing around opener/closer as this array should be multi-line. return; } } @@ -279,4 +297,117 @@ public function process_token( $stackPtr ) { } } + /** + * Process a multi-line array. + * + * @since 0.13.0 The actual checks contained in this method used to + * be in the `ArrayDeclaration` sniff. + * + * @param int $stackPtr The position of the current token in the stack. + * @param int $opener The position of the array opener. + * @param int $closer The position of the array closer. + * + * @return void + */ + protected function process_multi_line_array( $stackPtr, $opener, $closer ) { + /* + * Check that the closing bracket is on a new line. + */ + $last_content = $this->phpcsFile->findPrevious( T_WHITESPACE, ( $closer - 1 ), $opener, true ); + if ( false !== $last_content + && $this->tokens[ $last_content ]['line'] === $this->tokens[ $closer ]['line'] + ) { + $fix = $this->phpcsFile->addFixableError( + 'Closing parenthesis of array declaration must be on a new line', + $closer, + 'CloseBraceNewLine' + ); + if ( true === $fix ) { + $this->phpcsFile->fixer->beginChangeset(); + + if ( $last_content < ( $closer - 1 ) + && T_WHITESPACE === $this->tokens[ ( $closer - 1 ) ]['code'] + ) { + // Remove whitespace which would otherwise becoming trailing + // (as it gives problems with the fixed file). + $this->phpcsFile->fixer->replaceToken( ( $closer - 1 ), '' ); + } + + $this->phpcsFile->fixer->addNewlineBefore( $closer ); + $this->phpcsFile->fixer->endChangeset(); + } + } + + /* + * Check that each array item starts on a new line. + */ + $array_items = $this->get_function_call_parameters( $stackPtr ); + $end_of_last_item = $opener; + + foreach ( $array_items as $item ) { + $end_of_this_item = ( $item['end'] + 1 ); + + // Find the line on which the item starts. + $first_content = $this->phpcsFile->findNext( + array( T_WHITESPACE, T_DOC_COMMENT_WHITESPACE ), + $item['start'], + $end_of_this_item, + true + ); + + // Ignore comments after array items if the next real content starts on a new line. + if ( T_COMMENT === $this->tokens[ $first_content ]['code'] ) { + $next = $this->phpcsFile->findNext( + array( T_WHITESPACE, T_DOC_COMMENT_WHITESPACE ), + ( $first_content + 1 ), + $end_of_this_item, + true + ); + + if ( false === $next ) { + // Shouldn't happen, but just in case. + $end_of_last_item = $end_of_this_item; + continue; + } + + if ( $this->tokens[ $next ]['line'] !== $this->tokens[ $first_content ]['line'] ) { + $first_content = $next; + } + } + + if ( false === $first_content ) { + // Shouldn't happen, but just in case. + $end_of_last_item = $end_of_this_item; + continue; + } + + if ( $this->tokens[ $end_of_last_item ]['line'] === $this->tokens[ $first_content ]['line'] ) { + + $fix = $this->phpcsFile->addFixableError( + 'Each item in a multi-line array must be on a new line', + $first_content, + 'ArrayItemNoNewLine' + ); + + if ( true === $fix ) { + + $this->phpcsFile->fixer->beginChangeset(); + + if ( $item['start'] <= ( $first_content - 1 ) + && T_WHITESPACE === $this->tokens[ ( $first_content - 1 ) ]['code'] + ) { + // Remove whitespace which would otherwise becoming trailing + // (as it gives problems with the fixed file). + $this->phpcsFile->fixer->replaceToken( ( $first_content - 1 ), '' ); + } + + $this->phpcsFile->fixer->addNewlineBefore( $first_content ); + $this->phpcsFile->fixer->endChangeset(); + } + } + + $end_of_last_item = $end_of_this_item; + } + } + } // End class. diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc index c61818b885..6acbfc006d 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc @@ -25,7 +25,7 @@ $bad = array( // Test for fixing associative arrays with comments between values. $bad = array( 'key1' => 'value1', /* comment */ 'key2' => 'value2' ); // Bad. -// Test for (not) fixing non-associative array with a nested associative array which *will* be fixed. +// Test for fixing non-associative array with a nested associative array which *will* be fixed. $bad = array( 'value1', 'value2', [ 'sub1' => 1, 'sub2' => 2 ], 'value4' ); // Bad. /* @@ -60,3 +60,39 @@ $value = array ( ); $value = array( ); $x = array( ); + +/* + * Tests for multi-line arrays - closing brace on new line + array items each on new line. + */ +// OK. +$value = array( + 1, + 2, /* Comment. */ + 3, +); + +$value = array( + 1 => $one, + 2 => $two, // Comment. + 3 => $three, // Comment. +); + +// Bad. +$value = array(1, + 2 , 3 , +); + +$value = array(1 => $one, + 2 => $two , /* Comment. */ 3 => $three , ); + +$value = array( + '1'=> TRUE, FALSE, '3' => 'aaa',); + +$x = array('name' => 'test', + ); + +$foo = array(1 +, 2); + +$fields = array( + 'value' => 'type'); diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed index c2a6a615fc..d48bcf3104 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed @@ -8,7 +8,8 @@ $query_vars = array( 'food' ); // Bad, no spaces after opening and before closin $test = array( 1, 2 ); $bad = array( -'key' => 'value' ); // Bad, each value of an associative array should start on a new line. +'key' => 'value' +); // Bad, each value of an associative array should start on a new line. // Test for fixing nested associative arrays. $bad = array( @@ -16,11 +17,15 @@ array( 'key1' => 'value1', 'key2' => [ 'sub1' => 1, -'sub2' => 2] ), +'sub2' => 2 +] +), $key3 => 'value3', [ 'value4', -10 => 'value5', ] ); // Bad. +10 => 'value5', +] +); // Bad. // Test for fixing mixed single & multi-line nested associative arrays. $bad = array( @@ -28,11 +33,14 @@ $bad = array( 'key1' => 'value1', array( 'sub1' => 1, -'sub2' => 2,)), +'sub2' => 2, +) +), $key3 => 'value3', [ 'value4', -10 => 'value5' ] +10 => 'value5' +] ); // Bad. // Test for fixing associative arrays with multiple values & line indented with whitespace. @@ -41,17 +49,25 @@ array( 'key2' => 'value2', $key3 => 'value3', 'value4', -10 => 'value5' ); // Bad. +10 => 'value5' +); // Bad. // Test for fixing associative arrays with comments between values. $bad = array( 'key1' => 'value1', /* comment */ -'key2' => 'value2' ); // Bad. +'key2' => 'value2' +); // Bad. -// Test for (not) fixing non-associative array with a nested associative array which *will* be fixed. -$bad = array( 'value1', 'value2', [ +// Test for fixing non-associative array with a nested associative array which *will* be fixed. +$bad = array( +'value1', +'value2', +[ 'sub1' => 1, -'sub2' => 2 ], 'value4' ); // Bad. +'sub2' => 2 +], +'value4' +); // Bad. /* * Test spacing between array keyword and open parenthesis. @@ -83,3 +99,52 @@ $x = array( $value = array(); $value = array(); $x = array(); + +/* + * Tests for multi-line arrays - closing brace on new line + array items each on new line. + */ +// OK. +$value = array( + 1, + 2, /* Comment. */ + 3, +); + +$value = array( + 1 => $one, + 2 => $two, // Comment. + 3 => $three, // Comment. +); + +// Bad. +$value = array( +1, + 2 , +3 , +); + +$value = array( +1 => $one, + 2 => $two , +/* Comment. */ 3 => $three , +); + +$value = array( + '1'=> TRUE, +FALSE, +'3' => 'aaa', +); + +$x = array( +'name' => 'test', + ); + +$foo = array( +1 +, +2 +); + +$fields = array( + 'value' => 'type' +); diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php index fa7b95a10f..013297b0d3 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php @@ -38,6 +38,15 @@ public function getErrorList() { 59 => 2, 60 => 1, 61 => 1, + 81 => 1, + 82 => 1, + 85 => 1, + 86 => 2, + 89 => 3, + 91 => 1, + 94 => 1, + 95 => 2, + 98 => 1, ); } From bb2266b94188946f7581afbdca06691509e8c56a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 09:45:45 +0200 Subject: [PATCH 04/37] Remove ArrayDeclaration sniff: deprecate the sniff and remove from ruleset * Empty out the ArrayDeclaration sniff file and remove references to it in the `WordPress-Core` ruleset. * Remove perfunctory test file explaining why this wasn't unit tested here. * Minor touch up of documentation referring to the sniff. * Remove the file exclusion from the WPCS own custom PHPCS ruleset. --- WordPress-Core/ruleset.xml | 14 +- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 622 +----------------- .../Arrays/ArrayDeclarationSpacingSniff.php | 6 +- .../Sniffs/Arrays/ArrayIndentationSniff.php | 4 +- .../Tests/Arrays/ArrayDeclarationUnitTest.txt | 13 - bin/phpcs.xml | 5 - 6 files changed, 38 insertions(+), 626 deletions(-) delete mode 100644 WordPress/Tests/Arrays/ArrayDeclarationUnitTest.txt diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index d85d4b924a..f31faff851 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -46,21 +46,13 @@ - - - - - - - - - - - + + + diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php index 9e1e4be808..b3fbb4560a 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -7,10 +7,6 @@ * @license https://opensource.org/licenses/MIT MIT */ -if ( ! class_exists( 'Squiz_Sniffs_Arrays_ArrayDeclarationSniff', true ) ) { - throw new PHP_CodeSniffer_Exception( 'Class Squiz_Sniffs_Arrays_ArrayDeclarationSniff not found' ); -} - /** * Enforces WordPress array format, based upon Squiz code. * @@ -18,600 +14,42 @@ * * @package WPCS\WordPressCodingStandards * - * @since 0.1.0 - * @since 0.5.0 Now extends `Squiz_Sniffs_Arrays_ArrayDeclarationSniff`. - * @since 0.11.0 The additional single-line array checks have been moved to their own - * sniff WordPress.Arrays.ArrayDeclarationSpacing. - * This class now only contains a slimmed down version of the upstream sniff. - * - * DO NOT MAKE CHANGES TO THIS SNIFF - other than syncing with upstream -. - * ANY CHANGES NECESSARY SHOULD BE PULLED TO THE UPSTREAM SNIFF AND SYNCED IN AFTER MERGE! - * The code style of this sniff should be ignored so as to facilitate easy syncing with - * upstream. - * - * {@internal The upstream version is similar, except that we exclude a few errors. - * Unfortunately we have to actually comment out the code rather than just using - * the upstream sniff and `` in our ruleset, due to a bug/non-configurability - * of the sniff. - * {@see https://github.com/squizlabs/PHP_CodeSniffer/issues/582} }} - * - * Last synced with parent class October 5 2016 at commit ea32814346ecf29791de701b3fa464a9ca43f45b. - * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php + * @since 0.1.0 + * @since 0.5.0 Now extends `Squiz_Sniffs_Arrays_ArrayDeclarationSniff`. + * @since 0.11.0 The additional single-line array checks have been moved to their own + * sniff WordPress.Arrays.ArrayDeclarationSpacing. + * This class now only contains a slimmed down version of the upstream sniff. + * @deprecated 0.13.0 This sniff has now been deprecated. Most checks which were previously + * contained herein had recently been excluded in favour of dedicated + * sniffs with higher precision. The last remaining checks which were not + * already covered elsewhere have been moved to the `ArrayDeclarationSpacing` + * sniff. + * This class is left here to prevent breaking custom rulesets which refer + * to this sniff. */ -class WordPress_Sniffs_Arrays_ArrayDeclarationSniff extends Squiz_Sniffs_Arrays_ArrayDeclarationSniff { +class WordPress_Sniffs_Arrays_ArrayDeclarationSniff { /** - * Process a multi-line array. + * Don't use. * - * @since 0.5.0 + * @deprecated 0.13.0 * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. - * @param int $arrayStart Position of the array opener in the token stack. - * @param int $arrayEnd Position of the array closer in the token stack. + * @return int[] */ - public function processMultiLineArray( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $arrayStart, $arrayEnd ) { - $tokens = $phpcsFile->getTokens(); - $keywordStart = $tokens[ $stackPtr ]['column']; - - // Check the closing bracket is on a new line. - $lastContent = $phpcsFile->findPrevious( T_WHITESPACE, ( $arrayEnd - 1 ), $arrayStart, true ); - if ( $tokens[ $lastContent ]['line'] === $tokens[ $arrayEnd ]['line'] ) { - $error = 'Closing parenthesis of array declaration must be on a new line'; - $fix = $phpcsFile->addFixableError( $error, $arrayEnd, 'CloseBraceNewLine' ); - if ($fix === true) { - $phpcsFile->fixer->addNewlineBefore( $arrayEnd ); - } - /* - } elseif ( $tokens[ $arrayEnd ]['column'] !== $keywordStart ) { - // Check the closing bracket is lined up under the "a" in array. - $expected = ( $keywordStart - 1 ); - $found = ( $tokens[ $arrayEnd ]['column'] - 1 ); - $error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s'; - $data = array( - $expected, - $found, - ); - - $fix = $phpcsFile->addFixableError( $error, $arrayEnd, 'CloseBraceNotAligned', $data ); - if ($fix === true) { - if ($found === 0) { - $phpcsFile->fixer->addContent( ( $arrayEnd - 1 ), str_repeat(' ', $expected ) ); - } else { - $phpcsFile->fixer->replaceToken( ( $arrayEnd - 1 ), str_repeat(' ', $expected ) ); - } - } - */ - } // end if - - $keyUsed = false; - $singleUsed = false; - $indices = array(); - $maxLength = 0; - - if ($tokens[$stackPtr]['code'] === T_ARRAY) { - $lastToken = $tokens[ $stackPtr ]['parenthesis_opener']; - } else { - $lastToken = $stackPtr; - } - - // Find all the double arrows that reside in this scope. - for ( $nextToken = ( $stackPtr + 1 ); $nextToken < $arrayEnd; $nextToken++ ) { - // Skip bracketed statements, like function calls. - if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS - && (isset($tokens[$nextToken]['parenthesis_owner']) === false - || $tokens[ $nextToken ]['parenthesis_owner'] !== $stackPtr ) - ) { - $nextToken = $tokens[ $nextToken ]['parenthesis_closer']; - continue; - } - - if ( in_array( $tokens[ $nextToken ]['code'], array( T_ARRAY, T_OPEN_SHORT_ARRAY, T_CLOSURE ), true ) === true ) { - // Let subsequent calls of this test handle nested arrays. - if ($tokens[$lastToken]['code'] !== T_DOUBLE_ARROW) { - $indices[] = array( 'value' => $nextToken ); - $lastToken = $nextToken; - } - - if ($tokens[$nextToken]['code'] === T_ARRAY) { - $nextToken = $tokens[ $tokens[ $nextToken ]['parenthesis_opener'] ]['parenthesis_closer']; - } else if ($tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY) { - $nextToken = $tokens[ $nextToken ]['bracket_closer']; - } else { - // T_CLOSURE. - $nextToken = $tokens[ $nextToken ]['scope_closer']; - } - - $nextToken = $phpcsFile->findNext( T_WHITESPACE, ( $nextToken + 1 ), null, true ); - if ($tokens[$nextToken]['code'] !== T_COMMA) { - $nextToken--; - } else { - $lastToken = $nextToken; - } - - continue; - }//end if - - if ($tokens[$nextToken]['code'] !== T_DOUBLE_ARROW - && $tokens[$nextToken]['code'] !== T_COMMA - ) { - continue; - } - - $currentEntry = array(); - - if ($tokens[$nextToken]['code'] === T_COMMA) { - /* - $stackPtrCount = 0; - if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { - $stackPtrCount = count( $tokens[ $stackPtr ]['nested_parenthesis'] ); - } - - $commaCount = 0; - if (isset($tokens[$nextToken]['nested_parenthesis']) === true) { - $commaCount = count( $tokens[ $nextToken ]['nested_parenthesis'] ); - if ($tokens[$stackPtr]['code'] === T_ARRAY) { - // Remove parenthesis that are used to define the array. - $commaCount--; - } - } - - if ( $commaCount > $stackPtrCount ) { - // This comma is inside more parenthesis than the ARRAY keyword, - // then there it is actually a comma used to separate arguments - // in a function call. - continue; - } - - if ($keyUsed === true && $tokens[$lastToken]['code'] === T_COMMA) { - $error = 'No key specified for array entry; first entry specifies key'; - $phpcsFile->addError( $error, $nextToken, 'NoKeySpecified' ); - return; - } - */ - - if ($keyUsed === false) { - /* - if ($tokens[($nextToken - 1)]['code'] === T_WHITESPACE) { - $content = $tokens[ ( $nextToken - 2 ) ]['content']; - if ( $tokens[ ( $nextToken - 1 ) ]['content'] === $phpcsFile->eolChar ) { - $spaceLength = 'newline'; - } else { - $spaceLength = $tokens[ ( $nextToken - 1 ) ]['length']; - } - - $error = 'Expected 0 spaces between "%s" and comma; %s found'; - $data = array( - $content, - $spaceLength, - ); - - $fix = $phpcsFile->addFixableError( $error, $nextToken, 'SpaceBeforeComma', $data ); - if ($fix === true) { - $phpcsFile->fixer->replaceToken( ( $nextToken - 1 ), '' ); - } - } - */ - - $valueContent = $phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, - ( $lastToken + 1 ), - $nextToken, - true - ); - - $indices[] = array( 'value' => $valueContent ); - // $singleUsed = true; - }//end if - - $lastToken = $nextToken; - continue; - - }//end if - - if ($tokens[$nextToken]['code'] === T_DOUBLE_ARROW) { - /* - if ($singleUsed === true) { - $error = 'Key specified for array entry; first entry has no key'; - $phpcsFile->addError( $error, $nextToken, 'KeySpecified' ); - return; - } - */ - - $currentEntry['arrow'] = $nextToken; - $keyUsed = true; - - // Find the start of index that uses this double arrow. - $indexEnd = $phpcsFile->findPrevious( T_WHITESPACE, ( $nextToken - 1 ), $arrayStart, true ); - $indexStart = $phpcsFile->findStartOfStatement( $indexEnd ); - - if ( $indexStart === $indexEnd ) { - $currentEntry['index'] = $indexEnd; - $currentEntry['index_content'] = $tokens[ $indexEnd ]['content']; - } else { - $currentEntry['index'] = $indexStart; - $currentEntry['index_content'] = $phpcsFile->getTokensAsString( $indexStart, ( $indexEnd - $indexStart + 1 ) ); - } - - $indexLength = strlen( $currentEntry['index_content'] ); - if ( $maxLength < $indexLength ) { - $maxLength = $indexLength; - } - - // Find the value of this index. - $nextContent = $phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, - ( $nextToken + 1 ), - $arrayEnd, - true - ); - - $currentEntry['value'] = $nextContent; - $indices[] = $currentEntry; - $lastToken = $nextToken; - }//end if - }//end for - - // Check for mutli-line arrays that should be single-line. - $singleValue = false; - - if (empty($indices) === true) { - $singleValue = true; - } else if (count($indices) === 1 && $tokens[$lastToken]['code'] === T_COMMA) { - // There may be another array value without a comma. - $exclude = PHP_CodeSniffer_Tokens::$emptyTokens; - $exclude[] = T_COMMA; - $nextContent = $phpcsFile->findNext( $exclude, ( $indices[0]['value'] + 1 ), $arrayEnd, true ); - if ($nextContent === false) { - $singleValue = true; - } - } - - /* - if ($singleValue === true) { - // Array cannot be empty, so this is a multi-line array with - // a single value. It should be defined on single line. - $error = 'Multi-line array contains a single value; use single-line array instead'; - $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'MultiLineNotAllowed' ); - - if ($fix === true) { - $phpcsFile->fixer->beginChangeset(); - for ( $i = ( $arrayStart + 1 ); $i < $arrayEnd; $i++ ) { - if ($tokens[$i]['code'] !== T_WHITESPACE) { - break; - } - - $phpcsFile->fixer->replaceToken( $i, '' ); - } - - for ( $i = ( $arrayEnd - 1 ); $i > $arrayStart; $i-- ) { - if ($tokens[$i]['code'] !== T_WHITESPACE) { - break; - } - - $phpcsFile->fixer->replaceToken( $i, '' ); - } - - $phpcsFile->fixer->endChangeset(); - } - - return; - } // end if - */ - - /* - This section checks for arrays that don't specify keys. - - Arrays such as: - array( - 'aaa', - 'bbb', - 'd', - ); - */ - - if ($keyUsed === false && empty($indices) === false) { - $count = count( $indices ); - $lastIndex = $indices[ ( $count - 1 ) ]['value']; - - $trailingContent = $phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, - ( $arrayEnd - 1 ), - $lastIndex, - true - ); - - /* - if ($tokens[$trailingContent]['code'] !== T_COMMA) { - $phpcsFile->recordMetric( $stackPtr, 'Array end comma', 'no' ); - $error = 'Comma required after last value in array declaration'; - $fix = $phpcsFile->addFixableError( $error, $trailingContent, 'NoCommaAfterLast' ); - if ($fix === true) { - $phpcsFile->fixer->addContent( $trailingContent, ',' ); - } - } else { - $phpcsFile->recordMetric( $stackPtr, 'Array end comma', 'yes' ); - } - */ + public function register() { + return array(); + } - $lastValueLine = false; - foreach ( $indices as $value ) { - if (empty($value['value']) === true) { - // Array was malformed and we couldn't figure out - // the array value correctly, so we have to ignore it. - // Other parts of this sniff will correct the error. - continue; - } - - if ($lastValueLine !== false && $tokens[$value['value']]['line'] === $lastValueLine) { - $error = 'Each value in a multi-line array must be on a new line'; - $fix = $phpcsFile->addFixableError( $error, $value['value'], 'ValueNoNewline' ); - if ($fix === true) { - if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->replaceToken( ( $value['value'] - 1 ), '' ); - } - - $phpcsFile->fixer->addNewlineBefore( $value['value'] ); - } - /* - } else if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { - $expected = $keywordStart; - - $first = $phpcsFile->findFirstOnLine( T_WHITESPACE, $value['value'], true ); - $found = ($tokens[ $first ]['column'] - 1); - if ( $found !== $expected ) { - $error = 'Array value not aligned correctly; expected %s spaces but found %s'; - $data = array( - $expected, - $found, - ); - - $fix = $phpcsFile->addFixableError( $error, $value['value'], 'ValueNotAligned', $data ); - if ($fix === true) { - if ($found === 0) { - $phpcsFile->fixer->addContent( ( $value['value'] - 1 ), str_repeat(' ', $expected ) ); - } else { - $phpcsFile->fixer->replaceToken( ( $value['value'] - 1 ), str_repeat(' ', $expected ) ); - } - } - } - */ - } // end if - - $lastValueLine = $tokens[ $value['value'] ]['line']; - }//end foreach - }//end if - - /* - Below the actual indentation of the array is checked. - Errors will be thrown when a key is not aligned, when - a double arrow is not aligned, and when a value is not - aligned correctly. - If an error is found in one of the above areas, then errors - are not reported for the rest of the line to avoid reporting - spaces and columns incorrectly. Often fixing the first - problem will fix the other 2 anyway. - - For example: - - $a = array( - 'index' => '2', - ); - - or - - $a = [ - 'index' => '2', - ]; - - In this array, the double arrow is indented too far, but this - will also cause an error in the value's alignment. If the arrow were - to be moved back one space however, then both errors would be fixed. - */ - - $numValues = count( $indices ); - - $indicesStart = ( $keywordStart + 1 ); - $arrowStart = ( $indicesStart + $maxLength + 1 ); - //$valueStart = ($arrowStart + 3); - $indexLine = $tokens[ $stackPtr ]['line']; - //$lastIndexLine = null; - foreach ( $indices as $index ) { - if (isset($index['index']) === false) { - // Array value only. - if ( $tokens[ $index['value'] ]['line'] === $tokens[ $stackPtr ]['line'] && $numValues > 1 ) { - $error = 'The first value in a multi-value array must be on a new line'; - $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'FirstValueNoNewline' ); - if ($fix === true) { - $phpcsFile->fixer->addNewlineBefore( $index['value'] ); - } - } - - continue; - } - - $lastIndexLine = $indexLine; - $indexLine = $tokens[ $index['index'] ]['line']; - - if ( $indexLine === $tokens[ $stackPtr ]['line'] ) { - $error = 'The first index in a multi-value array must be on a new line'; - $fix = $phpcsFile->addFixableError( $error, $index['index'], 'FirstIndexNoNewline' ); - if ($fix === true) { - $phpcsFile->fixer->addNewlineBefore( $index['index'] ); - } - - continue; - } - - if ( $indexLine === $lastIndexLine ) { - $error = 'Each index in a multi-line array must be on a new line'; - $fix = $phpcsFile->addFixableError( $error, $index['index'], 'IndexNoNewline' ); - if ($fix === true) { - if ($tokens[($index['index'] - 1)]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->replaceToken( ( $index['index'] - 1 ), '' ); - } - - $phpcsFile->fixer->addNewlineBefore( $index['index'] ); - } - - continue; - } - - /* - if ( $tokens[ $index['index'] ]['column'] !== $indicesStart ) { - $expected = ( $indicesStart - 1 ); - $found = ( $tokens[ $index['index'] ]['column'] - 1 ); - $error = 'Array key not aligned correctly; expected %s spaces but found %s'; - $data = array( - $expected, - $found, - ); - - $fix = $phpcsFile->addFixableError( $error, $index['index'], 'KeyNotAligned', $data ); - if ($fix === true) { - if ($found === 0) { - $phpcsFile->fixer->addContent( ( $index['index'] - 1 ), str_repeat(' ', $expected ) ); - } else { - $phpcsFile->fixer->replaceToken( ( $index['index'] - 1 ), str_repeat(' ', $expected ) ); - } - } - - continue; - } - - if ( $tokens[ $index['arrow'] ]['column'] !== $arrowStart ) { - $expected = ( $arrowStart - ( strlen( $index['index_content'] ) + $tokens[ $index['index'] ]['column'] ) ); - $found = ( $tokens[ $index['arrow'] ]['column'] - ( strlen( $index['index_content'] ) + $tokens[ $index['index'] ]['column'] ) ); - $error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s'; - $data = array( - $expected, - $found, - ); - - $fix = $phpcsFile->addFixableError( $error, $index['arrow'], 'DoubleArrowNotAligned', $data ); - if ($fix === true) { - if ($found === 0) { - $phpcsFile->fixer->addContent( ( $index['arrow'] - 1 ), str_repeat(' ', $expected ) ); - } else { - $phpcsFile->fixer->replaceToken( ( $index['arrow'] - 1 ), str_repeat(' ', $expected ) ); - } - } - - continue; - } - - if ( $tokens[ $index['value'] ]['column'] !== $valueStart ) { - $expected = ( $valueStart - ( $tokens[ $index['arrow'] ]['length'] + $tokens[ $index['arrow'] ]['column'] ) ); - $found = ( $tokens[ $index['value'] ]['column'] - ( $tokens[ $index['arrow'] ]['length'] + $tokens[ $index['arrow'] ]['column'] ) ); - if ( $found < 0 ) { - $found = 'newline'; - } - - $error = 'Array value not aligned correctly; expected %s space(s) but found %s'; - $data = array( - $expected, - $found, - ); - - $fix = $phpcsFile->addFixableError( $error, $index['arrow'], 'ValueNotAligned', $data ); - if ($fix === true) { - if ($found === 'newline') { - $prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $index['value'] - 1 ), null, true ); - $phpcsFile->fixer->beginChangeset(); - for ( $i = ( $prev + 1 ); $i < $index['value']; $i++ ) { - $phpcsFile->fixer->replaceToken( $i, '' ); - } - - $phpcsFile->fixer->replaceToken( ( $index['value'] - 1 ), str_repeat(' ', $expected ) ); - $phpcsFile->fixer->endChangeset(); - } else if ($found === 0) { - $phpcsFile->fixer->addContent( ( $index['value'] - 1 ), str_repeat(' ', $expected ) ); - } else { - $phpcsFile->fixer->replaceToken( ( $index['value'] - 1 ), str_repeat(' ', $expected ) ); - } - } - } // end if - */ - - // Check each line ends in a comma. - /* - $valueLine = $tokens[ $index['value'] ]['line']; - $nextComma = false; - for ( $i = $index['value']; $i < $arrayEnd; $i++ ) { - // Skip bracketed statements, like function calls. - if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { - $i = $tokens[ $i ]['parenthesis_closer']; - $valueLine = $tokens[ $i ]['line']; - continue; - } - - if ($tokens[$i]['code'] === T_ARRAY) { - $i = $tokens[ $tokens[ $i ]['parenthesis_opener'] ]['parenthesis_closer']; - $valueLine = $tokens[ $i ]['line']; - continue; - } - - // Skip to the end of multi-line strings. - if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true) { - $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); - $i--; - $valueLine = $tokens[$i]['line']; - continue; - } - - if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { - $i = $tokens[ $i ]['bracket_closer']; - $valueLine = $tokens[ $i ]['line']; - continue; - } - - if ($tokens[$i]['code'] === T_CLOSURE) { - $i = $tokens[ $i ]['scope_closer']; - $valueLine = $tokens[ $i ]['line']; - continue; - } - - if ($tokens[$i]['code'] === T_COMMA) { - $nextComma = $i; - break; - } - }//end for - - if ($nextComma === false || ($tokens[$nextComma]['line'] !== $valueLine)) { - $error = 'Each line in an array declaration must end in a comma'; - $fix = $phpcsFile->addFixableError( $error, $index['value'], 'NoComma' ); - - if ($fix === true) { - // Find the end of the line and put a comma there. - for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { - if ( $tokens[ $i ]['line'] > $valueLine ) { - break; - } - } - - $phpcsFile->fixer->addContentBefore( ( $i - 1 ), ',' ); - } - } - - // Check that there is no space before the comma. - if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { - $content = $tokens[ ( $nextComma - 2 ) ]['content']; - $spaceLength = $tokens[ ( $nextComma - 1 ) ]['length']; - $error = 'Expected 0 spaces between "%s" and comma; %s found'; - $data = array( - $content, - $spaceLength, - ); - - $fix = $phpcsFile->addFixableError( $error, $nextComma, 'SpaceBeforeComma', $data ); - if ($fix === true) { - $phpcsFile->fixer->replaceToken( ( $nextComma - 1 ), '' ); - } - } - */ - }//end foreach - - }//end processMultiLineArray() + /** + * Don't use. + * + * @deprecated 0.13.0 + * + * @param PHP_CodeSniffer_File $phpcsFile A PHP_CodeSniffer file. + * @param int $stackPtr The position of the token. + * + * @return void + */ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {} } // End class. diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php index b42646f17b..3d7c985cc5 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php @@ -10,9 +10,6 @@ /** * Enforces WordPress array spacing format. * - * WordPress specific checks which are not covered by the `WordPress.Arrays.ArrayDeclaration`/ - * `Squiz.Arrays.ArrayDeclaration` sniff. - * * - Check for no space between array keyword and array opener. * - Check for no space between the parentheses of an empty array. * - Checks for one space after the array opener / before the array closer in single-line arrays. @@ -30,6 +27,9 @@ * - Added sniffing & fixing for associative arrays. * @since 0.12.0 Decoupled this sniff from the upstream sniff completely. * This sniff now extends the `WordPress_Sniff` instead. + * @since 0.13.0 Added the last remaining checks from the `ArrayDeclaration` sniff + * which were not covered elsewhere. The `ArrayDeclaration` sniff has + * now been deprecated. */ class WordPress_Sniffs_Arrays_ArrayDeclarationSpacingSniff extends WordPress_Sniff { diff --git a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php index 329b9a9856..29fb9f6001 100644 --- a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php @@ -141,7 +141,7 @@ public function process_token( $stackPtr ) { * Otherwise, only report the error, don't try and fix it (yet). * * It will get corrected in a future loop of the fixer once the closer - * has been moved to its own line by the `ArrayDeclaration` sniff. + * has been moved to its own line by the `ArrayDeclarationSpacing` sniff. */ $this->phpcsFile->addError( $error, @@ -184,7 +184,7 @@ public function process_token( $stackPtr ) { } // Bow out from reporting and fixing mixed multi-line/single-line arrays. - // That is handled by the ArrayDeclarationSniff. + // That is handled by the ArrayDeclarationSpacingSniff. if ( $this->tokens[ $first_content ]['line'] === $this->tokens[ $end_of_previous_item ]['line'] || ( 1 !== $this->tokens[ $first_content ]['column'] && T_WHITESPACE !== $this->tokens[ ( $first_content - 1 ) ]['code'] ) diff --git a/WordPress/Tests/Arrays/ArrayDeclarationUnitTest.txt b/WordPress/Tests/Arrays/ArrayDeclarationUnitTest.txt deleted file mode 100644 index 4b8ea34256..0000000000 --- a/WordPress/Tests/Arrays/ArrayDeclarationUnitTest.txt +++ /dev/null @@ -1,13 +0,0 @@ -/** - * THIS SNIFF IS INTENTIONALLY NOT TESTED HERE. - * - * The WordPress.Arrays.ArrayDeclaration sniff is a duplicate of the upstream - * Squiz.Arrays.ArrayDeclaration sniff with some checks commented out. - * - * As such all checks contained in the sniff are already tested upstream and - * the tests should not be duplicated here. - * - * @package WPCS\WordPressCodingStandards - * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards - * @license https://opensource.org/licenses/MIT MIT - */ diff --git a/bin/phpcs.xml b/bin/phpcs.xml index 6e933911a9..03f52da5c6 100644 --- a/bin/phpcs.xml +++ b/bin/phpcs.xml @@ -2,11 +2,6 @@ The Coding standard for the WordPress Coding Standards itself. - - /WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php - From e7d43983ce9b0336b8450f34d31a26b58bfa1a0a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 09:46:01 +0200 Subject: [PATCH 05/37] Remove ArrayDeclaration sniff: test both long as well as short arrays for spacing Creates a second copy of the `ArrayDeclarationSpacingUnitTest` file. The first (original) test case file now tests long arrays `array()`. The second (copy) test case file now tests short arrays `[]`. The test cases in both files are the same, except for the fact that the short array test case file does not contain the tests for _space between array keyword and opener_ as that's not applicable. --- ... => ArrayDeclarationSpacingUnitTest.1.inc} | 9 +- ...rayDeclarationSpacingUnitTest.1.inc.fixed} | 19 ++- .../ArrayDeclarationSpacingUnitTest.2.inc | 84 +++++++++++ ...rrayDeclarationSpacingUnitTest.2.inc.fixed | 137 ++++++++++++++++++ .../ArrayDeclarationSpacingUnitTest.php | 92 ++++++++---- 5 files changed, 302 insertions(+), 39 deletions(-) rename WordPress/Tests/Arrays/{ArrayDeclarationSpacingUnitTest.inc => ArrayDeclarationSpacingUnitTest.1.inc} (86%) rename WordPress/Tests/Arrays/{ArrayDeclarationSpacingUnitTest.inc.fixed => ArrayDeclarationSpacingUnitTest.1.inc.fixed} (96%) create mode 100644 WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc create mode 100644 WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc similarity index 86% rename from WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc rename to WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc index 6acbfc006d..5727abbafa 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc @@ -1,4 +1,7 @@ 'value' ); // Bad, each value of an associative array should start on a new line. // Test for fixing nested associative arrays. -$bad = array( array( 'key1' => 'value1', 'key2' => ['sub1' => 1, 'sub2' => 2] ), $key3 => 'value3', [ 'value4', 10 => 'value5', ] ); // Bad. +$bad = array( array( 'key1' => 'value1', 'key2' => array('sub1' => 1, 'sub2' => 2) ), $key3 => 'value3', array( 'value4', 10 => 'value5', ) ); // Bad. // Test for fixing mixed single & multi-line nested associative arrays. $bad = array( array( 'key1' => 'value1', array('sub1' => 1,'sub2' => 2,)), $key3 => 'value3', - [ 'value4', 10 => 'value5' ] + array( 'value4', 10 => 'value5' ) ); // Bad. // Test for fixing associative arrays with multiple values & line indented with whitespace. @@ -26,7 +29,7 @@ $bad = array( $bad = array( 'key1' => 'value1', /* comment */ 'key2' => 'value2' ); // Bad. // Test for fixing non-associative array with a nested associative array which *will* be fixed. -$bad = array( 'value1', 'value2', [ 'sub1' => 1, 'sub2' => 2 ], 'value4' ); // Bad. +$bad = array( 'value1', 'value2', array( 'sub1' => 1, 'sub2' => 2 ), 'value4' ); // Bad. /* * Test spacing between array keyword and open parenthesis. diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc.fixed similarity index 96% rename from WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed rename to WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc.fixed index d48bcf3104..b3e18bbdf4 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc.fixed @@ -1,4 +1,7 @@ 'value1', -'key2' => [ +'key2' => array( 'sub1' => 1, 'sub2' => 2 -] +) ), $key3 => 'value3', -[ +array( 'value4', 10 => 'value5', -] +) ); // Bad. // Test for fixing mixed single & multi-line nested associative arrays. @@ -37,10 +40,10 @@ array( ) ), $key3 => 'value3', - [ + array( 'value4', 10 => 'value5' -] +) ); // Bad. // Test for fixing associative arrays with multiple values & line indented with whitespace. @@ -62,10 +65,10 @@ $bad = array( $bad = array( 'value1', 'value2', -[ +array( 'sub1' => 1, 'sub2' => 2 -], +), 'value4' ); // Bad. diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc new file mode 100644 index 0000000000..53802a5232 --- /dev/null +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc @@ -0,0 +1,84 @@ + 'value' ]; // Bad, each value of an associative array should start on a new line. + +// Test for fixing nested associative arrays. +$bad = [ [ 'key1' => 'value1', 'key2' => ['sub1' => 1, 'sub2' => 2] ], $key3 => 'value3', [ 'value4', 10 => 'value5', ] ]; // Bad. + +// Test for fixing mixed single & multi-line nested associative arrays. +$bad = [ + [ 'key1' => 'value1', ['sub1' => 1,'sub2' => 2,]], + $key3 => 'value3', + [ 'value4', 10 => 'value5' ] +]; // Bad. + +// Test for fixing associative arrays with multiple values & line indented with whitespace. + $bad = [ 'key1' => 'value1', 'key2' => 'value2', $key3 => 'value3', 'value4', 10 => 'value5' ]; // Bad. + +// Test for fixing associative arrays with comments between values. +$bad = [ 'key1' => 'value1', /* comment */ 'key2' => 'value2' ]; // Bad. + +// Test for fixing non-associative array with a nested associative array which *will* be fixed. +$bad = [ 'value1', 'value2', [ 'sub1' => 1, 'sub2' => 2 ], 'value4' ]; // Bad. + +/* + * Tests for empty array with space between parentheses. + */ +// OK. +$a = []; +$value = [ /* comment */ ]; +$x = [ + // comment + ]; + +// Bad. +$value = [ ]; +$value = [ ]; +$x = [ + ]; + +/* + * Tests for multi-line arrays - closing brace on new line + array items each on new line. + */ +// OK. +$value = [ + 1, + 2, /* Comment. */ + 3, +]; + +$value = [ + 1 => $one, + 2 => $two, // Comment. + 3 => $three, // Comment. +]; + +// Bad. +$value = [1, + 2 , 3 , +]; + +$value = [1 => $one, + 2 => $two , /* Comment. */ 3 => $three , ]; + +$value = [ + '1'=> TRUE, FALSE, '3' => 'aaa',]; + +$x = ['name' => 'test', + ]; + +$foo = [1 +, 2]; + +$fields = [ + 'value' => 'type']; diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed new file mode 100644 index 0000000000..8ed5a40098 --- /dev/null +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed @@ -0,0 +1,137 @@ + 'value' +]; // Bad, each value of an associative array should start on a new line. + +// Test for fixing nested associative arrays. +$bad = [ +[ +'key1' => 'value1', +'key2' => [ +'sub1' => 1, +'sub2' => 2 +] +], +$key3 => 'value3', +[ +'value4', +10 => 'value5', +] +]; // Bad. + +// Test for fixing mixed single & multi-line nested associative arrays. +$bad = [ + [ +'key1' => 'value1', +[ +'sub1' => 1, +'sub2' => 2, +] +], + $key3 => 'value3', + [ +'value4', +10 => 'value5' +] +]; // Bad. + +// Test for fixing associative arrays with multiple values & line indented with whitespace. + $bad = [ +'key1' => 'value1', +'key2' => 'value2', +$key3 => 'value3', +'value4', +10 => 'value5' +]; // Bad. + +// Test for fixing associative arrays with comments between values. +$bad = [ +'key1' => 'value1', /* comment */ +'key2' => 'value2' +]; // Bad. + +// Test for fixing non-associative array with a nested associative array which *will* be fixed. +$bad = [ +'value1', +'value2', +[ +'sub1' => 1, +'sub2' => 2 +], +'value4' +]; // Bad. + +/* + * Tests for empty array with space between parentheses. + */ +// OK. +$a = []; +$value = [ /* comment */ ]; +$x = [ + // comment + ]; + +// Bad. +$value = []; +$value = []; +$x = []; + +/* + * Tests for multi-line arrays - closing brace on new line + array items each on new line. + */ +// OK. +$value = [ + 1, + 2, /* Comment. */ + 3, +]; + +$value = [ + 1 => $one, + 2 => $two, // Comment. + 3 => $three, // Comment. +]; + +// Bad. +$value = [ +1, + 2 , +3 , +]; + +$value = [ +1 => $one, + 2 => $two , +/* Comment. */ 3 => $three , +]; + +$value = [ + '1'=> TRUE, +FALSE, +'3' => 'aaa', +]; + +$x = [ +'name' => 'test', + ]; + +$foo = [ +1 +, +2 +]; + +$fields = [ + 'value' => 'type' +]; diff --git a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php index 013297b0d3..5537e46ed5 100644 --- a/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php +++ b/WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php @@ -18,36 +18,72 @@ class WordPress_Tests_Arrays_ArrayDeclarationSpacingUnitTest extends AbstractSni /** * Returns the lines where errors should occur. * + * @param string $testFile The name of the file being tested. + * * @return array => */ - public function getErrorList() { - return array( - 5 => 2, - 8 => 2, - 10 => 1, - 13 => 4, - 17 => 2, - 19 => 1, - 23 => 1, - 26 => 1, - 29 => 1, - 35 => 1, - 36 => 1, - 42 => 1, - 46 => 1, - 59 => 2, - 60 => 1, - 61 => 1, - 81 => 1, - 82 => 1, - 85 => 1, - 86 => 2, - 89 => 3, - 91 => 1, - 94 => 1, - 95 => 2, - 98 => 1, - ); + public function getErrorList( $testFile = '' ) { + + switch ( $testFile ) { + // Long arrays. + case 'ArrayDeclarationSpacingUnitTest.1.inc': + return array( + 8 => 2, + 11 => 2, + 13 => 1, + 16 => 4, + 20 => 2, + 22 => 1, + 26 => 1, + 29 => 1, + 32 => 1, + 38 => 1, + 39 => 1, + 45 => 1, + 49 => 1, + 62 => 2, + 63 => 1, + 64 => 1, + 84 => 1, + 85 => 1, + 88 => 1, + 89 => 2, + 92 => 3, + 94 => 1, + 97 => 1, + 98 => 2, + 101 => 1, + ); + + // Short arrays. + case 'ArrayDeclarationSpacingUnitTest.2.inc': + return array( + 8 => 2, + 11 => 2, + 13 => 1, + 16 => 4, + 20 => 2, + 22 => 1, + 26 => 1, + 29 => 1, + 32 => 1, + 45 => 1, + 46 => 1, + 47 => 1, + 67 => 1, + 68 => 1, + 71 => 1, + 72 => 2, + 75 => 3, + 77 => 1, + 80 => 1, + 81 => 2, + 84 => 1, + ); + + default: + return array(); + } } /** From b14688d7481dbfd6883dee14f0d8c49e0e3d72eb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 21 Jul 2017 00:12:09 +0200 Subject: [PATCH 06/37] PHPCS 3.x prep: work round a function which will no longer exist The `removeTokenListener()` method was used to prevent the sniffs using these abstracts from being called for further files, however, this method will no longer exist in PHPCS 3.x with no alternative. The `AbstractFunctionRestrictionSniff` also uses a `getGroups()` method, but calls it earlier and prevents the child sniffs from being run without groups by just not registering any token listeners if no groups are found. This same methodology has now been implemented in the two abstracts which used the `removeTokenListener()` method, effectively making these sniffs more efficient and preventing the removal of the method from becoming a problem. --- ...stractArrayAssignmentRestrictionsSniff.php | 47 ++++++++++++++---- .../AbstractVariableRestrictionsSniff.php | 48 +++++++++++++++---- .../ArrayAssignmentRestrictionsSniff.php | 2 +- .../Variables/VariableRestrictionsSniff.php | 2 +- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/WordPress/AbstractArrayAssignmentRestrictionsSniff.php b/WordPress/AbstractArrayAssignmentRestrictionsSniff.php index 8900522120..c5cf5fd86b 100644 --- a/WordPress/AbstractArrayAssignmentRestrictionsSniff.php +++ b/WordPress/AbstractArrayAssignmentRestrictionsSniff.php @@ -47,12 +47,26 @@ abstract class WordPress_AbstractArrayAssignmentRestrictionsSniff extends WordPr */ protected $excluded_groups = array(); + /** + * Cache for the group information. + * + * @since 0.13.0 + * + * @var array + */ + protected $groups_cache = array(); + /** * Returns an array of tokens this test wants to listen for. * * @return array */ public function register() { + // Retrieve the groups only once and don't set up a listener if there are no groups. + if ( false === $this->setup_groups() ) { + return array(); + } + return array( T_DOUBLE_ARROW, T_CLOSE_SQUARE_BRACKET, @@ -80,6 +94,28 @@ public function register() { */ abstract public function getGroups(); + /** + * Cache the groups. + * + * @since 0.13.0 + * + * @return bool True if the groups were setup. False if not. + */ + protected function setup_groups() { + $this->groups_cache = $this->getGroups(); + + if ( empty( $this->groups_cache ) && empty( self::$groups ) ) { + return false; + } + + // Allow for adding extra unit tests. + if ( ! empty( self::$groups ) ) { + $this->groups_cache = array_merge( $this->groups_cache, self::$groups ); + } + + return true; + } + /** * Processes this test, when one of its tokens is encountered. * @@ -89,15 +125,8 @@ abstract public function getGroups(); */ public function process_token( $stackPtr ) { - $groups = $this->getGroups(); - - if ( empty( $groups ) ) { - $this->phpcsFile->removeTokenListener( $this, $this->register() ); - return; - } - $this->excluded_groups = $this->merge_custom_array( $this->exclude ); - if ( array_diff_key( $groups, $this->excluded_groups ) === array() ) { + if ( array_diff_key( $this->groups_cache, $this->excluded_groups ) === array() ) { // All groups have been excluded. // Don't remove the listener as the exclude property can be changed inline. return; @@ -149,7 +178,7 @@ public function process_token( $stackPtr ) { return; } - foreach ( $groups as $groupName => $group ) { + foreach ( $this->groups_cache as $groupName => $group ) { if ( isset( $this->excluded_groups[ $groupName ] ) ) { continue; diff --git a/WordPress/AbstractVariableRestrictionsSniff.php b/WordPress/AbstractVariableRestrictionsSniff.php index b1bb20e01f..324f4853c9 100644 --- a/WordPress/AbstractVariableRestrictionsSniff.php +++ b/WordPress/AbstractVariableRestrictionsSniff.php @@ -48,12 +48,26 @@ abstract class WordPress_AbstractVariableRestrictionsSniff extends WordPress_Sni */ protected $excluded_groups = array(); + /** + * Cache for the group information. + * + * @since 0.13.0 + * + * @var array + */ + protected $groups_cache = array(); + /** * Returns an array of tokens this test wants to listen for. * * @return array */ public function register() { + // Retrieve the groups only once and don't set up a listener if there are no groups. + if ( false === $this->setup_groups() ) { + return array(); + } + return array( T_VARIABLE, T_OBJECT_OPERATOR, @@ -84,6 +98,28 @@ public function register() { */ abstract public function getGroups(); + /** + * Cache the groups. + * + * @since 0.13.0 + * + * @return bool True if the groups were setup. False if not. + */ + protected function setup_groups() { + $this->groups_cache = $this->getGroups(); + + if ( empty( $this->groups_cache ) && empty( self::$groups ) ) { + return false; + } + + // Allow for adding extra unit tests. + if ( ! empty( self::$groups ) ) { + $this->groups_cache = array_merge( $this->groups_cache, self::$groups ); + } + + return true; + } + /** * Processes this test, when one of its tokens is encountered. * @@ -94,16 +130,10 @@ abstract public function getGroups(); */ public function process_token( $stackPtr ) { - $token = $this->tokens[ $stackPtr ]; - $groups = $this->getGroups(); - - if ( empty( $groups ) ) { - $this->phpcsFile->removeTokenListener( $this, $this->register() ); - return; - } + $token = $this->tokens[ $stackPtr ]; $this->excluded_groups = $this->merge_custom_array( $this->exclude ); - if ( array_diff_key( $groups, $this->excluded_groups ) === array() ) { + if ( array_diff_key( $this->groups_cache, $this->excluded_groups ) === array() ) { // All groups have been excluded. // Don't remove the listener as the exclude property can be changed inline. return; @@ -118,7 +148,7 @@ public function process_token( $stackPtr ) { } } - foreach ( $groups as $groupName => $group ) { + foreach ( $this->groups_cache as $groupName => $group ) { if ( isset( $this->excluded_groups[ $groupName ] ) ) { continue; diff --git a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php index a122a7fa51..5fbe91df93 100644 --- a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php @@ -38,7 +38,7 @@ class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff extends WordPress * @return array */ public function getGroups() { - return parent::$groups; + return array(); } /** diff --git a/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php b/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php index e8d16d88ce..f28a07af45 100644 --- a/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php +++ b/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php @@ -39,7 +39,7 @@ class WordPress_Sniffs_Variables_VariableRestrictionsSniff extends WordPress_Abs * @return array */ public function getGroups() { - return parent::$groups; + return array(); } } // End class. From be850cb35f2dd931c540e6afebf5e43ce7e302f0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 19:50:04 +0200 Subject: [PATCH 07/37] PHPCS 3.x compat: Namespace all sniffs which extend from one of the WPCS base sniffs Includes adjustment of FQCN references in `@see` and `@uses` documentation. --- .../AbstractArrayAssignmentRestrictionsSniff.php | 6 +++++- WordPress/AbstractClassRestrictionsSniff.php | 6 +++++- WordPress/AbstractFunctionParameterSniff.php | 6 +++++- WordPress/AbstractFunctionRestrictionsSniff.php | 6 +++++- WordPress/AbstractVariableRestrictionsSniff.php | 6 +++++- WordPress/Sniff.php | 4 +++- .../Arrays/ArrayAssignmentRestrictionsSniff.php | 10 ++++++++-- WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php | 6 +++++- .../Sniffs/Arrays/ArrayDeclarationSpacingSniff.php | 7 ++++++- WordPress/Sniffs/Arrays/ArrayIndentationSniff.php | 7 ++++++- .../Arrays/ArrayKeySpacingRestrictionsSniff.php | 7 ++++++- WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php | 7 ++++++- WordPress/Sniffs/CSRF/NonceVerificationSniff.php | 7 ++++++- WordPress/Sniffs/Classes/ClassInstantiationSniff.php | 10 +++++++--- WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php | 7 ++++++- WordPress/Sniffs/DB/RestrictedClassesSniff.php | 7 ++++++- WordPress/Sniffs/DB/RestrictedFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/Files/FileNameSniff.php | 9 +++++++-- WordPress/Sniffs/Functions/DontExtractSniff.php | 7 ++++++- .../Functions/FunctionCallSignatureNoParamsSniff.php | 7 ++++++- .../Sniffs/Functions/FunctionRestrictionsSniff.php | 10 ++++++++-- .../NamingConventions/PrefixAllGlobalsSniff.php | 9 +++++++-- .../NamingConventions/ValidFunctionNameSniff.php | 1 + .../Sniffs/NamingConventions/ValidHookNameSniff.php | 7 ++++++- .../NamingConventions/ValidVariableNameSniff.php | 8 +++++--- WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php | 6 +++++- WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/PHP/POSIXFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/PHP/StrictComparisonsSniff.php | 7 ++++++- WordPress/Sniffs/PHP/StrictInArraySniff.php | 7 ++++++- WordPress/Sniffs/PHP/YodaConditionsSniff.php | 7 ++++++- WordPress/Sniffs/VIP/AdminBarRemovalSniff.php | 7 ++++++- WordPress/Sniffs/VIP/CronIntervalSniff.php | 7 ++++++- WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php | 7 ++++++- .../Sniffs/VIP/FileSystemWritesDisallowSniff.php | 7 ++++++- WordPress/Sniffs/VIP/OrderByRandSniff.php | 7 ++++++- WordPress/Sniffs/VIP/PluginMenuSlugSniff.php | 7 ++++++- WordPress/Sniffs/VIP/PostsPerPageSniff.php | 7 ++++++- WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/VIP/RestrictedVariablesSniff.php | 7 ++++++- WordPress/Sniffs/VIP/SessionFunctionsUsageSniff.php | 7 ++++++- WordPress/Sniffs/VIP/SessionVariableUsageSniff.php | 7 ++++++- WordPress/Sniffs/VIP/SlowDBQuerySniff.php | 7 ++++++- WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php | 9 +++++++-- WordPress/Sniffs/VIP/TimezoneChangeSniff.php | 7 ++++++- WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php | 11 ++++++++--- WordPress/Sniffs/Variables/GlobalVariablesSniff.php | 11 +++++++---- .../Sniffs/Variables/VariableRestrictionsSniff.php | 10 ++++++++-- WordPress/Sniffs/WP/AlternativeFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/WP/CapitalPDangitSniff.php | 7 ++++++- WordPress/Sniffs/WP/DeprecatedClassesSniff.php | 7 ++++++- WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/WP/DeprecatedParametersSniff.php | 7 ++++++- WordPress/Sniffs/WP/DiscouragedFunctionsSniff.php | 7 ++++++- WordPress/Sniffs/WP/EnqueuedResourcesSniff.php | 7 ++++++- WordPress/Sniffs/WP/I18nSniff.php | 7 ++++++- WordPress/Sniffs/WP/PreparedSQLSniff.php | 7 ++++++- .../Sniffs/WhiteSpace/CastStructureSpacingSniff.php | 7 ++++++- .../WhiteSpace/ControlStructureSpacingSniff.php | 7 ++++++- .../Sniffs/WhiteSpace/DisallowInlineTabsSniff.php | 7 ++++++- WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php | 1 + WordPress/Sniffs/XSS/EscapeOutputSniff.php | 9 +++++++-- 63 files changed, 371 insertions(+), 77 deletions(-) diff --git a/WordPress/AbstractArrayAssignmentRestrictionsSniff.php b/WordPress/AbstractArrayAssignmentRestrictionsSniff.php index c5cf5fd86b..b5558e14ed 100644 --- a/WordPress/AbstractArrayAssignmentRestrictionsSniff.php +++ b/WordPress/AbstractArrayAssignmentRestrictionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + +use WordPress\Sniff; + /** * Restricts array assignment of certain keys. * @@ -18,7 +22,7 @@ * `WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff` to * `WordPress_AbstractArrayAssignmentRestrictionsSniff`. */ -abstract class WordPress_AbstractArrayAssignmentRestrictionsSniff extends WordPress_Sniff { +abstract class AbstractArrayAssignmentRestrictionsSniff extends Sniff { /** * Exclude groups. diff --git a/WordPress/AbstractClassRestrictionsSniff.php b/WordPress/AbstractClassRestrictionsSniff.php index c83dc7e662..b37cd490fe 100644 --- a/WordPress/AbstractClassRestrictionsSniff.php +++ b/WordPress/AbstractClassRestrictionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restricts usage of some classes. * @@ -14,7 +18,7 @@ * * @since 0.10.0 */ -abstract class WordPress_AbstractClassRestrictionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +abstract class AbstractClassRestrictionsSniff extends AbstractFunctionRestrictionsSniff { /** * Regex pattern with placeholder for the class names. diff --git a/WordPress/AbstractFunctionParameterSniff.php b/WordPress/AbstractFunctionParameterSniff.php index f9e7fbc6d5..b201354aa1 100644 --- a/WordPress/AbstractFunctionParameterSniff.php +++ b/WordPress/AbstractFunctionParameterSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Advises about parameters used in function calls. * @@ -14,7 +18,7 @@ * * @since 0.11.0 */ -abstract class WordPress_AbstractFunctionParameterSniff extends WordPress_AbstractFunctionRestrictionsSniff { +abstract class AbstractFunctionParameterSniff extends AbstractFunctionRestrictionsSniff { /** * The group name for this group of functions. diff --git a/WordPress/AbstractFunctionRestrictionsSniff.php b/WordPress/AbstractFunctionRestrictionsSniff.php index 81fe126abd..a607d21edf 100644 --- a/WordPress/AbstractFunctionRestrictionsSniff.php +++ b/WordPress/AbstractFunctionRestrictionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + +use WordPress\Sniff; + /** * Restricts usage of some functions. * @@ -19,7 +23,7 @@ * `WordPress_AbstractFunctionRestrictionsSniff`. * @since 0.11.0 Extends the WordPress_Sniff class. */ -abstract class WordPress_AbstractFunctionRestrictionsSniff extends WordPress_Sniff { +abstract class AbstractFunctionRestrictionsSniff extends Sniff { /** * Exclude groups. diff --git a/WordPress/AbstractVariableRestrictionsSniff.php b/WordPress/AbstractVariableRestrictionsSniff.php index 324f4853c9..5ed71212ee 100644 --- a/WordPress/AbstractVariableRestrictionsSniff.php +++ b/WordPress/AbstractVariableRestrictionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + +use WordPress\Sniff; + /** * Restricts usage of some variables. * @@ -19,7 +23,7 @@ * `WordPress_AbstractVariableRestrictionsSniff`. * @since 0.11.0 Extends the WordPress_Sniff class. */ -abstract class WordPress_AbstractVariableRestrictionsSniff extends WordPress_Sniff { +abstract class AbstractVariableRestrictionsSniff extends Sniff { /** * Exclude groups. diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index e3746dc5e7..ec47c6dd16 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -7,6 +7,8 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress; + /** * Represents a PHP_CodeSniffer sniff for sniffing WordPress coding standards. * @@ -26,7 +28,7 @@ * In the rare few cases where the array values *do* have meaning, this * is documented in the property documentation.}} */ -abstract class WordPress_Sniff implements PHP_CodeSniffer_Sniff { +abstract class Sniff implements PHP_CodeSniffer_Sniff { /** * List of the functions which verify nonces. diff --git a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php index 5fbe91df93..f48d1e2cb2 100644 --- a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php @@ -7,21 +7,27 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + +use WordPress\AbstractArrayAssignmentRestrictionsSniff; + /** * Restricts array assignment of certain keys. * * @package WPCS\WordPressCodingStandards * * @since 0.3.0 + * @since 0.13.0 Class name changed: this class is now namespaced. + * * @deprecated 0.10.0 The functionality which used to be contained in this class has been moved to * the WordPress_AbstractArrayAssignmentRestrictionsSniff class. * This class is left here to prevent backward-compatibility breaks for * custom sniffs extending the old class and references to this * sniff from custom phpcs.xml files. * This file is also still used to unit test the abstract class. - * @see WordPress_AbstractArrayAssignmentRestrictionsSniff + * @see \WordPress\AbstractArrayAssignmentRestrictionsSniff */ -class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class ArrayAssignmentRestrictionsSniff extends AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php index b3fbb4560a..9f3e593d74 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -7,6 +7,8 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + /** * Enforces WordPress array format, based upon Squiz code. * @@ -19,6 +21,8 @@ * @since 0.11.0 The additional single-line array checks have been moved to their own * sniff WordPress.Arrays.ArrayDeclarationSpacing. * This class now only contains a slimmed down version of the upstream sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. + * * @deprecated 0.13.0 This sniff has now been deprecated. Most checks which were previously * contained herein had recently been excluded in favour of dedicated * sniffs with higher precision. The last remaining checks which were not @@ -27,7 +31,7 @@ * This class is left here to prevent breaking custom rulesets which refer * to this sniff. */ -class WordPress_Sniffs_Arrays_ArrayDeclarationSniff { +class ArrayDeclarationSniff { /** * Don't use. diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php index 3d7c985cc5..36c075e18d 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + +use WordPress\Sniff; + /** * Enforces WordPress array spacing format. * @@ -30,8 +34,9 @@ * @since 0.13.0 Added the last remaining checks from the `ArrayDeclaration` sniff * which were not covered elsewhere. The `ArrayDeclaration` sniff has * now been deprecated. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Arrays_ArrayDeclarationSpacingSniff extends WordPress_Sniff { +class ArrayDeclarationSpacingSniff extends Sniff { /** * Token this sniff targets. diff --git a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php index 29fb9f6001..9a67948759 100644 --- a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + +use WordPress\Sniff; + /** * Enforces WordPress array indentation for multi-line arrays. * @@ -15,11 +19,12 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. * * {@internal This sniff should eventually be pulled upstream as part of a solution * for https://github.com/squizlabs/PHP_CodeSniffer/issues/582 }} */ -class WordPress_Sniffs_Arrays_ArrayIndentationSniff extends WordPress_Sniff { +class ArrayIndentationSniff extends Sniff { /** * Should tabs be used for indenting? diff --git a/WordPress/Sniffs/Arrays/ArrayKeySpacingRestrictionsSniff.php b/WordPress/Sniffs/Arrays/ArrayKeySpacingRestrictionsSniff.php index 851bf91e1f..73e38aa686 100644 --- a/WordPress/Sniffs/Arrays/ArrayKeySpacingRestrictionsSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayKeySpacingRestrictionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + +use WordPress\Sniff; + /** * Check for proper spacing in array key references. * @@ -17,8 +21,9 @@ * @since 0.3.0 * @since 0.7.0 This sniff now has the ability to fix a number of the issues it flags. * @since 0.12.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Arrays_ArrayKeySpacingRestrictionsSniff extends WordPress_Sniff { +class ArrayKeySpacingRestrictionsSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php b/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php index 04b5d5661e..f2ef82522d 100644 --- a/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php +++ b/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Arrays; + +use WordPress\Sniff; + /** * Enforces a comma after each array item and the spacing around it. * @@ -22,8 +26,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Arrays_CommaAfterArrayItemSniff extends WordPress_Sniff { +class CommaAfterArrayItemSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/CSRF/NonceVerificationSniff.php b/WordPress/Sniffs/CSRF/NonceVerificationSniff.php index 215b67ea6d..d9828410df 100644 --- a/WordPress/Sniffs/CSRF/NonceVerificationSniff.php +++ b/WordPress/Sniffs/CSRF/NonceVerificationSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\CSRF; + +use WordPress\Sniff; + /** * Checks that nonce verification accompanies form processing. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.5.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_CSRF_NonceVerificationSniff extends WordPress_Sniff { +class NonceVerificationSniff extends Sniff { /** * Superglobals to notify about when not accompanied by an nonce check. diff --git a/WordPress/Sniffs/Classes/ClassInstantiationSniff.php b/WordPress/Sniffs/Classes/ClassInstantiationSniff.php index e2a646f598..2465a66c83 100644 --- a/WordPress/Sniffs/Classes/ClassInstantiationSniff.php +++ b/WordPress/Sniffs/Classes/ClassInstantiationSniff.php @@ -7,10 +7,13 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Classes; + +use WordPress\Sniff; + /** - * WordPress_Sniffs_Classes_ClassInstantiationSniff. - * * Verifies object instantiation statements. + * * - Demand the use of parenthesis. * - Demand no space between the class name and the parenthesis. * - Forbid assigning new by reference. @@ -21,8 +24,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Classes_ClassInstantiationSniff extends WordPress_Sniff { +class ClassInstantiationSniff extends Sniff { /** * A list of tokenizers this sniff supports. diff --git a/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php b/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php index 07ad7d4732..3778fd0372 100644 --- a/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php +++ b/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\CodeAnalysis; + +use WordPress\Sniff; + /** * Checks against empty statements. * @@ -22,8 +26,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_CodeAnalysis_EmptyStatementSniff extends WordPress_Sniff { +class EmptyStatementSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/DB/RestrictedClassesSniff.php b/WordPress/Sniffs/DB/RestrictedClassesSniff.php index 37ca463e3a..1083c86c96 100644 --- a/WordPress/Sniffs/DB/RestrictedClassesSniff.php +++ b/WordPress/Sniffs/DB/RestrictedClassesSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\DB; + +use WordPress\AbstractClassRestrictionsSniff; + /** * Verifies that no database related PHP classes are used. * @@ -20,8 +24,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.10.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_DB_RestrictedClassesSniff extends WordPress_AbstractClassRestrictionsSniff { +class RestrictedClassesSniff extends AbstractClassRestrictionsSniff { /** * Groups of classes to restrict. diff --git a/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php b/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php index 56a84f788d..557d5771e5 100644 --- a/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php +++ b/WordPress/Sniffs/DB/RestrictedFunctionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\DB; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Verifies that no database related PHP functions are used. * @@ -20,8 +24,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.10.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_DB_RestrictedFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class RestrictedFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/Files/FileNameSniff.php b/WordPress/Sniffs/Files/FileNameSniff.php index cec2fa4f50..624edc49d9 100644 --- a/WordPress/Sniffs/Files/FileNameSniff.php +++ b/WordPress/Sniffs/Files/FileNameSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Files; + +use WordPress\Sniff; + /** * Ensures filenames do not contain underscores. * @@ -22,10 +26,11 @@ * - This sniff will now allow for underscores in file names for certain theme * specific exceptions if the `$is_theme` property is set to `true`. * @since 0.12.0 - Now extends the `WordPress_Sniff` class. + * @since 0.13.0 Class name changed: this class is now namespaced. * - * @uses WordPress_Sniff::$custom_test_class_whitelist + * @uses \WordPress\Sniff::$custom_test_class_whitelist */ -class WordPress_Sniffs_Files_FileNameSniff extends WordPress_Sniff { +class FileNameSniff extends Sniff { /** * Regex for the theme specific exceptions. diff --git a/WordPress/Sniffs/Functions/DontExtractSniff.php b/WordPress/Sniffs/Functions/DontExtractSniff.php index 3cf949916b..026e6cc52e 100644 --- a/WordPress/Sniffs/Functions/DontExtractSniff.php +++ b/WordPress/Sniffs/Functions/DontExtractSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Functions; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restricts the usage of extract(). * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.10.0 Previously this check was contained within WordPress_Sniffs_VIP_RestrictedFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Functions_DontExtractSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class DontExtractSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php b/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php index 1d9bcafcd1..0f94a1f144 100644 --- a/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php +++ b/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Functions; + +use WordPress\Sniff; + /** * Enforces no whitespace between the parenthesis of a function call without parameters. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_Functions_FunctionCallSignatureNoParamsSniff extends WordPress_Sniff { +class FunctionCallSignatureNoParamsSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/Functions/FunctionRestrictionsSniff.php b/WordPress/Sniffs/Functions/FunctionRestrictionsSniff.php index 483e6cb03f..af829d27d0 100644 --- a/WordPress/Sniffs/Functions/FunctionRestrictionsSniff.php +++ b/WordPress/Sniffs/Functions/FunctionRestrictionsSniff.php @@ -7,20 +7,26 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Functions; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restricts usage of some functions. * * @package WPCS\WordPressCodingStandards * * @since 0.3.0 + * @since 0.13.0 Class name changed: this class is now namespaced. + * * @deprecated 0.10.0 The functionality which used to be contained in this class has been moved to * the WordPress_AbstractFunctionRestrictionsSniff class. * This class is left here to prevent backward-compatibility breaks for * custom sniffs extending the old class and references to this * sniff from custom phpcs.xml files. - * @see WordPress_AbstractFunctionRestrictionsSniff + * @see \WordPress\AbstractFunctionRestrictionsSniff */ -class WordPress_Sniffs_Functions_FunctionRestrictionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class FunctionRestrictionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index 856aed26d9..fecbac8773 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -7,16 +7,21 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\NamingConventions; + +use WordPress\AbstractFunctionParameterSniff; + /** * Verify that everything defined in the global namespace is prefixed with a theme/plugin specific prefix. * * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. * - * @uses WordPress_Sniff::$custom_test_class_whitelist + * @uses \WordPress\Sniff::$custom_test_class_whitelist */ -class WordPress_Sniffs_NamingConventions_PrefixAllGlobalsSniff extends WordPress_AbstractFunctionParameterSniff { +class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff { /** * Error message template. diff --git a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php index d23effcca8..d4b5f3a9a4 100644 --- a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -19,6 +19,7 @@ * @package WPCS\WordPressCodingStandards * * @since 0.1.0 + * @since 0.13.0 Class name changed: this class is now namespaced. * * Last synced with parent class July 2016 up to commit 4fea2e651109e41066a81e22e004d851fb1287f6. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php diff --git a/WordPress/Sniffs/NamingConventions/ValidHookNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidHookNameSniff.php index bc4b13611b..0974b7d7a0 100644 --- a/WordPress/Sniffs/NamingConventions/ValidHookNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidHookNameSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\NamingConventions; + +use WordPress\AbstractFunctionParameterSniff; + /** * Use lowercase letters in action and filter names. Separate words via underscores. * @@ -21,8 +25,9 @@ * * @since 0.10.0 * @since 0.11.0 Extends the WordPress_AbstractFunctionParameterSniff class. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_NamingConventions_ValidHookNameSniff extends WordPress_AbstractFunctionParameterSniff { +class ValidHookNameSniff extends AbstractFunctionParameterSniff { /** * Additional word separators. diff --git a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php index 55a353b700..deac96a7c5 100644 --- a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -10,6 +10,7 @@ if ( ! class_exists( 'PHP_CodeSniffer_Standards_AbstractVariableSniff', true ) ) { throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found' ); } +use WordPress\Sniff; /** * Checks the naming of variables and member variables. @@ -19,6 +20,7 @@ * @package WPCS\WordPressCodingStandards * * @since 0.9.0 + * @since 0.13.0 Class name changed: this class is now namespaced. * * Last synced with base class July 2014 at commit ed257ca0e56ad86cd2a4d6fa38ce0b95141c824f. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -301,10 +303,10 @@ protected function mergeWhiteList( $phpcs_file ) { || $this->customVariablesWhitelist !== $this->addedCustomProperties['variables'] ) { // Fix property potentially passed as comma-delimited string. - $customProperties = WordPress_Sniff::merge_custom_array( $this->customPropertiesWhitelist, array(), false ); + $customProperties = Sniff::merge_custom_array( $this->customPropertiesWhitelist, array(), false ); if ( ! empty( $this->customVariablesWhitelist ) ) { - $customProperties = WordPress_Sniff::merge_custom_array( + $customProperties = Sniff::merge_custom_array( $this->customVariablesWhitelist, $customProperties, false @@ -317,7 +319,7 @@ protected function mergeWhiteList( $phpcs_file ) { ); } - $this->whitelisted_mixed_case_member_var_names = WordPress_Sniff::merge_custom_array( + $this->whitelisted_mixed_case_member_var_names = Sniff::merge_custom_array( $customProperties, $this->whitelisted_mixed_case_member_var_names ); diff --git a/WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php b/WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php index bf2e83654b..a38d190c1a 100644 --- a/WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restrict the use of various development functions. * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_PHP_DevelopmentFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class DevelopmentFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php b/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php index e141eb9446..cef9e83136 100644 --- a/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php @@ -7,6 +7,8 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + /** * Discourages the use of various native PHP functions and suggests alternatives. * @@ -15,6 +17,8 @@ * @since 0.1.0 * @since 0.10.0 The checks for the POSIX functions have been replaced by the stand-alone * sniff WordPress_Sniffs_PHP_POSIXFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. + * * @deprecated 0.11.0 The checks for the PHP development functions have been replaced by the * stand-alone sniff WordPress_Sniffs_PHP_DevelopmentFunctionsSniff. * The checks for the WP deprecated functions have been replaced by the @@ -29,7 +33,7 @@ * function. To check for `register_globals` ini directive use * PHPCompatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff from wimg/PHPCompatibility. */ -class WordPress_Sniffs_PHP_DiscouragedFunctionsSniff { +class DiscouragedFunctionsSniff { /** * Don't use. diff --git a/WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php b/WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php index 4b8ca5c4c1..a6e12e139c 100644 --- a/WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Discourages the use of various native PHP functions and suggests alternatives. * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_PHP_DiscouragedPHPFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class DiscouragedPHPFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to discourage. diff --git a/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php b/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php index 936bdedf1d..cab71a4e16 100644 --- a/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/POSIXFunctionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference * to their POSIX counterparts. @@ -18,8 +22,9 @@ * * @since 0.10.0 Previously this check was contained within WordPress_Sniffs_VIP_RestrictedFunctionsSniff * and the WordPress_Sniffs_PHP_DiscouragedPHPFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_PHP_POSIXFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class POSIXFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/PHP/StrictComparisonsSniff.php b/WordPress/Sniffs/PHP/StrictComparisonsSniff.php index c87d9a0b9c..e24b98b4b9 100644 --- a/WordPress/Sniffs/PHP/StrictComparisonsSniff.php +++ b/WordPress/Sniffs/PHP/StrictComparisonsSniff.php @@ -7,19 +7,24 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\Sniff; + /** * Enforces Strict Comparison checks, based upon Squiz code. * * @package WPCS\WordPressCodingStandards * * @since 0.4.0 + * @since 0.13.0 Class name changed: this class is now namespaced. * * Last synced with base class ?[unknown date]? at commit ?[unknown commit]?. * It is currently unclear whether this sniff is actually based on Squiz code on whether the above * reference to it is a copy/paste oversight. * @link Possibly: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php */ -class WordPress_Sniffs_PHP_StrictComparisonsSniff extends WordPress_Sniff { +class StrictComparisonsSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/PHP/StrictInArraySniff.php b/WordPress/Sniffs/PHP/StrictInArraySniff.php index cc67f244bb..45a68b9e88 100644 --- a/WordPress/Sniffs/PHP/StrictInArraySniff.php +++ b/WordPress/Sniffs/PHP/StrictInArraySniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\AbstractFunctionParameterSniff; + /** * Flag calling in_array(), array_search() and array_keys() without true as the third parameter. * @@ -19,8 +23,9 @@ * The sniff no longer needlessly extends the WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff * which it didn't use. * @since 0.11.0 Refactored to extend the new WordPress_AbstractFunctionParameterSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_PHP_StrictInArraySniff extends WordPress_AbstractFunctionParameterSniff { +class StrictInArraySniff extends AbstractFunctionParameterSniff { /** * The group name for this group of functions. diff --git a/WordPress/Sniffs/PHP/YodaConditionsSniff.php b/WordPress/Sniffs/PHP/YodaConditionsSniff.php index 8cf7e99632..dda70d79ce 100644 --- a/WordPress/Sniffs/PHP/YodaConditionsSniff.php +++ b/WordPress/Sniffs/PHP/YodaConditionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\PHP; + +use WordPress\Sniff; + /** * Enforces Yoda conditional statements. * @@ -16,8 +20,9 @@ * * @since 0.3.0 * @since 0.12.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_PHP_YodaConditionsSniff extends WordPress_Sniff { +class YodaConditionsSniff extends Sniff { /** * The tokens that indicate the start of a condition. diff --git a/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php b/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php index 0dd2c5c4f0..a9343d6213 100644 --- a/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php +++ b/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionParameterSniff; + /** * Discourages removal of the admin bar. * @@ -18,8 +22,9 @@ * @since 0.11.0 - Extends the WordPress_AbstractFunctionParameterSniff class. * - Added the $remove_only property. * - Now also sniffs for manipulation of the admin bar visibility through CSS. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_AdminBarRemovalSniff extends WordPress_AbstractFunctionParameterSniff { +class AdminBarRemovalSniff extends AbstractFunctionParameterSniff { /** * A list of tokenizers this sniff supports. diff --git a/WordPress/Sniffs/VIP/CronIntervalSniff.php b/WordPress/Sniffs/VIP/CronIntervalSniff.php index 1c255a5a5e..1c30f47821 100644 --- a/WordPress/Sniffs/VIP/CronIntervalSniff.php +++ b/WordPress/Sniffs/VIP/CronIntervalSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\Sniff; + /** * Flag cron schedules less than 15 minutes. * @@ -17,8 +21,9 @@ * @since 0.3.0 * @since 0.11.0 - Extends the WordPress_Sniff class. * - Now deals correctly with WP time constants. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_CronIntervalSniff extends WordPress_Sniff { +class CronIntervalSniff extends Sniff { /** * Known WP Time constant names and their value. diff --git a/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php b/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php index eb92de3781..a7aa844467 100644 --- a/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php +++ b/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\Sniff; + /** * Flag Database direct queries. * @@ -18,8 +22,9 @@ * @since 0.3.0 * @since 0.6.0 Removed the add_unique_message() function as it is no longer needed. * @since 0.11.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_DirectDatabaseQuerySniff extends WordPress_Sniff { +class DirectDatabaseQuerySniff extends Sniff { /** * List of custom cache get functions. diff --git a/WordPress/Sniffs/VIP/FileSystemWritesDisallowSniff.php b/WordPress/Sniffs/VIP/FileSystemWritesDisallowSniff.php index e535328f18..104a71a448 100644 --- a/WordPress/Sniffs/VIP/FileSystemWritesDisallowSniff.php +++ b/WordPress/Sniffs/VIP/FileSystemWritesDisallowSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Disallow Filesystem writes. * @@ -17,8 +21,9 @@ * @since 0.3.0 * @since 0.11.0 Extends the WordPress_AbstractFunctionRestrictionsSniff instead of the * Generic_Sniffs_PHP_ForbiddenFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_FileSystemWritesDisallowSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class FileSystemWritesDisallowSniff extends AbstractFunctionRestrictionsSniff { /** * If true, an error will be thrown; otherwise a warning. diff --git a/WordPress/Sniffs/VIP/OrderByRandSniff.php b/WordPress/Sniffs/VIP/OrderByRandSniff.php index 158a11312c..e592a98900 100644 --- a/WordPress/Sniffs/VIP/OrderByRandSniff.php +++ b/WordPress/Sniffs/VIP/OrderByRandSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractArrayAssignmentRestrictionsSniff; + /** * Flag using orderby => rand. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.9.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_OrderByRandSniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class OrderByRandSniff extends AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/VIP/PluginMenuSlugSniff.php b/WordPress/Sniffs/VIP/PluginMenuSlugSniff.php index e99b0b202f..655d9e7965 100644 --- a/WordPress/Sniffs/VIP/PluginMenuSlugSniff.php +++ b/WordPress/Sniffs/VIP/PluginMenuSlugSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionParameterSniff; + /** * Warn about __FILE__ for page registration. * @@ -16,8 +20,9 @@ * * @since 0.3.0 * @since 0.11.0 Refactored to extend the new WordPress_AbstractFunctionParameterSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_PluginMenuSlugSniff extends WordPress_AbstractFunctionParameterSniff { +class PluginMenuSlugSniff extends AbstractFunctionParameterSniff { /** * The group name for this group of functions. diff --git a/WordPress/Sniffs/VIP/PostsPerPageSniff.php b/WordPress/Sniffs/VIP/PostsPerPageSniff.php index 6db73490ad..d4f28adcb1 100644 --- a/WordPress/Sniffs/VIP/PostsPerPageSniff.php +++ b/WordPress/Sniffs/VIP/PostsPerPageSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractArrayAssignmentRestrictionsSniff; + /** * Flag returning high or infinite posts_per_page. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.3.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_PostsPerPageSniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class PostsPerPageSniff extends AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php b/WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php index cb58395a72..41914ec235 100644 --- a/WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php +++ b/WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restricts usage of some functions in VIP context. * @@ -24,8 +28,9 @@ * The check for `parse_url()` and `curl_*` have been moved to the stand-alone sniff * WordPress_Sniffs_WP_AlternativeFunctionsSniff. * The check for `eval()` now defers to the upstream Squiz.PHP.Eval sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_RestrictedFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class RestrictedFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/VIP/RestrictedVariablesSniff.php b/WordPress/Sniffs/VIP/RestrictedVariablesSniff.php index d184daed85..05737ceb59 100644 --- a/WordPress/Sniffs/VIP/RestrictedVariablesSniff.php +++ b/WordPress/Sniffs/VIP/RestrictedVariablesSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractVariableRestrictionsSniff; + /** * Restricts usage of some variables in VIP context. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.3.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_RestrictedVariablesSniff extends WordPress_AbstractVariableRestrictionsSniff { +class RestrictedVariablesSniff extends AbstractVariableRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/VIP/SessionFunctionsUsageSniff.php b/WordPress/Sniffs/VIP/SessionFunctionsUsageSniff.php index 96ed685c86..9ae5c0f1fc 100644 --- a/WordPress/Sniffs/VIP/SessionFunctionsUsageSniff.php +++ b/WordPress/Sniffs/VIP/SessionFunctionsUsageSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Discourages the use of session functions. * @@ -17,8 +21,9 @@ * @since 0.3.0 * @since 0.11.0 Extends the WordPress_AbstractFunctionRestrictionsSniff instead of the * Generic_Sniffs_PHP_ForbiddenFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_SessionFunctionsUsageSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class SessionFunctionsUsageSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/VIP/SessionVariableUsageSniff.php b/WordPress/Sniffs/VIP/SessionVariableUsageSniff.php index f4b48639fe..74edbef6d4 100644 --- a/WordPress/Sniffs/VIP/SessionVariableUsageSniff.php +++ b/WordPress/Sniffs/VIP/SessionVariableUsageSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\Sniff; + /** * Discourages the use of the session variable. * Creating a session writes a file to the server and is unreliable in a multi-server environment. @@ -19,8 +23,9 @@ * @since 0.10.0 The sniff no longer needlessly extends the Generic_Sniffs_PHP_ForbiddenFunctionsSniff * which it didn't use. * @since 0.12.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_SessionVariableUsageSniff extends WordPress_Sniff { +class SessionVariableUsageSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/VIP/SlowDBQuerySniff.php b/WordPress/Sniffs/VIP/SlowDBQuerySniff.php index 9485cd414d..d3b022fe0a 100644 --- a/WordPress/Sniffs/VIP/SlowDBQuerySniff.php +++ b/WordPress/Sniffs/VIP/SlowDBQuerySniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractArrayAssignmentRestrictionsSniff; + /** * Flag potentially slow queries. * @@ -18,8 +22,9 @@ * @since 0.12.0 Introduced new and more intuitively named 'slow query' whitelist * comment, replacing the 'tax_query' whitelist comment which is now * deprecated. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_SlowDBQuerySniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class SlowDBQuerySniff extends AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php b/WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php index 5c492065fe..c3b7737250 100644 --- a/WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php +++ b/WordPress/Sniffs/VIP/SuperGlobalInputUsageSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\Sniff; + /** * Flag any usage of super global input var ( _GET / _POST / etc. ). * @@ -15,9 +19,10 @@ * @package WPCS\WordPressCodingStandards * * @since 0.3.0 - * @since 0.4.0 This class now extends WordPress_Sniff. + * @since 0.4.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_SuperGlobalInputUsageSniff extends WordPress_Sniff { +class SuperGlobalInputUsageSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/VIP/TimezoneChangeSniff.php b/WordPress/Sniffs/VIP/TimezoneChangeSniff.php index 2fe0fc1c01..5740f9eb7e 100644 --- a/WordPress/Sniffs/VIP/TimezoneChangeSniff.php +++ b/WordPress/Sniffs/VIP/TimezoneChangeSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Disallow the changing of timezone. * @@ -17,8 +21,9 @@ * @since 0.3.0 * @since 0.11.0 Extends the WordPress_AbstractFunctionRestrictionsSniff instead of the * Generic_Sniffs_PHP_ForbiddenFunctionsSniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_TimezoneChangeSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class TimezoneChangeSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php b/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php index 3edc57549f..04a5e8701f 100644 --- a/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php +++ b/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\VIP; + +use WordPress\Sniff; + /** * Flag any non-validated/sanitized input ( _GET / _POST / etc. ). * @@ -15,10 +19,11 @@ * @package WPCS\WordPressCodingStandards * * @since 0.3.0 - * @since 0.4.0 This class now extends WordPress_Sniff. - * @since 0.5.0 Method getArrayIndexKey() has been moved to WordPress_Sniff. + * @since 0.4.0 This class now extends WordPress_Sniff. + * @since 0.5.0 Method getArrayIndexKey() has been moved to WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_VIP_ValidatedSanitizedInputSniff extends WordPress_Sniff { +class ValidatedSanitizedInputSniff extends Sniff { /** * Check for validation functions for a variable within its own parenthesis only. diff --git a/WordPress/Sniffs/Variables/GlobalVariablesSniff.php b/WordPress/Sniffs/Variables/GlobalVariablesSniff.php index 55214417eb..68386b7c4f 100644 --- a/WordPress/Sniffs/Variables/GlobalVariablesSniff.php +++ b/WordPress/Sniffs/Variables/GlobalVariablesSniff.php @@ -7,9 +7,11 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Variables; + +use WordPress\Sniff; + /** - * WordPress_Sniffs_Variables_GlobalVariablesSniff. - * * Warns about overwriting WordPress native global variables. * * @package WPCS\WordPressCodingStandards @@ -17,10 +19,11 @@ * @since 0.3.0 * @since 0.4.0 This class now extends WordPress_Sniff. * @since 0.12.0 The $wp_globals property has been moved to the WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. * - * @uses WordPress_Sniff::$custom_test_class_whitelist + * @uses \WordPress\Sniff::$custom_test_class_whitelist */ -class WordPress_Sniffs_Variables_GlobalVariablesSniff extends WordPress_Sniff { +class GlobalVariablesSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php b/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php index f28a07af45..9b85e725af 100644 --- a/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php +++ b/WordPress/Sniffs/Variables/VariableRestrictionsSniff.php @@ -7,21 +7,27 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\Variables; + +use WordPress\AbstractVariableRestrictionsSniff; + /** * Restricts usage of some variables. * * @package WPCS\WordPressCodingStandards * * @since 0.3.0 + * @since 0.13.0 Class name changed: this class is now namespaced. + * * @deprecated 0.10.0 The functionality which used to be contained in this class has been moved to * the WordPress_AbstractVariableRestrictionsSniff class. * This class is left here to prevent backward-compatibility breaks for * custom sniffs extending the old class and references to this * sniff from custom phpcs.xml files. * This file is also still used to unit test the abstract class. - * @see WordPress_AbstractVariableRestrictionsSniff + * @see \WordPress\AbstractVariableRestrictionsSniff */ -class WordPress_Sniffs_Variables_VariableRestrictionsSniff extends WordPress_AbstractVariableRestrictionsSniff { +class VariableRestrictionsSniff extends AbstractVariableRestrictionsSniff { /** * Groups of variables to restrict. diff --git a/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php b/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php index 7deda9d7bf..e96d41a557 100644 --- a/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php +++ b/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Discourages the use of various functions and suggests (WordPress) alternatives. * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_AlternativeFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class AlternativeFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/WP/CapitalPDangitSniff.php b/WordPress/Sniffs/WP/CapitalPDangitSniff.php index b80964d9ed..b2ead93d9f 100644 --- a/WordPress/Sniffs/WP/CapitalPDangitSniff.php +++ b/WordPress/Sniffs/WP/CapitalPDangitSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\Sniff; + /** * Capital P Dangit! * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_CapitalPDangitSniff extends WordPress_Sniff { +class CapitalPDangitSniff extends Sniff { /** * Regex to match a large number or spelling variations of WordPress in text strings. diff --git a/WordPress/Sniffs/WP/DeprecatedClassesSniff.php b/WordPress/Sniffs/WP/DeprecatedClassesSniff.php index 3145c82198..05d0428f11 100644 --- a/WordPress/Sniffs/WP/DeprecatedClassesSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedClassesSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\AbstractClassRestrictionsSniff; + /** * Restricts the use of deprecated WordPress classes and suggests alternatives. * * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_DeprecatedClassesSniff extends WordPress_AbstractClassRestrictionsSniff { +class DeprecatedClassesSniff extends AbstractClassRestrictionsSniff { /** * Minimum WordPress version. diff --git a/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php b/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php index 1604e17594..d2cc749104 100644 --- a/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedFunctionsSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Restricts the use of various deprecated WordPress functions and suggests alternatives. * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_DeprecatedFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class DeprecatedFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Minimum WordPress version. diff --git a/WordPress/Sniffs/WP/DeprecatedParametersSniff.php b/WordPress/Sniffs/WP/DeprecatedParametersSniff.php index c04190127d..7f0c26e04b 100644 --- a/WordPress/Sniffs/WP/DeprecatedParametersSniff.php +++ b/WordPress/Sniffs/WP/DeprecatedParametersSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\AbstractFunctionParameterSniff; + /** * Check for usage of deprecated parameters in WP functions and suggest alternative based on the parameter passed. * @@ -19,8 +23,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_DeprecatedParametersSniff extends WordPress_AbstractFunctionParameterSniff { +class DeprecatedParametersSniff extends AbstractFunctionParameterSniff { /** * The group name for this group of functions. diff --git a/WordPress/Sniffs/WP/DiscouragedFunctionsSniff.php b/WordPress/Sniffs/WP/DiscouragedFunctionsSniff.php index a2045f2396..8e1deb763c 100644 --- a/WordPress/Sniffs/WP/DiscouragedFunctionsSniff.php +++ b/WordPress/Sniffs/WP/DiscouragedFunctionsSniff.php @@ -7,14 +7,19 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\AbstractFunctionRestrictionsSniff; + /** * Discourages the use of various WordPress functions and suggests alternatives. * * @package WPCS\WordPressCodingStandards * * @since 0.11.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_DiscouragedFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff { +class DiscouragedFunctionsSniff extends AbstractFunctionRestrictionsSniff { /** * Groups of functions to restrict. diff --git a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php index 8b2fe574e9..ea9af15b9f 100644 --- a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php +++ b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\Sniff; + /** * Makes sure scripts and styles are enqueued and not explicitly echo'd. * @@ -16,8 +20,9 @@ * * @since 0.3.0 * @since 0.12.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_EnqueuedResourcesSniff extends WordPress_Sniff { +class EnqueuedResourcesSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/WP/I18nSniff.php b/WordPress/Sniffs/WP/I18nSniff.php index 7a1bf7f5ea..06536475d4 100644 --- a/WordPress/Sniffs/WP/I18nSniff.php +++ b/WordPress/Sniffs/WP/I18nSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\Sniff; + /** * Makes sure WP internationalization functions are used properly. * @@ -20,8 +24,9 @@ * - Now has the ability to handle text-domain set via the command-line * as a comma-delimited list. * `phpcs --runtime-set text_domain my-slug,default` + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_I18nSniff extends WordPress_Sniff { +class I18nSniff extends Sniff { /** * These Regexes copied from http://php.net/manual/en/function.sprintf.php#93552 diff --git a/WordPress/Sniffs/WP/PreparedSQLSniff.php b/WordPress/Sniffs/WP/PreparedSQLSniff.php index 85d3202ae1..e9fef1af06 100644 --- a/WordPress/Sniffs/WP/PreparedSQLSniff.php +++ b/WordPress/Sniffs/WP/PreparedSQLSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WP; + +use WordPress\Sniff; + /** * Sniff for prepared SQL. * @@ -17,8 +21,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.8.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WP_PreparedSQLSniff extends WordPress_Sniff { +class PreparedSQLSniff extends Sniff { /** * The lists of $wpdb methods. diff --git a/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php index 16bc7de9e3..f5c3466932 100755 --- a/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WhiteSpace; + +use WordPress\Sniff; + /** * Ensure cast statements don't contain whitespace, but *are* surrounded by whitespace, based upon Squiz code. * @@ -18,8 +22,9 @@ * @since 0.11.0 This sniff now has the ability to fix the issues it flags. * @since 0.11.0 The error level for all errors thrown by this sniff has been raised from warning to error. * @since 0.12.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WhiteSpace_CastStructureSpacingSniff extends WordPress_Sniff { +class CastStructureSpacingSniff extends Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 736913567d..88ad2be200 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WhiteSpace; + +use WordPress\Sniff; + /** * Enforces spacing around logical operators and assignments, based upon Squiz code. * @@ -16,13 +20,14 @@ * @since 2013-06-11 This sniff no longer supports JS. * @since 0.3.0 This sniff now has the ability to fix most errors it flags. * @since 0.7.0 This class now extends WordPress_Sniff. + * @since 0.13.0 Class name changed: this class is now namespaced. * * Last synced with base class 2017-01-15 at commit b024ad84656c37ef5733c6998ebc1e60957b2277. * Note: This class has diverged quite far from the original. All the same, checking occassionally * to see if there are upstream fixes made from which this sniff can benefit, is warranted. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php */ -class WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff extends WordPress_Sniff { +class ControlStructureSpacingSniff extends Sniff { /** * Check for blank lines on start/end of control structures. diff --git a/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php b/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php index 7ca26d2c2b..f8555e550b 100644 --- a/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php +++ b/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\WhiteSpace; + +use WordPress\Sniff; + /** * Enforces using spaces for mid-line alignment. * @@ -15,8 +19,9 @@ * @package WPCS\WordPressCodingStandards * * @since 0.12.0 + * @since 0.13.0 Class name changed: this class is now namespaced. */ -class WordPress_Sniffs_WhiteSpace_DisallowInlineTabsSniff extends WordPress_Sniff { +class DisallowInlineTabsSniff extends Sniff { /** * The --tab-width CLI value that is being used. diff --git a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 921dfcd462..7677bd3f08 100644 --- a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -28,6 +28,7 @@ * T_BOOLEAN_NOT and the logical operators (`&&` and the like) - via the * registration method and changing the value of the customizable * $ignoreNewlines property. + * @since 0.13.0 Class name changed: this class is now namespaced. * * Last synced with base class June 2017 at commit 41127aa4764536f38f504fb3f7b8831f05919c89. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php diff --git a/WordPress/Sniffs/XSS/EscapeOutputSniff.php b/WordPress/Sniffs/XSS/EscapeOutputSniff.php index 70e2693393..7af4d77ff5 100644 --- a/WordPress/Sniffs/XSS/EscapeOutputSniff.php +++ b/WordPress/Sniffs/XSS/EscapeOutputSniff.php @@ -7,6 +7,10 @@ * @license https://opensource.org/licenses/MIT MIT */ +namespace WordPress\Sniffs\XSS; + +use WordPress\Sniff; + /** * Verifies that all outputted strings are escaped. * @@ -20,8 +24,9 @@ * have been moved to the WordPress_Sniff parent class. * @since 0.12.0 This sniff will now also check for output escaping when using shorthand * echo tags ` Date: Sat, 22 Jul 2017 20:24:14 +0200 Subject: [PATCH 08/37] PHPCS 3.x compat: Namespace all sniffs which extend an upstream sniff --- .../Sniffs/NamingConventions/ValidFunctionNameSniff.php | 8 ++++---- .../Sniffs/NamingConventions/ValidVariableNameSniff.php | 8 ++++---- WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php index d4b5f3a9a4..e9960b4e1b 100644 --- a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -7,9 +7,9 @@ * @license https://opensource.org/licenses/MIT MIT */ -if ( ! class_exists( 'PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff', true ) ) { - throw new PHP_CodeSniffer_Exception( 'Class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff not found' ); -} +namespace WordPress\Sniffs\NamingConventions; + +use PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff as PHPCS_PEAR_ValidFunctionNameSniff; /** * Enforces WordPress function name and method name format, based upon Squiz code. @@ -27,7 +27,7 @@ * {@internal While this class extends the PEAR parent, it does not actually use the checks * contained in the parent. It only uses the properties and the token registration from the parent.}} */ -class WordPress_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff { +class ValidFunctionNameSniff extends PHPCS_PEAR_ValidFunctionNameSniff { /** * Additional double underscore prefixed methods specific to certain PHP native extensions. diff --git a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php index deac96a7c5..5b950885fd 100644 --- a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -7,9 +7,9 @@ * @license https://opensource.org/licenses/MIT MIT */ -if ( ! class_exists( 'PHP_CodeSniffer_Standards_AbstractVariableSniff', true ) ) { - throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found' ); -} +namespace WordPress\Sniffs\NamingConventions; + +use PHP_CodeSniffer_Standards_AbstractVariableSniff as PHPCS_AbstractVariableSniff; use WordPress\Sniff; /** @@ -25,7 +25,7 @@ * Last synced with base class July 2014 at commit ed257ca0e56ad86cd2a4d6fa38ce0b95141c824f. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php */ -class WordPress_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff { +class ValidVariableNameSniff extends PHPCS_AbstractVariableSniff { /** * PHP Reserved Vars. diff --git a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 7677bd3f08..9f5376354f 100644 --- a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -7,9 +7,9 @@ * @license https://opensource.org/licenses/MIT MIT */ -if ( ! class_exists( 'Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff', true ) ) { - throw new PHP_CodeSniffer_Exception( 'Class Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff not found' ); -} +namespace WordPress\Sniffs\WhiteSpace; + +use Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff as PHPCS_Squiz_OperatorSpacingSniff; /** * Verify operator spacing, uses the Squiz sniff, but additionally also sniffs for the `!` (boolean not) operator. @@ -33,7 +33,7 @@ * Last synced with base class June 2017 at commit 41127aa4764536f38f504fb3f7b8831f05919c89. * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php */ -class WordPress_Sniffs_WhiteSpace_OperatorSpacingSniff extends Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff { +class OperatorSpacingSniff extends PHPCS_Squiz_OperatorSpacingSniff { /** * Allow newlines instead of spaces. From 5c4759149ffbd774e4a425473a2a82f06bdf99d0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 05:13:44 +0200 Subject: [PATCH 09/37] PHPCS 3.x compat: Fix all references to upstream PHPCS classes * Add use statements for all PHPCS class references. * Where possible use the PHPCS 3.x class name as a use alias to make the future change of dropping 2.x support easier. --- .../AbstractFunctionRestrictionsSniff.php | 5 +- WordPress/Sniff.php | 50 ++++++++++--------- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 8 +-- .../Arrays/ArrayDeclarationSpacingSniff.php | 3 +- .../Sniffs/Arrays/ArrayIndentationSniff.php | 3 +- .../Arrays/CommaAfterArrayItemSniff.php | 5 +- .../Classes/ClassInstantiationSniff.php | 7 +-- .../CodeAnalysis/EmptyStatementSniff.php | 3 +- WordPress/Sniffs/Files/FileNameSniff.php | 6 +-- .../FunctionCallSignatureNoParamsSniff.php | 7 +-- .../PrefixAllGlobalsSniff.php | 25 +++++----- .../ValidFunctionNameSniff.php | 19 +++---- .../ValidVariableNameSniff.php | 29 +++++------ .../Sniffs/PHP/DiscouragedFunctionsSniff.php | 8 +-- WordPress/Sniffs/PHP/YodaConditionsSniff.php | 15 +++--- WordPress/Sniffs/VIP/AdminBarRemovalSniff.php | 5 +- WordPress/Sniffs/VIP/CronIntervalSniff.php | 3 +- .../Sniffs/VIP/DirectDatabaseQuerySniff.php | 3 +- .../Sniffs/Variables/GlobalVariablesSniff.php | 5 +- WordPress/Sniffs/WP/CapitalPDangitSniff.php | 5 +- .../Sniffs/WP/EnqueuedResourcesSniff.php | 3 +- WordPress/Sniffs/WP/I18nSniff.php | 7 +-- .../WhiteSpace/CastStructureSpacingSniff.php | 3 +- .../ControlStructureSpacingSniff.php | 15 +++--- .../WhiteSpace/OperatorSpacingSniff.php | 3 +- WordPress/Sniffs/XSS/EscapeOutputSniff.php | 9 ++-- 26 files changed, 142 insertions(+), 112 deletions(-) diff --git a/WordPress/AbstractFunctionRestrictionsSniff.php b/WordPress/AbstractFunctionRestrictionsSniff.php index a607d21edf..cba5f6bbb7 100644 --- a/WordPress/AbstractFunctionRestrictionsSniff.php +++ b/WordPress/AbstractFunctionRestrictionsSniff.php @@ -10,6 +10,7 @@ namespace WordPress; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Restricts usage of some functions. @@ -210,7 +211,7 @@ public function is_targetted_token( $stackPtr ) { // Exclude function definitions, class methods, and namespaced calls. if ( T_STRING === $this->tokens[ $stackPtr ]['code'] && isset( $this->tokens[ ( $stackPtr - 1 ) ] ) ) { - $prev = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); + $prev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); if ( false !== $prev ) { // Skip sniffing if calling a same-named method, or on function definitions. @@ -226,7 +227,7 @@ public function is_targetted_token( $stackPtr ) { // Skip namespaced functions, ie: \foo\bar() not \bar(). if ( T_NS_SEPARATOR === $this->tokens[ $prev ]['code'] ) { - $pprev = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $prev - 1 ), null, true ); + $pprev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prev - 1 ), null, true ); if ( false !== $pprev && T_STRING === $this->tokens[ $pprev ]['code'] ) { return false; } diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index ec47c6dd16..5cb011120f 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -9,6 +9,10 @@ namespace WordPress; +use PHP_CodeSniffer_Sniff as PHPCS_Sniff; +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * Represents a PHP_CodeSniffer sniff for sniffing WordPress coding standards. * @@ -28,7 +32,7 @@ * In the rare few cases where the array values *do* have meaning, this * is documented in the property documentation.}} */ -abstract class Sniff implements PHP_CodeSniffer_Sniff { +abstract class Sniff implements PHPCS_Sniff { /** * List of the functions which verify nonces. @@ -798,7 +802,7 @@ abstract class Sniff implements PHP_CodeSniffer_Sniff { * * @since 0.4.0 * - * @var PHP_CodeSniffer_File + * @var \PHP_CodeSniffer\Files\File */ protected $phpcsFile; @@ -816,14 +820,14 @@ abstract class Sniff implements PHP_CodeSniffer_Sniff { * * @since 0.11.0 * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. * * @return int|void Integer stack pointer to skip forward or void to continue * normal file processing. */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + public function process( File $phpcsFile, $stackPtr ) { $this->init( $phpcsFile ); return $this->process_token( $stackPtr ); } @@ -848,9 +852,9 @@ abstract public function process_token( $stackPtr ); * * @since 0.4.0 * - * @param PHP_CodeSniffer_File $phpcsFile The file currently being processed. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file currently being processed. */ - protected function init( PHP_CodeSniffer_File $phpcsFile ) { + protected function init( File $phpcsFile ) { $this->phpcsFile = $phpcsFile; $this->tokens = $phpcsFile->getTokens(); } @@ -1200,7 +1204,7 @@ protected function is_assignment( $stackPtr ) { } $next_non_empty = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens + Tokens::$emptyTokens , ( $stackPtr + 1 ) , null , true @@ -1214,7 +1218,7 @@ protected function is_assignment( $stackPtr ) { } // If the next token is an assignment, that's all we need to know. - if ( isset( PHP_CodeSniffer_Tokens::$assignmentTokens[ $this->tokens[ $next_non_empty ]['code'] ] ) ) { + if ( isset( Tokens::$assignmentTokens[ $this->tokens[ $next_non_empty ]['code'] ] ) ) { return true; } @@ -1394,7 +1398,7 @@ protected function is_safe_casted( $stackPtr ) { // Get the last non-empty token. $prev = $this->phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens + Tokens::$emptyTokens , ( $stackPtr - 1 ) , null , true @@ -1481,7 +1485,7 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) { * to resolve the function name, do so. */ $first_non_empty = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true @@ -1538,7 +1542,7 @@ protected function get_array_access_key( $stackPtr ) { // Find the next non-empty token. $open_bracket = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true @@ -1704,19 +1708,19 @@ protected function is_comparison( $stackPtr ) { // Find the previous non-empty token. We check before the var first because // yoda conditions are usually expected. $previous_token = $this->phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); - if ( isset( PHP_CodeSniffer_Tokens::$comparisonTokens[ $this->tokens[ $previous_token ]['code'] ] ) ) { + if ( isset( Tokens::$comparisonTokens[ $this->tokens[ $previous_token ]['code'] ] ) ) { return true; } // Maybe the comparison operator is after this. $next_token = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true @@ -1726,14 +1730,14 @@ protected function is_comparison( $stackPtr ) { while ( T_OPEN_SQUARE_BRACKET === $this->tokens[ $next_token ]['code'] ) { $next_token = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $this->tokens[ $next_token ]['bracket_closer'] + 1 ), null, true ); } - if ( isset( PHP_CodeSniffer_Tokens::$comparisonTokens[ $this->tokens[ $next_token ]['code'] ] ) ) { + if ( isset( Tokens::$comparisonTokens[ $this->tokens[ $next_token ]['code'] ] ) ) { return true; } @@ -1829,7 +1833,7 @@ public function does_function_call_have_parameters( $stackPtr ) { return false; } - $next_non_empty = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); // Deal with short array syntax. if ( 'T_OPEN_SHORT_ARRAY' === $this->tokens[ $stackPtr ]['type'] ) { @@ -1856,7 +1860,7 @@ public function does_function_call_have_parameters( $stackPtr ) { } $close_parenthesis = $this->tokens[ $next_non_empty ]['parenthesis_closer']; - $next_next_non_empty = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next_non_empty + 1 ), ( $close_parenthesis + 1 ), true ); + $next_next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), ( $close_parenthesis + 1 ), true ); if ( $next_next_non_empty === $close_parenthesis ) { // No parameters. @@ -1932,7 +1936,7 @@ public function get_function_call_parameters( $stackPtr ) { $nestedParenthesisCount = 0; } else { - $opener = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $opener = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); $closer = $this->tokens[ $opener ]['parenthesis_closer']; $nestedParenthesisCount = 1; @@ -1982,7 +1986,7 @@ public function get_function_call_parameters( $stackPtr ) { * Prevents code like the following from setting a third parameter: * functionCall( $param1, $param2, ); */ - $has_next_param = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next_comma + 1 ), $closer, true, null, true ); + $has_next_param = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_comma + 1 ), $closer, true, null, true ); if ( false === $has_next_param ) { break; } @@ -2154,7 +2158,7 @@ public function get_declared_namespace_name( $stackPtr ) { return false; } - $nextToken = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); if ( T_OPEN_CURLY_BRACKET === $this->tokens[ $nextToken ]['code'] ) { // Declaration for global namespace when using multiple namespaces in a file. // I.e.: `namespace {}`. diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php index 9f3e593d74..2f9899f1c7 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -9,6 +9,8 @@ namespace WordPress\Sniffs\Arrays; +use PHP_CodeSniffer_File as File; + /** * Enforces WordPress array format, based upon Squiz code. * @@ -49,11 +51,11 @@ public function register() { * * @deprecated 0.13.0 * - * @param PHP_CodeSniffer_File $phpcsFile A PHP_CodeSniffer file. - * @param int $stackPtr The position of the token. + * @param \PHP_CodeSniffer\Files\File $phpcsFile A PHP_CodeSniffer file. + * @param int $stackPtr The position of the token. * * @return void */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {} + public function process( File $phpcsFile, $stackPtr ) {} } // End class. diff --git a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php index 36c075e18d..95b1c167ba 100644 --- a/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Arrays; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces WordPress array spacing format. @@ -219,7 +220,7 @@ protected function process_single_line_array( $stackPtr, $opener, $closer ) { * interpreted as alignment whitespace. */ $first_non_empty = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, $item['start'], ( $item['end'] + 1 ), true diff --git a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php index 9a67948759..b9de09f59b 100644 --- a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Arrays; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces WordPress array indentation for multi-line arrays. @@ -69,7 +70,7 @@ public function register() { * * Existing heredoc, nowdoc and inline HTML indentation should be respected at all times. */ - $this->ignore_tokens = PHP_CodeSniffer_Tokens::$heredocTokens; + $this->ignore_tokens = Tokens::$heredocTokens; unset( $this->ignore_tokens[ T_START_HEREDOC ], $this->ignore_tokens[ T_START_NOWDOC ] ); $this->ignore_tokens[ T_INLINE_HTML ] = T_INLINE_HTML; diff --git a/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php b/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php index f2ef82522d..16614102f3 100644 --- a/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php +++ b/WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Arrays; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces a comma after each array item and the spacing around it. @@ -114,7 +115,7 @@ public function process_token( $stackPtr ) { } $last_content = $this->phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, $item['end'], $item['start'], true @@ -155,7 +156,7 @@ public function process_token( $stackPtr ) { if ( $last_content !== $item['end'] // Ignore whitespace at the end of a multi-line item if it is the end of a heredoc/nowdoc. && ( true === $single_line - || ! isset( PHP_CodeSniffer_Tokens::$heredocTokens[ $this->tokens[ $last_content ]['code'] ] ) ) + || ! isset( Tokens::$heredocTokens[ $this->tokens[ $last_content ]['code'] ] ) ) ) { $newlines = 0; $spaces = 0; diff --git a/WordPress/Sniffs/Classes/ClassInstantiationSniff.php b/WordPress/Sniffs/Classes/ClassInstantiationSniff.php index 2465a66c83..85a7d2ca7c 100644 --- a/WordPress/Sniffs/Classes/ClassInstantiationSniff.php +++ b/WordPress/Sniffs/Classes/ClassInstantiationSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Classes; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Verifies object instantiation statements. @@ -58,7 +59,7 @@ public function register() { * * Currently does not account for classnames passed as a variable variable. */ - $this->classname_tokens = PHP_CodeSniffer_Tokens::$emptyTokens; + $this->classname_tokens = Tokens::$emptyTokens; $this->classname_tokens[ T_NS_SEPARATOR ] = T_NS_SEPARATOR; $this->classname_tokens[ T_STRING ] = T_STRING; $this->classname_tokens[ T_SELF ] = T_SELF; @@ -100,7 +101,7 @@ public function process_token( $stackPtr ) { */ if ( 'PHP' === $this->phpcsFile->tokenizerType ) { $prev_non_empty = $this->phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ($stackPtr - 1), null, true @@ -139,7 +140,7 @@ public function process_token( $stackPtr ) { // Walk back to the last part of the class name. $has_comment = false; for ( $classname_ptr = ( $next_non_empty_after_class_name - 1 ); $classname_ptr >= $stackPtr; $classname_ptr-- ) { - if ( ! isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $this->tokens[ $classname_ptr ]['code'] ] ) ) { + if ( ! isset( Tokens::$emptyTokens[ $this->tokens[ $classname_ptr ]['code'] ] ) ) { // Prevent a false positive on variable variables, disregard them for now. if ( $stackPtr === $classname_ptr ) { return; diff --git a/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php b/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php index 3778fd0372..c79f3f08ad 100644 --- a/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php +++ b/WordPress/Sniffs/CodeAnalysis/EmptyStatementSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\CodeAnalysis; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Checks against empty statements. @@ -57,7 +58,7 @@ public function process_token( $stackPtr ) { */ case 'T_SEMICOLON': $prevNonEmpty = $this->phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true diff --git a/WordPress/Sniffs/Files/FileNameSniff.php b/WordPress/Sniffs/Files/FileNameSniff.php index 624edc49d9..a0328c1ac5 100644 --- a/WordPress/Sniffs/Files/FileNameSniff.php +++ b/WordPress/Sniffs/Files/FileNameSniff.php @@ -118,7 +118,7 @@ class FileNameSniff extends Sniff { * @return array */ public function register() { - if ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) { + if ( defined( '\PHP_CODESNIFFER_IN_TESTS' ) ) { $this->class_exceptions = array_merge( $this->class_exceptions, $this->unittest_class_exceptions ); } @@ -195,8 +195,8 @@ public function process_token( $stackPtr ) { if ( ( 'Template' === trim( $this->tokens[ $subpackage ]['content'] ) && $this->tokens[ $subpackage_tag ]['line'] === $this->tokens[ $subpackage ]['line'] ) - && ( ( ! defined( 'PHP_CODESNIFFER_IN_TESTS' ) && '-template.php' !== $fileName_end ) - || ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) && '-template.inc' !== $fileName_end ) ) + && ( ( ! defined( '\PHP_CODESNIFFER_IN_TESTS' ) && '-template.php' !== $fileName_end ) + || ( defined( '\PHP_CODESNIFFER_IN_TESTS' ) && '-template.inc' !== $fileName_end ) ) && false === $has_class ) { $this->phpcsFile->addError( diff --git a/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php b/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php index 0f94a1f144..cb321a0cd4 100644 --- a/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php +++ b/WordPress/Sniffs/Functions/FunctionCallSignatureNoParamsSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Functions; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces no whitespace between the parenthesis of a function call without parameters. @@ -29,7 +30,7 @@ class FunctionCallSignatureNoParamsSniff extends Sniff { * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; } /** @@ -42,7 +43,7 @@ public function register() { public function process_token( $stackPtr ) { // Find the next non-empty token. - $openParenthesis = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + $openParenthesis = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); if ( T_OPEN_PARENTHESIS !== $this->tokens[ $openParenthesis ]['code'] ) { // Not a function call. @@ -55,7 +56,7 @@ public function process_token( $stackPtr ) { } // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $this->phpcsFile->findPrevious( $search, ( $stackPtr - 1 ), null, true ); if ( T_FUNCTION === $this->tokens[ $previous ]['code'] ) { diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index fecbac8773..ad0ec98481 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\NamingConventions; use WordPress\AbstractFunctionParameterSniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Verify that everything defined in the global namespace is prefixed with a theme/plugin specific prefix. @@ -272,7 +273,7 @@ public function process_token( $stackPtr ) { return; } - $constant_name_ptr = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $constant_name_ptr = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); if ( false === $constant_name_ptr ) { // Live coding. return; @@ -329,7 +330,7 @@ protected function process_variable_variable( $stackPtr ) { // Is this a variable variable ? // Not concerned with nested ones as those will be recognized on their own token. - $next_non_empty = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); if ( false === $next_non_empty || ! isset( $indicators[ $this->tokens[ $next_non_empty ]['code'] ] ) ) { return; } @@ -341,14 +342,14 @@ protected function process_variable_variable( $stackPtr ) { $next_non_empty = $this->tokens[ $next_non_empty ]['bracket_closer']; } - $maybe_assignment = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true, null, true ); + $maybe_assignment = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true, null, true ); while ( false !== $maybe_assignment && T_OPEN_SQUARE_BRACKET === $this->tokens[ $maybe_assignment ]['code'] && isset( $this->tokens[ $maybe_assignment ]['bracket_closer'] ) ) { $maybe_assignment = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $this->tokens[ $maybe_assignment ]['bracket_closer'] + 1 ), null, true, @@ -361,7 +362,7 @@ protected function process_variable_variable( $stackPtr ) { return; } - if ( ! isset( PHP_CodeSniffer_Tokens::$assignmentTokens[ $this->tokens[ $maybe_assignment ]['code'] ] ) ) { + if ( ! isset( Tokens::$assignmentTokens[ $this->tokens[ $maybe_assignment ]['code'] ] ) ) { // Not an assignment. return; } @@ -435,13 +436,13 @@ protected function process_variable_assignment( $stackPtr ) { } if ( 'GLOBALS' === $variable_name ) { - $array_open = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $array_open = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); if ( false === $array_open || T_OPEN_SQUARE_BRACKET !== $this->tokens[ $array_open ]['code'] ) { // Live coding or something very silly. return; } - $array_key = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $array_open + 1 ), null, true, null, true ); + $array_key = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $array_open + 1 ), null, true, null, true ); if ( false === $array_key ) { // No key found, nothing to do. return; @@ -451,7 +452,7 @@ protected function process_variable_assignment( $stackPtr ) { $variable_name = $this->strip_quotes( $this->tokens[ $array_key ]['content'] ); // Check whether a prefix is needed. - if ( isset( PHP_CodeSniffer_Tokens::$stringTokens[ $this->tokens[ $array_key ]['code'] ] ) + if ( isset( Tokens::$stringTokens[ $this->tokens[ $array_key ]['code'] ] ) && $this->variable_prefixed_or_whitelisted( $variable_name ) === true ) { return; @@ -470,7 +471,7 @@ protected function process_variable_assignment( $stackPtr ) { // If the first part was dynamic, throw a warning. $is_error = false; } - } elseif ( ! isset( PHP_CodeSniffer_Tokens::$stringTokens[ $this->tokens[ $array_key ]['code'] ] ) ) { + } elseif ( ! isset( Tokens::$stringTokens[ $this->tokens[ $array_key ]['code'] ] ) ) { // Dynamic array key, throw a warning. $is_error = false; } @@ -580,7 +581,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p } else { // This may be a dynamic hook/constant name. $first_non_empty = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, $parameters[1]['start'], ( $parameters[1]['end'] + 1 ), true @@ -593,7 +594,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p $first_non_empty_content = $this->strip_quotes( $this->tokens[ $first_non_empty ]['content'] ); // Try again with just the first token if it's a text string. - if ( isset( PHP_CodeSniffer_Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) + if ( isset( Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) && $this->is_prefixed( $first_non_empty_content ) === true ) { return; @@ -612,7 +613,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p // Start of hook/constant name is dynamic, throw a warning. $is_error = false; } - } elseif ( ! isset( PHP_CodeSniffer_Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) ) { + } elseif ( ! isset( Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) ) { // Dynamic hook/constant name, throw a warning. $is_error = false; } diff --git a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php index e9960b4e1b..4f27b7d993 100644 --- a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\NamingConventions; use PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff as PHPCS_PEAR_ValidFunctionNameSniff; +use PHP_CodeSniffer_File as File; /** * Enforces WordPress function name and method name format, based upon Squiz code. @@ -55,13 +56,13 @@ class ValidFunctionNameSniff extends PHPCS_PEAR_ValidFunctionNameSniff { /** * Processes the tokens outside the scope. * - * @param PHP_CodeSniffer_File $phpcsFile The file being processed. - * @param int $stackPtr The position where this token was - * found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. + * @param int $stackPtr The position where this token was + * found. * * @return void */ - protected function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + protected function processTokenOutsideScope( File $phpcsFile, $stackPtr ) { $functionName = $phpcsFile->getDeclarationName( $stackPtr ); if ( ! isset( $functionName ) ) { @@ -101,14 +102,14 @@ protected function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $s /** * Processes the tokens within the scope. * - * @param PHP_CodeSniffer_File $phpcsFile The file being processed. - * @param int $stackPtr The position where this token was - * found. - * @param int $currScope The position of the current scope. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. + * @param int $stackPtr The position where this token was + * found. + * @param int $currScope The position of the current scope. * * @return void */ - protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope ) { + protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) { $methodName = $phpcsFile->getDeclarationName( $stackPtr ); if ( ! isset( $methodName ) ) { diff --git a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php index 5b950885fd..3a10912b24 100644 --- a/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\NamingConventions; use PHP_CodeSniffer_Standards_AbstractVariableSniff as PHPCS_AbstractVariableSniff; +use PHP_CodeSniffer_File as File; use WordPress\Sniff; /** @@ -122,13 +123,13 @@ class ValidVariableNameSniff extends PHPCS_AbstractVariableSniff { /** * Processes this test, when one of its tokens is encountered. * - * @param PHP_CodeSniffer_File $phpcs_file The file being scanned. - * @param int $stack_ptr The position of the current token in the - * stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned. + * @param int $stack_ptr The position of the current token in the + * stack passed in $tokens. * * @return void */ - protected function processVariable( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) { + protected function processVariable( File $phpcs_file, $stack_ptr ) { $tokens = $phpcs_file->getTokens(); $var_name = ltrim( $tokens[ $stack_ptr ]['content'], '$' ); @@ -208,13 +209,13 @@ protected function processVariable( PHP_CodeSniffer_File $phpcs_file, $stack_ptr /** * Processes class member variables. * - * @param PHP_CodeSniffer_File $phpcs_file The file being scanned. - * @param int $stack_ptr The position of the current token in the - * stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned. + * @param int $stack_ptr The position of the current token in the + * stack passed in $tokens. * * @return void */ - protected function processMemberVar( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) { + protected function processMemberVar( File $phpcs_file, $stack_ptr ) { $tokens = $phpcs_file->getTokens(); @@ -242,13 +243,13 @@ protected function processMemberVar( PHP_CodeSniffer_File $phpcs_file, $stack_pt /** * Processes the variable found within a double quoted string. * - * @param PHP_CodeSniffer_File $phpcs_file The file being scanned. - * @param int $stack_ptr The position of the double quoted - * string. + * @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned. + * @param int $stack_ptr The position of the double quoted + * string. * * @return void */ - protected function processVariableInString( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) { + protected function processVariableInString( File $phpcs_file, $stack_ptr ) { $tokens = $phpcs_file->getTokens(); @@ -294,11 +295,11 @@ public static function isSnakeCase( $var_name ) { * * @since 0.10.0 * - * @param PHP_CodeSniffer_File $phpcs_file The file being scanned. + * @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned. * * @return void */ - protected function mergeWhiteList( $phpcs_file ) { + protected function mergeWhiteList( File $phpcs_file ) { if ( $this->customPropertiesWhitelist !== $this->addedCustomProperties['properties'] || $this->customVariablesWhitelist !== $this->addedCustomProperties['variables'] ) { diff --git a/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php b/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php index cef9e83136..763ab86b24 100644 --- a/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php +++ b/WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php @@ -9,6 +9,8 @@ namespace WordPress\Sniffs\PHP; +use PHP_CodeSniffer_File as File; + /** * Discourages the use of various native PHP functions and suggests alternatives. * @@ -51,11 +53,11 @@ public function register() { * * @deprecated 0.11.0 * - * @param PHP_CodeSniffer_File $phpcsFile A PHP_CodeSniffer file. - * @param int $stackPtr The position of the token. + * @param \PHP_CodeSniffer\Files\File $phpcsFile A PHP_CodeSniffer file. + * @param int $stackPtr The position of the token. * * @return void */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {} + public function process( File $phpcsFile, $stackPtr ) {} } diff --git a/WordPress/Sniffs/PHP/YodaConditionsSniff.php b/WordPress/Sniffs/PHP/YodaConditionsSniff.php index dda70d79ce..6da38d9ac8 100644 --- a/WordPress/Sniffs/PHP/YodaConditionsSniff.php +++ b/WordPress/Sniffs/PHP/YodaConditionsSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\PHP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces Yoda conditional statements. @@ -40,8 +41,8 @@ class YodaConditionsSniff extends Sniff { */ public function register() { - $starters = PHP_CodeSniffer_Tokens::$booleanOperators; - $starters += PHP_CodeSniffer_Tokens::$assignmentTokens; + $starters = Tokens::$booleanOperators; + $starters += Tokens::$assignmentTokens; $starters[ T_CASE ] = T_CASE; $starters[ T_RETURN ] = T_RETURN; $starters[ T_SEMICOLON ] = T_SEMICOLON; @@ -75,7 +76,7 @@ public function process_token( $stackPtr ) { for ( $i = $stackPtr; $i > $start; $i-- ) { // Ignore whitespace. - if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) { + if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) { continue; } @@ -98,15 +99,15 @@ public function process_token( $stackPtr ) { } // Check if this is a var to var comparison, e.g.: if ( $var1 == $var2 ). - $next_non_empty = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + $next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( isset( PHP_CodeSniffer_Tokens::$castTokens[ $this->tokens[ $next_non_empty ]['code'] ] ) ) { - $next_non_empty = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true ); + if ( isset( Tokens::$castTokens[ $this->tokens[ $next_non_empty ]['code'] ] ) ) { + $next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next_non_empty + 1 ), null, true ); } if ( in_array( $this->tokens[ $next_non_empty ]['code'], array( T_SELF, T_PARENT, T_STATIC ), true ) ) { $next_non_empty = $this->phpcsFile->findNext( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_DOUBLE_COLON ) ) + array_merge( Tokens::$emptyTokens, array( T_DOUBLE_COLON ) ) , ( $next_non_empty + 1 ) , null , true diff --git a/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php b/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php index a9343d6213..0dbdb5086a 100644 --- a/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php +++ b/WordPress/Sniffs/VIP/AdminBarRemovalSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\VIP; use WordPress\AbstractFunctionParameterSniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Discourages removal of the admin bar. @@ -140,7 +141,7 @@ class AdminBarRemovalSniff extends AbstractFunctionParameterSniff { */ public function register() { // Set up all string targets. - $this->string_tokens = PHP_CodeSniffer_Tokens::$textStringTokens; + $this->string_tokens = Tokens::$textStringTokens; $targets = $this->string_tokens; @@ -361,7 +362,7 @@ protected function process_css_style( $stackPtr ) { $opener = $this->phpcsFile->findPrevious( T_OPEN_CURLY_BRACKET, $stackPtr ); if ( false !== $opener ) { for ( $i = ( $opener - 1 ); $i >= 0; $i-- ) { - if ( isset( PHP_CodeSniffer_Tokens::$commentTokens[ $this->tokens[ $i ]['code'] ] ) + if ( isset( Tokens::$commentTokens[ $this->tokens[ $i ]['code'] ] ) || T_CLOSE_CURLY_BRACKET === $this->tokens[ $i ]['code'] ) { break; diff --git a/WordPress/Sniffs/VIP/CronIntervalSniff.php b/WordPress/Sniffs/VIP/CronIntervalSniff.php index 1c30f47821..aa6309fa75 100644 --- a/WordPress/Sniffs/VIP/CronIntervalSniff.php +++ b/WordPress/Sniffs/VIP/CronIntervalSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\VIP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Flag cron schedules less than 15 minutes. @@ -78,7 +79,7 @@ public function process_token( $stackPtr ) { } // Detect callback function name. - $callbackArrayPtr = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true ); + $callbackArrayPtr = $this->phpcsFile->findNext( Tokens::$emptyTokens, $callback['start'], ( $callback['end'] + 1 ), true ); // If callback is array, get second element. if ( false !== $callbackArrayPtr diff --git a/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php b/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php index a7aa844467..c5d1e7c36c 100644 --- a/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php +++ b/WordPress/Sniffs/VIP/DirectDatabaseQuerySniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\VIP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Flag Database direct queries. @@ -152,7 +153,7 @@ public function process_token( $stackPtr ) { // Check for Database Schema Changes. $_pos = $stackPtr; - while ( $_pos = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$textStringTokens, ( $_pos + 1 ), $endOfStatement, false, null, true ) ) { + while ( $_pos = $this->phpcsFile->findNext( Tokens::$textStringTokens, ( $_pos + 1 ), $endOfStatement, false, null, true ) ) { if ( preg_match( '#\b(?:ALTER|CREATE|DROP)\b#i', $this->tokens[ $_pos ]['content'] ) > 0 ) { $this->phpcsFile->addError( 'Attempting a database schema change is highly discouraged.', $_pos, 'SchemaChange' ); } diff --git a/WordPress/Sniffs/Variables/GlobalVariablesSniff.php b/WordPress/Sniffs/Variables/GlobalVariablesSniff.php index 68386b7c4f..9ec1db806e 100644 --- a/WordPress/Sniffs/Variables/GlobalVariablesSniff.php +++ b/WordPress/Sniffs/Variables/GlobalVariablesSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Variables; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Warns about overwriting WordPress native global variables. @@ -48,7 +49,7 @@ public function process_token( $stackPtr ) { $token = $this->tokens[ $stackPtr ]; if ( T_VARIABLE === $token['code'] && '$GLOBALS' === $token['content'] ) { - $bracketPtr = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + $bracketPtr = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); if ( false === $bracketPtr || T_OPEN_SQUARE_BRACKET !== $this->tokens[ $bracketPtr ]['code'] || ! isset( $this->tokens[ $bracketPtr ]['bracket_closer'] ) ) { return; @@ -145,7 +146,7 @@ public function process_token( $stackPtr ) { && in_array( $this->tokens[ $ptr ]['content'], $search, true ) ) { // Don't throw false positives for static class properties. - $previous = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $ptr - 1 ), null, true, null, true ); + $previous = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $ptr - 1 ), null, true, null, true ); if ( false !== $previous && T_DOUBLE_COLON === $this->tokens[ $previous ]['code'] ) { continue; } diff --git a/WordPress/Sniffs/WP/CapitalPDangitSniff.php b/WordPress/Sniffs/WP/CapitalPDangitSniff.php index b2ead93d9f..ca42817f48 100644 --- a/WordPress/Sniffs/WP/CapitalPDangitSniff.php +++ b/WordPress/Sniffs/WP/CapitalPDangitSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Capital P Dangit! @@ -191,9 +192,9 @@ public function process_token( $stackPtr ) { // Ignore any text strings which are array keys `$var['key']` as this is a false positive in 80% of all cases. if ( T_CONSTANT_ENCAPSED_STRING === $this->tokens[ $stackPtr ]['code'] ) { - $prevToken = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); + $prevToken = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); if ( false !== $prevToken && T_OPEN_SQUARE_BRACKET === $this->tokens[ $prevToken ]['code'] ) { - $nextToken = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); + $nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); if ( false !== $nextToken && T_CLOSE_SQUARE_BRACKET === $this->tokens[ $nextToken ]['code'] ) { return; } diff --git a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php index ea9af15b9f..965a28abe7 100644 --- a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php +++ b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Makes sure scripts and styles are enqueued and not explicitly echo'd. @@ -30,7 +31,7 @@ class EnqueuedResourcesSniff extends Sniff { * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$textStringTokens; + return Tokens::$textStringTokens; } /** diff --git a/WordPress/Sniffs/WP/I18nSniff.php b/WordPress/Sniffs/WP/I18nSniff.php index 06536475d4..64e2102752 100644 --- a/WordPress/Sniffs/WP/I18nSniff.php +++ b/WordPress/Sniffs/WP/I18nSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WP; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Makes sure WP internationalization functions are used properly. @@ -178,7 +179,7 @@ public function process_token( $stack_ptr ) { for ( $i = ( $func_open_paren_token + 1 ); $i < $this->tokens[ $func_open_paren_token ]['parenthesis_closer']; $i++ ) { $this_token = $this->tokens[ $i ]; $this_token['token_index'] = $i; - if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $this_token['code'] ] ) ) { + if ( isset( Tokens::$emptyTokens[ $this_token['code'] ] ) ) { continue; } if ( T_COMMA === $this_token['code'] ) { @@ -188,7 +189,7 @@ public function process_token( $stack_ptr ) { } // Merge consecutive single or double quoted strings (when they span multiple lines). - if ( isset( PHP_CodeSniffer_Tokens::$textStringTokens[ $this_token['code'] ] ) ) { + if ( isset( Tokens::$textStringTokens[ $this_token['code'] ] ) ) { for ( $j = ( $i + 1 ); $j < $this->tokens[ $func_open_paren_token ]['parenthesis_closer']; $j++ ) { if ( $this_token['code'] === $this->tokens[ $j ]['code'] ) { $this_token['content'] .= $this->tokens[ $j ]['content']; @@ -526,7 +527,7 @@ protected function check_for_translator_comment( $stack_ptr, $args ) { continue; } - $previous_comment = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$commentTokens, ( $stack_ptr - 1 ) ); + $previous_comment = $this->phpcsFile->findPrevious( Tokens::$commentTokens, ( $stack_ptr - 1 ) ); if ( false !== $previous_comment ) { /* diff --git a/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php index f5c3466932..75b420145f 100755 --- a/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/CastStructureSpacingSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WhiteSpace; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Ensure cast statements don't contain whitespace, but *are* surrounded by whitespace, based upon Squiz code. @@ -32,7 +33,7 @@ class CastStructureSpacingSniff extends Sniff { * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$castTokens; + return Tokens::$castTokens; } /** diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 88ad2be200..e923fd3877 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WhiteSpace; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Enforces spacing around logical operators and assignments, based upon Squiz code. @@ -165,7 +166,7 @@ public function process_token( $stackPtr ) { } } - $parenthesisOpener = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + $parenthesisOpener = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); // If this is a function declaration. if ( T_FUNCTION === $this->tokens[ $stackPtr ]['code'] ) { @@ -178,7 +179,7 @@ public function process_token( $stackPtr ) { // This function returns by reference (function &function_name() {}). $parenthesisOpener = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $parenthesisOpener + 1 ), null, true @@ -188,7 +189,7 @@ public function process_token( $stackPtr ) { if ( isset( $function_name_ptr ) ) { $parenthesisOpener = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $parenthesisOpener + 1 ), null, true @@ -216,7 +217,7 @@ public function process_token( $stackPtr ) { if ( isset( $this->tokens[ $parenthesisOpener ]['parenthesis_closer'] ) ) { $usePtr = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $this->tokens[ $parenthesisOpener ]['parenthesis_closer'] + 1 ), null, true, @@ -330,7 +331,7 @@ public function process_token( $stackPtr ) { $this->phpcsFile->fixer->addContentBefore( $parenthesisCloser, ' ' ); } } elseif ( ' ' !== $this->tokens[ ( $parenthesisCloser - 1 ) ]['content'] ) { - $prevNonEmpty = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $parenthesisCloser - 1 ), null, true ); + $prevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $parenthesisCloser - 1 ), null, true ); if ( $this->tokens[ ( $parenthesisCloser ) ]['line'] === $this->tokens[ ( $prevNonEmpty + 1 ) ]['line'] ) { $error = 'Expected exactly one space before closing parenthesis; "%s" found.'; $fix = $this->phpcsFile->addFixableError( @@ -435,7 +436,7 @@ public function process_token( $stackPtr ) { if ( $firstContent !== $scopeCloser ) { $lastContent = $this->phpcsFile->findPrevious( T_WHITESPACE, ( $scopeCloser - 1 ), null, true ); - $lastNonEmptyContent = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $scopeCloser - 1 ), null, true ); + $lastNonEmptyContent = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $scopeCloser - 1 ), null, true ); $checkToken = $lastContent; if ( isset( $this->tokens[ $lastNonEmptyContent ]['scope_condition'] ) ) { @@ -485,7 +486,7 @@ public function process_token( $stackPtr ) { if ( T_COMMENT === $this->tokens[ $trailingContent ]['code'] ) { // Special exception for code where the comment about // an ELSE or ELSEIF is written between the control structures. - $nextCode = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $scopeCloser + 1 ), null, true ); + $nextCode = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $scopeCloser + 1 ), null, true ); if ( T_ELSE === $this->tokens[ $nextCode ]['code'] || T_ELSEIF === $this->tokens[ $nextCode ]['code'] ) { $trailingContent = $nextCode; diff --git a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 9f5376354f..25cc4c390d 100644 --- a/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WhiteSpace; use Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff as PHPCS_Squiz_OperatorSpacingSniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Verify operator spacing, uses the Squiz sniff, but additionally also sniffs for the `!` (boolean not) operator. @@ -53,7 +54,7 @@ class OperatorSpacingSniff extends PHPCS_Squiz_OperatorSpacingSniff { public function register() { $tokens = parent::register(); $tokens[ T_BOOLEAN_NOT ] = T_BOOLEAN_NOT; - $logical_operators = PHP_CodeSniffer_Tokens::$booleanOperators; + $logical_operators = Tokens::$booleanOperators; // Using array union to auto-dedup. return $tokens + $logical_operators; diff --git a/WordPress/Sniffs/XSS/EscapeOutputSniff.php b/WordPress/Sniffs/XSS/EscapeOutputSniff.php index 7af4d77ff5..eb6ac8fe02 100644 --- a/WordPress/Sniffs/XSS/EscapeOutputSniff.php +++ b/WordPress/Sniffs/XSS/EscapeOutputSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\XSS; use WordPress\Sniff; +use PHP_CodeSniffer_Tokens as Tokens; /** * Verifies that all outputted strings are escaped. @@ -198,7 +199,7 @@ public function process_token( $stackPtr ) { $function = $this->tokens[ $stackPtr ]['content']; // Find the opening parenthesis (if present; T_ECHO might not have it). - $open_paren = $this->phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + $open_paren = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); // If function, not T_ECHO nor T_PRINT. if ( T_STRING === $this->tokens[ $stackPtr ]['code'] ) { @@ -260,7 +261,7 @@ public function process_token( $stackPtr ) { if ( ! isset( $end_of_statement ) ) { $end_of_statement = $this->phpcsFile->findNext( array( T_SEMICOLON, T_CLOSE_TAG ), $stackPtr ); - $last_token = $this->phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $end_of_statement - 1 ), null, true ); + $last_token = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $end_of_statement - 1 ), null, true ); // Check for the ternary operator. We only need to do this here if this // echo is lacking parenthesis. Otherwise it will be handled below. @@ -286,7 +287,7 @@ public function process_token( $stackPtr ) { for ( $i = $stackPtr; $i < $end_of_statement; $i++ ) { // Ignore whitespaces and comments. - if ( isset( PHP_CodeSniffer_Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) { + if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) ) { continue; } @@ -388,7 +389,7 @@ public function process_token( $stackPtr ) { // Get the first parameter (name of function being used on the array). $mapped_function = $this->phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, + Tokens::$emptyTokens, ( $function_opener + 1 ), $this->tokens[ $function_opener ]['parenthesis_closer'], true From 744fc5907ae110261dfc264aee33963583012f60 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 12:16:36 +0200 Subject: [PATCH 10/37] PHPCS 3.x compat: Make sure checks for PHP native functions/etc are done in the global namespace --- .../NamingConventions/PrefixAllGlobalsSniff.php | 12 ++++++------ .../NamingConventions/PrefixAllGlobalsUnitTest.php | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index ad0ec98481..c16a9a21b9 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -207,7 +207,7 @@ public function process_token( $stackPtr ) { } $item_name = $this->phpcsFile->getDeclarationName( $stackPtr ); - if ( function_exists( $item_name ) ) { + if ( function_exists( '\\' . $item_name ) ) { // Backfill for PHP native function. return; } @@ -234,14 +234,14 @@ public function process_token( $stackPtr ) { switch ( $this->tokens[ $stackPtr ]['type'] ) { case 'T_CLASS': - if ( class_exists( $item_name ) ) { + if ( class_exists( '\\' . $item_name ) ) { // Backfill for PHP native class. return; } break; case 'T_INTERFACE': - if ( interface_exists( $item_name ) ) { + if ( interface_exists( '\\' . $item_name ) ) { // Backfill for PHP native interface. return; } @@ -251,7 +251,7 @@ public function process_token( $stackPtr ) { break; case 'T_TRAIT': - if ( function_exists( 'trait_exists' ) && trait_exists( $item_name ) ) { + if ( function_exists( '\trait_exists' ) && trait_exists( '\\' . $item_name ) ) { // Backfill for PHP native trait. return; } @@ -280,7 +280,7 @@ public function process_token( $stackPtr ) { } $item_name = $this->tokens[ $constant_name_ptr ]['content']; - if ( defined( $item_name ) ) { + if ( defined( '\\' . $item_name ) ) { // Backfill for PHP native constant. return; } @@ -620,7 +620,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p } if ( 'define' === $matched_content ) { - if ( defined( $raw_content ) ) { + if ( defined( '\\' . $raw_content ) ) { // Backfill for PHP native constant. return; } diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php index 48433e6698..3d24c48125 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php @@ -53,10 +53,10 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) { 154 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. 155 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. // Backfills. - 225 => ( function_exists( 'mb_strpos' ) ) ? 0 : 1, - 230 => ( function_exists( 'array_column' ) ) ? 0 : 1, - 234 => ( defined( 'E_DEPRECATED' ) ) ? 0 : 1, - 238 => ( class_exists( 'IntlTimeZone' ) ) ? 0 : 1, + 225 => ( function_exists( '\mb_strpos' ) ) ? 0 : 1, + 230 => ( function_exists( '\array_column' ) ) ? 0 : 1, + 234 => ( defined( '\E_DEPRECATED' ) ) ? 0 : 1, + 238 => ( class_exists( '\IntlTimeZone' ) ) ? 0 : 1, ); case 'PrefixAllGlobalsUnitTest.1.inc': From 51cdbb3fc67fc02553ee28d209e8311adebf5518 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 21 Jul 2017 00:26:47 +0200 Subject: [PATCH 11/37] PHPCS 3.x compat: Add helper class & implement use A number of PHPCS 2.x classes have been split out over several classes in PHPCS 3.x. Aliasing the new classname to the old would therefore not be a good idea. This helper class acts as a compatibility layer for these cases. The helper class only addresses the most common functionality needed and is by no means exhaustive. The helper class contains one function which is currently not (yet) used `get_version()`. This method was added in anticipation of continuing support for both PHPCS 3.x as well as 2.x for another year. PHPCS 3.x is likely to bring functionality which will not be available in PHPCS 2.x, so being able to retrieve the PHPCS version will, at some point, probably be helpful. N.B. The class name for this class has to be in CamelCaps (without underscore) as otherwise the PHPCS 2.x autoloader can not find it. --- WordPress/PHPCSHelper.php | 115 ++++++++++++++++++ .../Sniffs/Arrays/ArrayIndentationSniff.php | 9 +- .../PrefixAllGlobalsSniff.php | 3 +- WordPress/Sniffs/WP/I18nSniff.php | 3 +- .../WhiteSpace/DisallowInlineTabsSniff.php | 9 +- WordPress/Tests/WP/I18nUnitTest.php | 6 +- 6 files changed, 127 insertions(+), 18 deletions(-) create mode 100644 WordPress/PHPCSHelper.php diff --git a/WordPress/PHPCSHelper.php b/WordPress/PHPCSHelper.php new file mode 100644 index 0000000000..8817900680 --- /dev/null +++ b/WordPress/PHPCSHelper.php @@ -0,0 +1,115 @@ +config->tabWidth ) && $phpcsFile->config->tabWidth > 0 ) { + $tab_width = $phpcsFile->config->tabWidth; + } + } else { + // PHPCS 2.x. + $cli_values = $phpcsFile->phpcs->cli->getCommandLineValues(); + if ( isset( $cli_values['tabWidth'] ) && $cli_values['tabWidth'] > 0 ) { + $tab_width = $cli_values['tabWidth']; + } + } + + return $tab_width; + } + +} diff --git a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php index b9de09f59b..e8c04b14ee 100644 --- a/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayIndentationSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\Arrays; use WordPress\Sniff; +use WordPress\PHPCSHelper; use PHP_CodeSniffer_Tokens as Tokens; /** @@ -89,13 +90,7 @@ public function register() { */ public function process_token( $stackPtr ) { if ( ! isset( $this->tab_width ) ) { - $cli_values = $this->phpcsFile->phpcs->cli->getCommandLineValues(); - if ( ! isset( $cli_values['tabWidth'] ) || 0 === $cli_values['tabWidth'] ) { - // We have no idea how wide tabs are, so assume 4 spaces for fixing. - $this->tab_width = 4; - } else { - $this->tab_width = $cli_values['tabWidth']; - } + $this->tab_width = PHPCSHelper::get_tab_width( $this->phpcsFile ); } /* diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index c16a9a21b9..0cb2269248 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\NamingConventions; use WordPress\AbstractFunctionParameterSniff; +use WordPress\PHPCSHelper; use PHP_CodeSniffer_Tokens as Tokens; /** @@ -156,7 +157,7 @@ public function process_token( $stackPtr ) { } // Allow overruling the prefixes set in a ruleset via the command line. - $cl_prefixes = trim( PHP_CodeSniffer::getConfigData( 'prefixes' ) ); + $cl_prefixes = trim( PHPCSHelper::get_config_data( 'prefixes' ) ); if ( ! empty( $cl_prefixes ) ) { $this->prefixes = $cl_prefixes; } diff --git a/WordPress/Sniffs/WP/I18nSniff.php b/WordPress/Sniffs/WP/I18nSniff.php index 64e2102752..d33714ba57 100644 --- a/WordPress/Sniffs/WP/I18nSniff.php +++ b/WordPress/Sniffs/WP/I18nSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WP; use WordPress\Sniff; +use WordPress\PHPCSHelper; use PHP_CodeSniffer_Tokens as Tokens; /** @@ -146,7 +147,7 @@ public function process_token( $stack_ptr ) { $token = $this->tokens[ $stack_ptr ]; // Allow overruling the text_domain set in a ruleset via the command line. - $cl_text_domain = trim( PHP_CodeSniffer::getConfigData( 'text_domain' ) ); + $cl_text_domain = trim( PHPCSHelper::get_config_data( 'text_domain' ) ); if ( ! empty( $cl_text_domain ) ) { $this->text_domain = $cl_text_domain; } diff --git a/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php b/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php index f8555e550b..cf19c31854 100644 --- a/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php +++ b/WordPress/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php @@ -10,6 +10,7 @@ namespace WordPress\Sniffs\WhiteSpace; use WordPress\Sniff; +use WordPress\PHPCSHelper; /** * Enforces using spaces for mid-line alignment. @@ -50,13 +51,7 @@ public function register() { */ public function process_token( $stackPtr ) { if ( ! isset( $this->tab_width ) ) { - $cli_values = $this->phpcsFile->phpcs->cli->getCommandLineValues(); - if ( ! isset( $cli_values['tabWidth'] ) || 0 === $cli_values['tabWidth'] ) { - // We have no idea how wide tabs are, so assume 4 spaces for fixing. - $this->tab_width = 4; - } else { - $this->tab_width = $cli_values['tabWidth']; - } + $this->tab_width = PHPCSHelper::get_tab_width( $this->phpcsFile ); } $check_tokens = array( diff --git a/WordPress/Tests/WP/I18nUnitTest.php b/WordPress/Tests/WP/I18nUnitTest.php index a2de453114..edfc318a8e 100644 --- a/WordPress/Tests/WP/I18nUnitTest.php +++ b/WordPress/Tests/WP/I18nUnitTest.php @@ -7,6 +7,8 @@ * @license https://opensource.org/licenses/MIT MIT */ +use WordPress\PHPCSHelper; + /** * Unit test class for the I18n sniff. * @@ -20,14 +22,14 @@ class WordPress_Tests_WP_I18nUnitTest extends AbstractSniffUnitTest { */ protected function setUp() { parent::setUp(); - PHP_CodeSniffer::setConfigData( 'text_domain', 'my-slug,default', true ); + PHPCSHelper::set_config_data( 'text_domain', 'my-slug,default', true ); } /** * Reset the $groups property. */ protected function tearDown() { - PHP_CodeSniffer::setConfigData( 'text_domain', null, true ); + PHPCSHelper::set_config_data( 'text_domain', null, true ); parent::tearDown(); } From 3112d5a16598afe7ee25bd667a088a464a5ea651 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 20:37:32 +0200 Subject: [PATCH 12/37] PHPCS 3.x compat: Alias PHPCS 3.x classes to their PHPCS 2.x counterparts Notes: * The file is automatically loaded by PHPCS using the new `` option. This option, when encountered in the ruleset, is ignored by previous PHPCS versions, so can be used safely and will automatically make sure the file is only loaded for PHPCS 3.x. * The file is loaded after the PHPCS native autoloader has started, but before any sniffs are loaded making for a clean way to add the aliases and prevent fatal 'class not found' errors. * Each class alias statement has to be wrapped in a `class_exists` condition to prevent fatal `class already declared` errors when this ruleset is used in combination with other external ruleset(s) which use the same/similar methodology to create PHPCS cross-version compatibility. The `` ruleset directive was added in PHPCS 3.0.1 in response to issue squizlabs/PHP_CodeSniffer#1469 --- WordPress-Core/ruleset.xml | 2 ++ WordPress-Extra/ruleset.xml | 2 ++ WordPress-VIP/ruleset.xml | 2 ++ WordPress/PHPCSAliases.php | 50 +++++++++++++++++++++++++++++++++++++ WordPress/ruleset.xml | 4 ++- 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 WordPress/PHPCSAliases.php diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index f31faff851..d763d610e8 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -2,6 +2,8 @@ Non-controversial generally-agreed upon WordPress Coding Standards + ./../WordPress/PHPCSAliases.php + diff --git a/WordPress-Extra/ruleset.xml b/WordPress-Extra/ruleset.xml index 59cb2686bf..370dc23a5f 100644 --- a/WordPress-Extra/ruleset.xml +++ b/WordPress-Extra/ruleset.xml @@ -2,6 +2,8 @@ Best practices beyond core WordPress Coding Standards + ./../WordPress/PHPCSAliases.php + diff --git a/WordPress-VIP/ruleset.xml b/WordPress-VIP/ruleset.xml index bbe5c82359..23c386b6a6 100644 --- a/WordPress-VIP/ruleset.xml +++ b/WordPress-VIP/ruleset.xml @@ -2,6 +2,8 @@ WordPress VIP Coding Standards + ./../WordPress/PHPCSAliases.php + + /Test/AllTests.php + /Test/Standards/*.php + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000000..f5c2cf0ceb --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,8 @@ + + + From 3070fb27f4fa59c3cc097a2524bd57d6733c8d71 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 24 Jul 2017 17:57:20 +0200 Subject: [PATCH 16/37] PHPCS 3.x compat: Update readme, build script and config files Travis: * No longer test the build against PHP 5.2. * Only test against PHP 5.3 icw PHPCS 2.x. * Test all other PHP versions against both PHPCS 2.x as well as `master` (3.x). * Test the build against PHPCS 3.x using the `master` branch. * Removed allowance for build failures against `master`. * Adjusted phpunit command to allow for testing on both PHPCS 2.x as well as 3.x. Composer: * Add the minimum required PHP version. * Make the PHPCS requirement more specific and allow for PHPCS 3.x. "Own" PHPCS custom ruleset: * Enforce PSR1 namespaces. Readme: * Adjusted (minimum) requirements information. * Updated installation instructions. * Updated travis example code. Contributing * Updated unit test instructions. * Updated example sniff code. * Removed reference to potentially upstreaming WPCS to PHPCS as reason for using the PHPCS unit test suite. Let's be fair: the sniffs ought to be unit tested anyway, no matter what. * Update unit testing conventions example. The example was referring to a now deprecated sniff, so selected another sniff for the example code. --- .travis.yml | 44 +++++-------- CONTRIBUTING.md | 172 +++++++++++++++++++++++++++++++----------------- README.md | 15 +++-- bin/phpcs.xml | 3 + composer.json | 3 +- 5 files changed, 144 insertions(+), 93 deletions(-) diff --git a/.travis.yml b/.travis.yml index b31d43db6e..0ab146b878 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ dist: trusty cache: apt: true - + language: - php @@ -14,11 +14,12 @@ php: - 5.6 - 7.0 - 7.1 + - nightly env: - # Branch for patches against 2.x. `master` is now 3.x which WPCS is not (yet) compatible with. - - PHPCS_BRANCH=2.9 LINT=1 - # Tagged release + # `master` is now 3.x. + - PHPCS_BRANCH=master LINT=1 + # Lowest tagged release in the 2.x series with which WPCS is compatible. - PHPCS_BRANCH=2.9.0 matrix: @@ -31,38 +32,27 @@ matrix: apt: packages: - libxml2-utils - # Run against PHPCS 3.0. I just picked to run it against 5.6. - - php: 5.6 - env: PHPCS_BRANCH=master + # Run against HHVM and PHP nightly. - php: hhvm sudo: required - dist: trusty + dist: trusty group: edge - env: PHPCS_BRANCH=2.9 LINT=1 - - php: nightly - env: PHPCS_BRANCH=2.9 LINT=1 - # Test PHP 5.3 with short_open_tags set to On (is Off by default) - - php: 5.3 - env: PHPCS_BRANCH=2.9 SHORT_OPEN_TAGS=true - dist: precise + env: PHPCS_BRANCH=master LINT=1 + + # Test PHP 5.3 only against PHPCS 2.x as PHPCS 3.x has a minimum requirement of PHP 5.4. - php: 5.3 - env: PHPCS_BRANCH=2.9 + env: PHPCS_BRANCH=2.9 LINT=1 dist: precise + # Test PHP 5.3 with short_open_tags set to On (is Off by default) - php: 5.3 - env: PHPCS_BRANCH=2.9.0 - dist: precise - - php: 5.2 - env: PHPCS_BRANCH=2.9 - dist: precise - - php: 5.2 - env: PHPCS_BRANCH=2.9.0 + env: PHPCS_BRANCH=2.9.0 SHORT_OPEN_TAGS=true dist: precise + allow_failures: # Allow failures for unstable builds. - php: nightly - php: hhvm - - env: PHPCS_BRANCH=master before_install: - export XMLLINT_INDENT=" " @@ -82,8 +72,10 @@ script: # Lint the PHP files against parse errors. - if [[ "$LINT" == "1" ]]; then if find . -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; fi # Run the unit tests. - - if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then phpunit --filter WordPress /tmp/phpcs/tests/AllTests.php; fi - - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --filter WordPress /tmp/phpcs/tests/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then phpunit --filter WordPress $(pwd)/Test/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then phpunit --filter WordPress $PHPCS_DIR/tests/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --filter WordPress $(pwd)/Test/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --filter WordPress $PHPCS_DIR/tests/AllTests.php; fi # WordPress Coding Standards. # @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards # @link http://pear.php.net/package/PHP_CodeSniffer/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 30093967ec..ac4c14a776 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,11 +24,15 @@ When you introduce new `public` sniff properties, or your sniff extends a class Sometimes, a sniff will flag code which upon further inspection by a human turns out to be OK. If the sniff you are writing is susceptible to this, please consider adding the ability to [whitelist lines of code](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Whitelisting-code-which-flags-errors). -To this end, the `WordPress_Sniff::has_whitelist_comment()` method was introduced. +To this end, the `WordPress\Sniff::has_whitelist_comment()` method was introduced. Example usage: ```php -class WordPress_Sniffs_CSRF_NonceVerificationSniff extends WordPress_Sniff { +namespace WordPress\Sniffs\CSRF; + +use WordPress\Sniff; + +class NonceVerificationSniff extends Sniff { public function process_token( $stackPtr ) { @@ -48,92 +52,140 @@ When you introduce a new whitelist comment, please don't forget to update the [w # Unit Testing -TL;DR +## Pre-requisites +* WordPress-Coding-Standards +* PHP CodeSniffer 2.9.x or 3.x +* PHPUnit 4.x or 5.x -If you have installed `phpcs` and the WordPress-Coding-Standards as [noted in the README](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#how-to-use-this), then you can navigate to the directory where the `phpcs` repo is checked out and do: +The WordPress Coding Standards use the PHP CodeSniffer native unit test suite for unit testing the sniffs. -```sh -composer install -vendor/bin/phpunit --filter WordPress tests/AllTests.php +Presuming you have installed PHP CodeSniffer and the WordPress-Coding-Standards as [noted in the README](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#how-to-use-this), all you need now is `PHPUnit`. + +If you already have PHPUnit installed on your system: Congrats, you're all set. + +If not, you can navigate to the directory where the `PHP_CodeSniffer` repo is checked out and do `composer install` to install the `dev` dependencies. +Alternatively, you can [install PHPUnit](https://phpunit.de/manual/5.7/en/installation.html) as a PHAR file. + +## Before running the unit tests + +N.B.: _If you used Composer to install the WordPress Coding Standards, you can skip this step._ + +For the unit tests to work, you need to make sure PHPUnit can find your `PHP_CodeSniffer` install. + +The easiest way to do this is to add a `phpunit.xml` file to the root of your WPCS installation and set a `PHPCS_DIR` environment variable from within this file. Make sure to adjust the path to reflect your local setup. +```xml + + + + + ``` +## Running the unit tests + +The WordPress Coding Standards are compatible with both PHPCS 2.x as well as 3.x. This has some implications for running the unit tests. + +* Navigate to the directory in which you installed WPCS. +* To run the unit tests with PHPCS 3.x: + ```sh + phpunit --bootstrap="./Test/phpcs3-bootstrap.php" --filter WordPress /path/to/PHP_CodeSniffer/tests/AllTests.php + ``` +* To run the unit tests with PHPCS 2.x: + ```sh + phpunit --bootstrap="./Test/phpcs2-bootstrap.php" --filter WordPress ./Test/AllTests.php + ``` + Expected output: +``` +PHPUnit 4.8.19 by Sebastian Bergmann and contributors. -[![asciicast](https://asciinema.org/a/98078.png)](https://asciinema.org/a/98078) +Runtime: PHP 7.1.3 with Xdebug 2.5.1 +Configuration: /WordPressCS/phpunit.xml + +...................................................... -You can ignore any skipped tests as these are for `PHP_CodeSniffer` external tools. +Tests generated 558 unique error codes; 48 were fixable (8.6%) -The reason why we need to checkout from `PHP_CodeSniffer` git repo to run the tests is because -PEAR installation is intended for ready-to-use not for development. At some point `WordPress-Coding-Standards` -might be submitted to `PHP_CodeSniffer` repo and using their existing convention for unit tests -will eventually help them to test the code before merging in. +Time: 12.25 seconds, Memory: 24.00Mb + +OK (54 tests, 0 assertions) +``` + +[![asciicast](https://asciinema.org/a/98078.png)](https://asciinema.org/a/98078) ## Unit Testing conventions -If you see inside the `WordPress/Tests`, the structure mimics the `WordPress/Sniffs`. For example, -the `WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php` sniff has unit test class defined in -`WordPress/Tests/Arrays/ArrayDeclarationUnitTest.php` that check `WordPress/Tests/Arrays/ArrayDeclarationUnitTest.inc` -file. See the file naming convention? Lets take a look what inside `ArrayDeclarationUnitTest.php`: +If you look inside the `WordPress/Tests` subdirectory, you'll see the structure mimics the `WordPress/Sniffs` subdirectory structure. For example, the `WordPress/Sniffs/PHP/POSIXFunctionsSniff.php` sniff has its unit test class defined in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.php` which checks the `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc` test case file. See the file naming convention? + +Lets take a look at what's inside `POSIXFunctionsUnitTest.php`: ```php ... -class WordPress_Tests_Arrays_ArrayDeclarationUnitTest extends AbstractSniffUnitTest -{ - public function getErrorList() - { - return array( - 3 => 1, - 7 => 1, - 9 => 1, - 16 => 1, - 31 => 2, - ); - - }//end getErrorList() -} +namespace WordPress\Tests\PHP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + +class POSIXFunctionsUnitTest extends AbstractSniffUnitTest { + + /** + * Returns the lines where errors should occur. + * + * @return array => + */ + public function getErrorList() { + return array( + 13 => 1, + 16 => 1, + 18 => 1, + 20 => 1, + 22 => 1, + 24 => 1, + 26 => 1, + ); + + } ... ``` -Also note the class name convention. The method `getErrorList` MUST return an array of line numbers -indicating errors (when running `phpcs`) found in `WordPress/Tests/Arrays/ArrayDeclarationUnitTest.inc`. +Also note the class name convention. The method `getErrorList()` MUST return an array of line numbers indicating errors (when running `phpcs`) found in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc`. If you run: ```sh $ cd /path-to-cloned/phpcs -$ ./scripts/phpcs --standard=Wordpress -s CodeSniffer/Standards/WordPress/Tests/Arrays/ArrayDeclarationUnitTest.inc +$ ./bin/phpcs --standard=Wordpress -s /path/to/WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc --sniffs=WordPress.PHP.POSIXFunctions ... -------------------------------------------------------------------------------- -FOUND 8 ERROR(S) AND 2 WARNING(S) AFFECTING 6 LINE(S) +FOUND 7 ERRORS AFFECTING 7 LINES -------------------------------------------------------------------------------- - 3 | ERROR | Array keyword should be lower case; expected "array" but found - | | "Array" (WordPress.Arrays.ArrayDeclaration) - 7 | ERROR | There must be no space between the Array keyword and the - | | opening parenthesis (WordPress.Arrays.ArrayDeclaration) - 9 | ERROR | Empty array declaration must have no space between the - | | parentheses (WordPress.Arrays.ArrayDeclaration) - 12 | WARNING | No space after opening parenthesis of array is bad style - | | (WordPress.Arrays.ArrayDeclaration) - 12 | WARNING | No space before closing parenthesis of array is bad style - | | (WordPress.Arrays.ArrayDeclaration) - 16 | ERROR | Each line in an array declaration must end in a comma - | | (WordPress.Arrays.ArrayDeclaration) - 31 | ERROR | Expected 1 space between "'type'" and double arrow; 0 found - | | (WordPress.Arrays.ArrayDeclaration) - 31 | ERROR | Expected 1 space between double arrow and "'post'"; 0 found - | | (WordPress.Arrays.ArrayDeclaration) - 31 | ERROR | Expected 1 space before "=>"; 0 found - | | (WordPress.WhiteSpace.OperatorSpacing) - 31 | ERROR | Expected 1 space after "=>"; 0 found - | | (WordPress.WhiteSpace.OperatorSpacing) + 13 | ERROR | ereg() has been deprecated since PHP 5.3 and removed in PHP 7.0, + | | please use preg_match() instead. + | | (WordPress.PHP.POSIXFunctions.ereg_ereg) + 16 | ERROR | eregi() has been deprecated since PHP 5.3 and removed in PHP 7.0, + | | please use preg_match() instead. + | | (WordPress.PHP.POSIXFunctions.ereg_eregi) + 18 | ERROR | ereg_replace() has been deprecated since PHP 5.3 and removed in PHP + | | 7.0, please use preg_replace() instead. + | | (WordPress.PHP.POSIXFunctions.ereg_replace_ereg_replace) + 20 | ERROR | eregi_replace() has been deprecated since PHP 5.3 and removed in PHP + | | 7.0, please use preg_replace() instead. + | | (WordPress.PHP.POSIXFunctions.ereg_replace_eregi_replace) + 22 | ERROR | split() has been deprecated since PHP 5.3 and removed in PHP 7.0, + | | please use explode(), str_split() or preg_split() instead. + | | (WordPress.PHP.POSIXFunctions.split_split) + 24 | ERROR | spliti() has been deprecated since PHP 5.3 and removed in PHP 7.0, + | | please use explode(), str_split() or preg_split() instead. + | | (WordPress.PHP.POSIXFunctions.split_spliti) + 26 | ERROR | sql_regcase() has been deprecated since PHP 5.3 and removed in PHP + | | 7.0, please use preg_match() instead. + | | (WordPress.PHP.POSIXFunctions.ereg_sql_regcase) -------------------------------------------------------------------------------- .... ``` +You'll see the line number and number of ERRORs we need to return in the `getErrorList()` method. -You'll see the line number and number of ERRORs we need to return in `getErrorList` method. -In line #31 there are two ERRORs belong to `WordPress.WhiteSpace.OperatorSpacing` sniff and -it MUST not included in `ArrayDeclarationUnitTest` (that's why we only return 2 errros for line #31). -Also there's `getWarningList` method in unit test class that returns an array of line numbers -indicating WARNINGs. +The `--sniffs=...` directive limits the output to the sniff you are testing. ## Sniff Code Standards diff --git a/README.md b/README.md index b8b701db8b..d131ad2c10 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,8 @@ This project is a collection of [PHP_CodeSniffer](https://github.com/squizlabs/P ### Requirements -The WordPress Coding Standards require PHP 5.2 or higher and the [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version **2.9.0** or higher. -The WordPress Coding Standards are currently [not compatible with the upcoming PHPCS 3 release](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/718). +The WordPress Coding Standards require PHP 5.3 or higher and [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version **2.9.0** or higher. +As of version 0.13.0, the WordPress Coding Standards are compatible with PHPCS 3.0.2+. In that case, the minimum PHP requirement is PHP 5.4. ### Composer @@ -58,7 +58,7 @@ Running this command will: 3. Register WordPress standards in PHP_CodeSniffer configuration. 4. Make `phpcs` command available from `wpcs/vendor/bin`. -For the convenience of using `phpcs` as a global command, you may want to add the `wpcs/vendor/bin` path to a PATH environment in your operating system. +For the convenience of using `phpcs` as a global command, you may want to add the path to the `wpcs/vendor/scripts` (PHPCS 2.x) and/or `wpcs/vendor/bin` (PHPCS 3.x) directories to a `PATH` environment variable for your operating system. #### Installing WPCS as a dependency @@ -96,10 +96,13 @@ cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs cd phpcs +#PHPCS 2.x ./scripts/phpcs --config-set installed_paths ../wpcs +#PHPCS 3.x +./bin/phpcs --config-set installed_paths ../wpcs ``` -And then add the `~/projects/phpcs/scripts` directory to your `PATH` environment variable via your `.bashrc`. +And then add the `~/projects/phpcs/scripts` (PHPCS 2.x) or `~/projects/phpcs/bin` (PHPCS 3.x) directory to your `PATH` environment variable via your `.bashrc`. You should then see `WordPress-Core` et al listed when you run `phpcs -i`. @@ -241,7 +244,7 @@ before_install: # Install WordPress Coding Standards. - if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $SNIFFS_DIR; fi # Set install path for WordPress Coding Standards. - - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs --config-set installed_paths $SNIFFS_DIR; fi + - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/bin/phpcs --config-set installed_paths $SNIFFS_DIR; fi # After CodeSniffer install you should refresh your path. - if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi @@ -251,7 +254,7 @@ script: # for example: `--standard=wpcs.xml`. # You can use any of the normal PHPCS command line arguments in the command: # https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage - - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p . --standard=WordPress; fi + - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/bin/phpcs -p . --standard=WordPress; fi ``` diff --git a/bin/phpcs.xml b/bin/phpcs.xml index b39b2cf4ff..904cec2b93 100644 --- a/bin/phpcs.xml +++ b/bin/phpcs.xml @@ -13,4 +13,7 @@ + + + diff --git a/composer.json b/composer.json index a26ee8b553..1b5e513a83 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ } ], "require" : { - "squizlabs/php_codesniffer": "^2.9.0" + "php" : ">=5.3", + "squizlabs/php_codesniffer": "^2.9.0 || ^3.0.2" }, "suggest" : { "dealerdirect/phpcodesniffer-composer-installer": "*" From ab7cbd91aec8d3c13aa61bb5aa422459619d8e0e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 13:11:16 +0200 Subject: [PATCH 17/37] PHPCS 3.x compat: remove work-around for how PHP 5.2 tokenizes code --- WordPress/Sniff.php | 51 +++++++++++---------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index 5cb011120f..914ee899e8 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -2184,30 +2184,18 @@ public function get_declared_namespace_name( $stackPtr ) { /** * Check if a content string contains a specific html open tag. * - * {@internal For PHP 5.3+ this is straightforward, just check if $content - * contains the tag. - * PHP 5.2 however, creates a separate token for `text text` as: - * - T_INLINE_HTML 'text' - * - T_INLINE_HTML 'text text' - * - * We don't need to worry about checking the rest of the content of the next - * token as sniffs using this function will be sniffing for all text string - * tokens, so the next token will be passed to the sniff in the next iteration - * and checked then. - * Similarly, no need to check content before the ' open tag, false otherwise. */ - public function has_html_open_tag( $tag_name, $stackPtr, $content = false ) { - if ( false === $content ) { + public function has_html_open_tag( $tag_name, $stackPtr = null, $content = false ) { + if ( false === $content && isset( $stackPtr ) ) { $content = $this->tokens[ $stackPtr ]['content']; } - // Check for the open tag in normal string tokens and T_INLINE_HTML for PHP 5.3+. - if ( 's' !== $tag_name[0] || PHP_VERSION_ID >= 50300 || T_INLINE_HTML !== $this->tokens[ $stackPtr ]['code'] ) { - if ( false !== strpos( $content, '<' . $tag_name ) ) { - return true; - } - } elseif ( 'tokens[ $next_ptr ] ) - && T_INLINE_HTML === $this->tokens[ $next_ptr ]['code'] - && 0 === strpos( $this->tokens[ $next_ptr ]['content'], $rest_tag_name ) - ) { - return true; - } + if ( ! empty( $content ) && false !== strpos( $content, '<' . $tag_name ) ) { + return true; } return false; From c60ab46f8502ec54aaba4f9694fbde099932ebc7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 25 Jul 2017 02:19:10 +0200 Subject: [PATCH 18/37] :green_heart: Build: improve our own codestyle checking * Run the sniffs using a higher PHP and PHPCS version. * Minor simplification in the command to call phpcs. * Show warnings, but do not break the build for it. Previously warnings wouldn't be shown at all. * Move a number of command line options to the ruleset. * Exclude the Composer `vendor` directory if it exists. * Update the section in the `Contributing.md` regarding code style --- .travis.yml | 8 ++++---- CONTRIBUTING.md | 5 ++--- bin/phpcs.xml | 6 ++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0ab146b878..08f69892a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,9 +25,9 @@ env: matrix: fast_finish: true include: - # Run PHPCS against WPCS. I just picked to run it against 5.5. - - php: 5.5 - env: PHPCS_BRANCH=2.9 SNIFF=1 + # Run PHPCS against WPCS. I just picked to run it against 7.0. + - php: 7.0 + env: PHPCS_BRANCH=master SNIFF=1 addons: apt: packages: @@ -85,7 +85,7 @@ script: # -n flag: Do not print warnings. (shortcut for --warning-severity=0) # --standard: Use WordPress as the standard. # --extensions: Only sniff PHP files. - - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p -s -n . --standard=./bin/phpcs.xml --extensions=php; fi + - if [[ "$SNIFF" == "1" ]]; then $PHPCS_BIN . --standard=./bin/phpcs.xml --runtime-set ignore_warnings_on_exit 1; fi # Validate the xml files. # @link http://xmlsoft.org/xmllint.html - if [[ "$SNIFF" == "1" ]]; then xmllint --noout ./*/ruleset.xml; fi diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ac4c14a776..0612aec828 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -187,7 +187,6 @@ You'll see the line number and number of ERRORs we need to return in the `getErr The `--sniffs=...` directive limits the output to the sniff you are testing. -## Sniff Code Standards - -The sniffs for WPCS should be written such that they pass the `WordPress-Core` code standards. +## Code Standards for this project +The sniffs and test files - not test _case_ files! - for WPCS should be written such that they pass the `WordPress-Extra` and the `WordPress-Docs` code standards using the custom ruleset as found in `/bin/phpcs.xml`. diff --git a/bin/phpcs.xml b/bin/phpcs.xml index 904cec2b93..7c03fbda03 100644 --- a/bin/phpcs.xml +++ b/bin/phpcs.xml @@ -2,10 +2,16 @@ The Coding standard for the WordPress Coding Standards itself. + + + /Test/AllTests.php /Test/Standards/*.php + + */vendor/* + From 6f42270e1b8131d507e3f4a68419895988c1a159 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 25 Jul 2017 02:22:13 +0200 Subject: [PATCH 19/37] :books: Readme: Update the example output ... to reflect the current state of the `wp-load.php` file in combination with the current WPCS sniffs. --- README.md | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d131ad2c10..9b951e8e26 100644 --- a/README.md +++ b/README.md @@ -155,25 +155,32 @@ Run the `phpcs` command line tool on a given file or directory, for example: Will result in following output: -------------------------------------------------------------------------------- - FOUND 8 ERRORS AND 2 WARNINGS AFFECTING 7 LINES + FOUND 10 ERRORS AND 5 WARNINGS AFFECTING 8 LINES -------------------------------------------------------------------------------- - 1 | ERROR | [x] End of line character is invalid; expected "\n" but found "\r\n" - 36 | ERROR | [x] Expected 1 spaces before closing bracket; 0 found - 41 | WARNING | [ ] Silencing errors is discouraged - 41 | WARNING | [ ] Silencing errors is discouraged - 48 | ERROR | [ ] Inline comments must end in full-stops, exclamation marks, or - | | question marks - 48 | ERROR | [x] There must be no blank line following an inline comment - 76 | ERROR | [ ] Inline comments must end in full-stops, exclamation marks, or - | | question marks - 92 | ERROR | [x] String "Create a Configuration File" does not require double - | | quotes; use single quotes instead - 94 | ERROR | [ ] Expected next thing to be an escaping function (see Codex for - | | 'Data Validation'), not '$die' - 94 | ERROR | [ ] Expected next thing to be an escaping function (see Codex for - | | 'Data Validation'), not '__' + 24 | WARNING | [ ] error_reporting() can lead to full path disclosure. + 24 | WARNING | [ ] error_reporting() found. Changing configuration at runtime + | | is rarely necessary. + 34 | ERROR | [x] Expected 1 spaces before closing bracket; 0 found + 39 | WARNING | [ ] Silencing errors is discouraged + 39 | WARNING | [ ] Silencing errors is discouraged + 46 | ERROR | [ ] Inline comments must end in full-stops, exclamation marks, + | | or question marks + 46 | ERROR | [x] There must be no blank line following an inline comment + 63 | WARNING | [ ] Detected access of super global var $_SERVER, probably + | | needs manual inspection. + 63 | ERROR | [ ] Detected usage of a non-validated input variable: $_SERVER + 63 | ERROR | [ ] Missing wp_unslash() before sanitization. + 63 | ERROR | [ ] Detected usage of a non-sanitized input variable: $_SERVER + 74 | ERROR | [ ] Inline comments must end in full-stops, exclamation marks, + | | or question marks + 90 | ERROR | [x] String "Create a Configuration File" does not require + | | double quotes; use single quotes instead + 92 | ERROR | [ ] Expected next thing to be an escaping function (see Codex + | | for 'Data Validation'), not '$die' + 92 | ERROR | [ ] Expected next thing to be an escaping function (see Codex + | | for 'Data Validation'), not '__' -------------------------------------------------------------------------------- - PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY + PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY -------------------------------------------------------------------------------- ### PhpStorm From 94c4d4016747589464fc4ba557051e16afca2590 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 25 Jul 2017 03:10:30 +0200 Subject: [PATCH 20/37] :books: Minor textual improvements to the Contributing.md document While rewriting the text in the `Contributing.md` document regarding the unit tests, I noticed some more room for improvement. --- CONTRIBUTING.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ac4c14a776..19445f1499 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,14 +1,25 @@ -# Upstream Issues +Hi, thank you for your interest in contributing to the WordPress Coding Standards! We look forward to working with you. -Since WPCS employs many sniffs that are part of PHPCS, sometimes an issue will be caused by a bug in PHPCS and not in WPCS itself. Before reporting a bug, you should check what sniff an error is coming from. Running `phpcs` with the `-s` flag, which will show the names of the sniffs with each error. If the error message in question doesn't come from a sniff whose name starts with `WordPress`, the issue is probably a bug in PHPCS itself, and should be [reported there](https://github.com/squizlabs/PHP_CodeSniffer/issues). +# Reporting Bugs -# Branches +Before reporting a bug, you should check what sniff an error is coming from. +Running `phpcs` with the `-s` flag will show the name of the sniff with each error. + +Bug reports containing a minimal code sample which can be used to reproduce the issue are highly appreciated as those are most easily actionable. + +## Upstream Issues + +Since WPCS employs many sniffs that are part of PHPCS, sometimes an issue will be caused by a bug in PHPCS and not in WPCS itself. If the error message in question doesn't come from a sniff whose name starts with `WordPress`, the issue is probably a bug in PHPCS itself, and should be [reported there](https://github.com/squizlabs/PHP_CodeSniffer/issues). + +# Contributing patches and new features + +## Branches Ongoing development will be done in the `develop` with merges done into `master` once considered stable. To contribute an improvement to this project, fork the repo and open a pull request to the `develop` branch. Alternatively, if you have push access to this repo, create a feature branch prefixed by `feature/` and then open an intra-repo PR from that branch to `develop`. -Once a commit is made to `develop`, a PR should be opened from `develop` into `master` and named "Next release". This PR will then serve provide a second round of Travis CI checks (especially for any hotfixes pushed directly to the `develop` branch), and provide collaborators with a forum to discuss the upcoming stable release. +Once a commit is made to `develop`, a PR should be opened from `develop` into `master` and named "Next release". This PR will provide collaborators with a forum to discuss the upcoming stable release. # Considerations when writing sniffs From 96dc7d0c1c4dad972298be2c72be1c15f29ab458 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 25 Jul 2017 03:14:41 +0200 Subject: [PATCH 21/37] :books: Minor textual improvements to the Readme.md document While updating the text in the `Readme.md` document regarding the PHPCS 3.x compatibility, I noticed some more room for improvement. --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d131ad2c10..0879a9fe44 100644 --- a/README.md +++ b/README.md @@ -72,11 +72,11 @@ It is strongly suggested to `require` one of these plugins in your project to ha ### Standalone -1. Install PHP_CodeSniffer by following its [installation instructions](https://github.com/squizlabs/PHP_CodeSniffer#installation) (via Composer, PEAR, or Git checkout). +1. Install PHP_CodeSniffer by following its [installation instructions](https://github.com/squizlabs/PHP_CodeSniffer#installation) (via Composer, Phar file, PEAR, or Git checkout). Do ensure that PHP_CodeSniffer's version matches our requirements(#requirements), if, for example, you're using VVV(https://github.com/Varying-Vagrant-Vagrants/VVV). -2. Clone WordPress standards repository: +2. Clone the WordPress standards repository: git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs @@ -110,25 +110,25 @@ You should then see `WordPress-Core` et al listed when you run `phpcs -i`. ### Standards subsets -The project encompasses a super–set of the sniffs that the WordPress community may need. If you use the `WordPress` standard you will get all the checks. Some of them might be unnecessary for your environment, for example, those specific to WordPress VIP coding requirements. +The project encompasses a super-set of the sniffs that the WordPress community may need. If you use the `WordPress` standard you will get all the checks. Some of them might be unnecessary for your environment, for example, those specific to WordPress VIP coding requirements. You can use the following as standard names when invoking `phpcs` to select sniffs, fitting your needs: -* `WordPress` — complete set with all of the sniffs in the project - - `WordPress-Core` — main ruleset for [WordPress core coding standards](http://make.wordpress.org/core/handbook/coding-standards/) - - `WordPress-Docs` — additional ruleset for [WordPress inline documentation standards](https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/) - - `WordPress-Extra` — extended ruleset for recommended best practices, not sufficiently covered in the WordPress core coding standards +* `WordPress` - complete set with all of the sniffs in the project + - `WordPress-Core` - main ruleset for [WordPress core coding standards](http://make.wordpress.org/core/handbook/coding-standards/) + - `WordPress-Docs` - additional ruleset for [WordPress inline documentation standards](https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/) + - `WordPress-Extra` - extended ruleset for recommended best practices, not sufficiently covered in the WordPress core coding standards - includes `WordPress-Core` - - `WordPress-VIP` — extended ruleset for [WordPress VIP coding requirements](http://vip.wordpress.com/documentation/code-review-what-we-look-for/) + - `WordPress-VIP` - extended ruleset for [WordPress VIP coding requirements](http://vip.wordpress.com/documentation/code-review-what-we-look-for/) - includes `WordPress-Core` ### Using a custom ruleset -If you need to further customize the selection of sniffs for your project — you can create a custom ruleset file. When you name this file either `phpcs.xml` or `phpcs.xml.dist`, PHP_CodeSniffer will automatically locate it as long as it is placed in the directory from which you run the CodeSniffer or in a directory above it. If you follow these naming conventions you don't have to supply a `--standard` arg. For more info, read about [using a default configuration file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file). See also provided [`phpcs.xml.dist.sample`](phpcs.xml.dist.sample) file and [fully annotated example](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml) in PHP_CodeSniffer documentation. +If you need to further customize the selection of sniffs for your project - you can create a custom ruleset file. When you name this file either `phpcs.xml` or `phpcs.xml.dist`, PHP_CodeSniffer will automatically locate it as long as it is placed in the directory from which you run the CodeSniffer or in a directory above it. If you follow these naming conventions you don't have to supply a `--standard` arg. For more info, read about [using a default configuration file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file). See also provided [`phpcs.xml.dist.sample`](phpcs.xml.dist.sample) file and [fully annotated example](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml) in the PHP CodeSniffer documentation. ### Customizing sniff behaviour -The WordPress Coding Standard contains a number of sniffs which are configurable. This means that you can turn parts of the sniff on or off, or change the behaviour by setting a property for the sniff in your custom `ruleset.xml` file. +The WordPress Coding Standard contains a number of sniffs which are configurable. This means that you can turn parts of the sniff on or off, or change the behaviour by setting a property for the sniff in your custom `phpcs.xml` file. You can find a complete list of all the properties you can change in the [wiki](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties). @@ -178,7 +178,7 @@ Will result in following output: ### PhpStorm -Please see “[PHP Code Sniffer with WordPress Coding Standards Integration](https://confluence.jetbrains.com/display/PhpStorm/WordPress+Development+using+PhpStorm#WordPressDevelopmentusingPhpStorm-PHPCodeSnifferwithWordPressCodingStandardsIntegrationinPhpStorm)” in PhpStorm documentation. +Please see "[PHP Code Sniffer with WordPress Coding Standards Integration](https://confluence.jetbrains.com/display/PhpStorm/WordPress+Development+using+PhpStorm#WordPressDevelopmentusingPhpStorm-PHPCodeSnifferwithWordPressCodingStandardsIntegrationinPhpStorm)" in the PhpStorm documentation. ### Sublime Text @@ -213,7 +213,7 @@ sublime-phpcs is insanely powerful, but if you'd prefer automatic linting, [Subl ### Visual Studio -Please see “[Setting up PHP CodeSniffer in Visual Studio Code](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code/)”, a tutorial by Tom McFarlin. +Please see "[Setting up PHP CodeSniffer in Visual Studio Code](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code/)", a tutorial by Tom McFarlin. ## Running your code through WPCS automatically using CI tools @@ -265,7 +265,7 @@ You can find information on how to deal with some of the more frequent issues in ## Contributing -See [CONTRIBUTING](CONTRIBUTING.md), including information about [unit testing](CONTRIBUTING.md#unit-testing). +See [CONTRIBUTING](CONTRIBUTING.md), including information about [unit testing](CONTRIBUTING.md#unit-testing) the standard. ## License From ea76a402679ac0caa2c4242e69717d80ebbe3e38 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 13:13:19 +0200 Subject: [PATCH 22/37] PHPCS 3.x compat: remove test skipping for PHP 5.2 Includes removal of two secondary test case files which were a work-around for unrecognized syntaxes in PHP 5.2. --- .../Classes/ClassInstantiationUnitTest.2.inc | 17 ----- .../ClassInstantiationUnitTest.2.inc.fixed | 17 ----- ...t.1.inc => ClassInstantiationUnitTest.inc} | 8 +++ ...d => ClassInstantiationUnitTest.inc.fixed} | 8 +++ .../Classes/ClassInstantiationUnitTest.php | 27 +------- .../Tests/DB/RestrictedClassesUnitTest.php | 9 --- WordPress/Tests/Files/FileNameUnitTest.php | 5 -- .../PrefixAllGlobalsUnitTest.php | 21 +------ .../Tests/VIP/AdminBarRemovalUnitTest.php | 8 +-- .../Tests/VIP/DirectDatabaseQueryUnitTest.php | 2 +- .../Tests/VIP/PluginMenuSlugUnitTest.php | 7 +-- .../Tests/VIP/RestrictedFunctionsUnitTest.php | 1 - .../Tests/WP/CapitalPDangitUnitTest.1.inc | 10 --- WordPress/Tests/WP/CapitalPDangitUnitTest.inc | 6 ++ .../Tests/WP/CapitalPDangitUnitTest.inc.fixed | 6 ++ WordPress/Tests/WP/CapitalPDangitUnitTest.php | 63 ++++++++----------- .../Tests/WP/EnqueuedResourcesUnitTest.php | 12 ++-- WordPress/Tests/WP/I18nUnitTest.php | 2 +- WordPress/Tests/WP/PreparedSQLUnitTest.php | 19 +----- .../ControlStructureSpacingUnitTest.php | 11 ---- WordPress/Tests/XSS/EscapeOutputUnitTest.php | 1 - 21 files changed, 74 insertions(+), 186 deletions(-) delete mode 100644 WordPress/Tests/Classes/ClassInstantiationUnitTest.2.inc delete mode 100644 WordPress/Tests/Classes/ClassInstantiationUnitTest.2.inc.fixed rename WordPress/Tests/Classes/{ClassInstantiationUnitTest.1.inc => ClassInstantiationUnitTest.inc} (92%) rename WordPress/Tests/Classes/{ClassInstantiationUnitTest.1.inc.fixed => ClassInstantiationUnitTest.inc.fixed} (92%) delete mode 100644 WordPress/Tests/WP/CapitalPDangitUnitTest.1.inc diff --git a/WordPress/Tests/Classes/ClassInstantiationUnitTest.2.inc b/WordPress/Tests/Classes/ClassInstantiationUnitTest.2.inc deleted file mode 100644 index 7b3038a5b6..0000000000 --- a/WordPress/Tests/Classes/ClassInstantiationUnitTest.2.inc +++ /dev/null @@ -1,17 +0,0 @@ - 1, 38 => 1, @@ -70,12 +53,8 @@ public function getErrorList( $testFile = '' ) { 80 => 1, 84 => 1, 85 => 1, - ); - - case 'ClassInstantiationUnitTest.2.inc': - return array( - 16 => 1, - 17 => 1, + 97 => 1, + 98 => 1, ); case 'ClassInstantiationUnitTest.js': diff --git a/WordPress/Tests/DB/RestrictedClassesUnitTest.php b/WordPress/Tests/DB/RestrictedClassesUnitTest.php index 75e2234125..4bf68b6c75 100644 --- a/WordPress/Tests/DB/RestrictedClassesUnitTest.php +++ b/WordPress/Tests/DB/RestrictedClassesUnitTest.php @@ -52,15 +52,6 @@ protected function tearDown() { parent::tearDown(); } - /** - * Skip this test on PHP 5.2 as otherwise testing the namespace resolving would fail. - * - * @return bool Whether to skip this test. - */ - protected function shouldSkipTest() { - return ( PHP_VERSION_ID < 50300 ); - } - /** * Returns the lines where errors should occur. * diff --git a/WordPress/Tests/Files/FileNameUnitTest.php b/WordPress/Tests/Files/FileNameUnitTest.php index edd38737cb..85a1f87aa2 100644 --- a/WordPress/Tests/Files/FileNameUnitTest.php +++ b/WordPress/Tests/Files/FileNameUnitTest.php @@ -97,11 +97,6 @@ protected function getTestFiles( $testFileBase ) { $sep = DIRECTORY_SEPARATOR; $test_files = glob( dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', GLOB_BRACE ); - // Adjust the expected results array for PHP 5.2 as PHP 5.2 does not recognize namespaces. - if ( PHP_VERSION_ID < 50300 ) { - $this->expected_results['test-sample-phpunit6.inc'] = 1; - } - if ( ! empty( $test_files ) ) { return $test_files; } diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php index 352f333ab4..108471574b 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php @@ -52,12 +52,6 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) { 40 => 1, 90 => 1, 91 => 1, - // Scoped. - 149 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. - 151 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. - 153 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. - 154 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. - 155 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, // PHPCS on PHP 5.2 does not recognize namespaces. // Backfills. 225 => ( function_exists( '\mb_strpos' ) ) ? 0 : 1, 230 => ( function_exists( '\array_column' ) ) ? 0 : 1, @@ -66,20 +60,7 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) { ); case 'PrefixAllGlobalsUnitTest.1.inc': - // Namespaced - all OK. - if ( PHP_VERSION_ID >= 50300 ) { - return array(); - } - - // PHPCS on PHP 5.2 does not recognize namespaces. - return array( - 9 => 1, - 11 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - ); - + // Namespaced - all OK, fall through to the default case. default: return array(); diff --git a/WordPress/Tests/VIP/AdminBarRemovalUnitTest.php b/WordPress/Tests/VIP/AdminBarRemovalUnitTest.php index 3e07aa6333..7adbae60df 100644 --- a/WordPress/Tests/VIP/AdminBarRemovalUnitTest.php +++ b/WordPress/Tests/VIP/AdminBarRemovalUnitTest.php @@ -43,9 +43,9 @@ public function getErrorList( $testFile = '' ) { 21 => 1, 26 => 1, 32 => 1, - 56 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. - 57 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. - 58 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. + 56 => 1, + 57 => 1, + 58 => 1, 68 => 1, 69 => 1, 70 => 1, @@ -60,7 +60,7 @@ public function getErrorList( $testFile = '' ) { case 'AdminBarRemovalUnitTest.css': return array( - 15 => 1, + 15 => 1, 16 => 1, 17 => 1, 22 => 1, diff --git a/WordPress/Tests/VIP/DirectDatabaseQueryUnitTest.php b/WordPress/Tests/VIP/DirectDatabaseQueryUnitTest.php index 0a45e1177e..41a8d786d2 100644 --- a/WordPress/Tests/VIP/DirectDatabaseQueryUnitTest.php +++ b/WordPress/Tests/VIP/DirectDatabaseQueryUnitTest.php @@ -41,7 +41,7 @@ public function getErrorList() { 190 => 1, 250 => 1, 257 => 1, - 274 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. + 274 => 1, ); } diff --git a/WordPress/Tests/VIP/PluginMenuSlugUnitTest.php b/WordPress/Tests/VIP/PluginMenuSlugUnitTest.php index e8b142712f..5a8307c2ce 100644 --- a/WordPress/Tests/VIP/PluginMenuSlugUnitTest.php +++ b/WordPress/Tests/VIP/PluginMenuSlugUnitTest.php @@ -28,10 +28,9 @@ class PluginMenuSlugUnitTest extends AbstractSniffUnitTest { */ public function getErrorList() { return array( - 3 => 1, - 5 => 1, - 9 => 2, - 14 => ( PHP_VERSION_ID < 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NS_SEPARATOR. + 3 => 1, + 5 => 1, + 9 => 2, ); } diff --git a/WordPress/Tests/VIP/RestrictedFunctionsUnitTest.php b/WordPress/Tests/VIP/RestrictedFunctionsUnitTest.php index 33d09b3f71..341e85e9e0 100644 --- a/WordPress/Tests/VIP/RestrictedFunctionsUnitTest.php +++ b/WordPress/Tests/VIP/RestrictedFunctionsUnitTest.php @@ -30,7 +30,6 @@ public function getErrorList() { return array( 3 => 1, 17 => 1, - 30 => ( PHP_VERSION_ID >= 50300 ) ? 0 : 1, 32 => 1, 34 => 1, 36 => 1, diff --git a/WordPress/Tests/WP/CapitalPDangitUnitTest.1.inc b/WordPress/Tests/WP/CapitalPDangitUnitTest.1.inc deleted file mode 100644 index 62e82673db..0000000000 --- a/WordPress/Tests/WP/CapitalPDangitUnitTest.1.inc +++ /dev/null @@ -1,10 +0,0 @@ - wordpress.pot + + wordpress.pot + + => */ - public function getWarningList( $testFile = 'CapitalPDangitUnitTest.inc' ) { + public function getWarningList() { - switch ( $testFile ) { - case 'CapitalPDangitUnitTest.inc': - return array( - 3 => 1, - 5 => 1, - 8 => 1, - 26 => 1, - 28 => 1, - 34 => 1, - 35 => 1, - 36 => 1, - 40 => 1, - 41 => 1, - 45 => 1, - 53 => 1, - 60 => 1, - 61 => 1, - 62 => 1, - 65 => 1, - 66 => 1, - 68 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 2, // PHPCS on PHP 5.2 apparently breaks the token up into two tokens. - 101 => 1, - 139 => 1, - 146 => 0, // False negative. - 173 => 1, - ); + return array( + 3 => 1, + 5 => 1, + 8 => 1, + 26 => 1, + 28 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 40 => 1, + 41 => 1, + 45 => 1, + 53 => 1, + 60 => 1, + 61 => 1, + 62 => 1, + 65 => 1, + 66 => 1, + 68 => 1, + 101 => 1, + 139 => 1, + 146 => 0, // False negative. + 173 => 1, + 181 => 1, + ); - case 'CapitalPDangitUnitTest.1.inc': - return array( - 9 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize nowdocs. - ); - - default: - return array(); - - } // End switch(). } } // End class. diff --git a/WordPress/Tests/WP/EnqueuedResourcesUnitTest.php b/WordPress/Tests/WP/EnqueuedResourcesUnitTest.php index d36bb6aafd..dd19200ca5 100644 --- a/WordPress/Tests/WP/EnqueuedResourcesUnitTest.php +++ b/WordPress/Tests/WP/EnqueuedResourcesUnitTest.php @@ -29,9 +29,9 @@ class EnqueuedResourcesUnitTest extends AbstractSniffUnitTest { public function getErrorList() { return array( 1 => 1, - 2 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 has a bug tokenizing inline HTML / ` 1, 6 => 1, - 7 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 has a bug tokenizing inline HTML / ` 1, 10 => 1, 11 => 1, 13 => 1, @@ -40,10 +40,10 @@ public function getErrorList() { 17 => 1, 20 => 1, 21 => 1, - 25 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize double quoted T_HEREDOC. - 26 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize double quoted T_HEREDOC. - 30 => 1, // PHPCS on PHP 5.2 does not recognize T_NOWDOC, but sees this as a literal string anyway. - 31 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. + 25 => 1, + 26 => 1, + 30 => 1, + 31 => 1, ); } diff --git a/WordPress/Tests/WP/I18nUnitTest.php b/WordPress/Tests/WP/I18nUnitTest.php index 7a44bebd69..0083094b60 100644 --- a/WordPress/Tests/WP/I18nUnitTest.php +++ b/WordPress/Tests/WP/I18nUnitTest.php @@ -106,7 +106,7 @@ public function getErrorList( $testFile = 'I18nUnitTest.inc' ) { 125 => 1, 128 => 1, 129 => 1, - 132 => ( PHP_VERSION_ID >= 50300 ) ? 1 : 2, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. + 132 => 1, 138 => 1, 143 => 1, 148 => 1, diff --git a/WordPress/Tests/WP/PreparedSQLUnitTest.php b/WordPress/Tests/WP/PreparedSQLUnitTest.php index 2b06196616..bccc30f1bb 100644 --- a/WordPress/Tests/WP/PreparedSQLUnitTest.php +++ b/WordPress/Tests/WP/PreparedSQLUnitTest.php @@ -29,7 +29,7 @@ class PreparedSQLUnitTest extends AbstractSniffUnitTest { * @return array => */ public function getErrorList() { - $errors = array( + return array( 3 => 1, 4 => 1, 5 => 1, @@ -44,23 +44,6 @@ public function getErrorList() { 64 => 1, 71 => 1, ); - - // Deal with PHP 5.2 not recognizing quoted heredoc openers, nor nowdoc syntax. - // These are all false positives! - if ( PHP_VERSION_ID < 50300 ) { - $errors[68] = 2; - $errors[69] = 2; - $errors[70] = 2; - $errors[71] = 4; - $errors[75] = 2; - $errors[76] = 7; - $errors[77] = 4; - $errors[78] = 5; - $errors[79] = 7; - $errors[80] = 1; - } - - return $errors; } /** diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php index 7c63a07c40..642f69c436 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php @@ -21,17 +21,6 @@ */ class ControlStructureSpacingUnitTest extends AbstractSniffUnitTest { - /** - * Skip this test on PHP 5.2. - * - * @since 0.9.0 - * - * @return bool Whether to skip this test. - */ - protected function shouldSkipTest() { - return ( PHP_VERSION_ID < 50300 ); - } - /** * Returns the lines where errors should occur. * diff --git a/WordPress/Tests/XSS/EscapeOutputUnitTest.php b/WordPress/Tests/XSS/EscapeOutputUnitTest.php index b5560247fe..0be554aab4 100644 --- a/WordPress/Tests/XSS/EscapeOutputUnitTest.php +++ b/WordPress/Tests/XSS/EscapeOutputUnitTest.php @@ -69,7 +69,6 @@ public function getErrorList() { 205 => 1, 206 => 1, 207 => 1, - 212 => ( PHP_VERSION_ID < 50300 ) ? 1 : 0, // PHPCS on PHP 5.2 does not recognize T_NOWDOC. 223 => 1, 225 => 1, 226 => 1, From 15307be48025c52bd0e10c473a2d9e39f8a72dd3 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 15 May 2017 15:23:34 +0200 Subject: [PATCH 23/37] PHPCS 3.x compat: remove work-arounds for compatibility with PHP 5.2 This should also allow the unit tests to pass against on PHP nightly. --- WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php | 4 +++- WordPress/Sniffs/WP/PreparedSQLSniff.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php b/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php index 04a5e8701f..704a16ebf6 100644 --- a/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php +++ b/WordPress/Sniffs/VIP/ValidatedSanitizedInputSniff.php @@ -95,7 +95,9 @@ public function process_token( $stackPtr ) { || T_HEREDOC === $this->tokens[ $stackPtr ]['code'] ) { $interpolated_variables = array_map( - create_function( '$symbol', 'return "$" . $symbol;' ), // Replace with closure when 5.3 is minimum requirement for PHPCS. + function ( $symbol ) { + return '$' . $symbol; + }, $this->get_interpolated_variables( $this->tokens[ $stackPtr ]['content'] ) ); foreach ( array_intersect( $interpolated_variables, $superglobals ) as $bad_variable ) { diff --git a/WordPress/Sniffs/WP/PreparedSQLSniff.php b/WordPress/Sniffs/WP/PreparedSQLSniff.php index e9fef1af06..b695d29ec3 100644 --- a/WordPress/Sniffs/WP/PreparedSQLSniff.php +++ b/WordPress/Sniffs/WP/PreparedSQLSniff.php @@ -139,7 +139,9 @@ public function process_token( $stackPtr ) { $bad_variables = array_filter( $this->get_interpolated_variables( $this->tokens[ $this->i ]['content'] ), - create_function( '$symbol', 'return ( $symbol !== "wpdb" );' ) // Replace this with closure once 5.3 is minimum requirement. + function ( $symbol ) { + return ( 'wpdb' !== $symbol ); + } ); foreach ( $bad_variables as $bad_variable ) { From cdd8a7e0e043aced139fb0ca8f5e867b43bd1a9e Mon Sep 17 00:00:00 2001 From: Barry Ceelen Date: Tue, 25 Jul 2017 12:34:00 +0200 Subject: [PATCH 24/37] Update .travis.yml example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d345b28b76..49453cc543 100644 --- a/README.md +++ b/README.md @@ -244,8 +244,8 @@ matrix: env: SNIFF=1 before_install: - - if [[ "$SNIFF" == "1" ]]; export PHPCS_DIR=/tmp/phpcs; fi - - if [[ "$SNIFF" == "1" ]]; export SNIFFS_DIR=/tmp/sniffs; fi + - if [[ "$SNIFF" == "1" ]]; then export PHPCS_DIR=/tmp/phpcs; fi + - if [[ "$SNIFF" == "1" ]]; then export SNIFFS_DIR=/tmp/sniffs; fi # Install PHP CodeSniffer. - if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR; fi # Install WordPress Coding Standards. From 857ba9b4e8008a620b33571b9bc79d5dc6722c29 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 25 Jul 2017 14:50:51 +0200 Subject: [PATCH 25/37] PHPCS 3.x compat: prevent conflicts with other standards. Fix a mixup between `class_exists()` and `interface_exists()`: * `PHP_CodeSniffer_Sniff` is the interface. * `PHP_CodeSniffer_File` is a class. --- WordPress/PHPCSAliases.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/WordPress/PHPCSAliases.php b/WordPress/PHPCSAliases.php index 052a066358..9a4fdaf2c9 100644 --- a/WordPress/PHPCSAliases.php +++ b/WordPress/PHPCSAliases.php @@ -25,15 +25,15 @@ */ if ( ! defined( 'WPCS_PHPCS_ALIASES_SET' ) ) { // PHPCS base classes/interface. - if ( ! interface_exists( '\PHP_CodeSniffer_File' ) ) { + if ( ! interface_exists( '\PHP_CodeSniffer_Sniff' ) ) { + class_alias( 'PHP_CodeSniffer\Sniffs\Sniff', '\PHP_CodeSniffer_Sniff' ); + } + if ( ! class_exists( '\PHP_CodeSniffer_File' ) ) { class_alias( 'PHP_CodeSniffer\Files\File', '\PHP_CodeSniffer_File' ); } if ( ! class_exists( '\PHP_CodeSniffer_Tokens' ) ) { class_alias( 'PHP_CodeSniffer\Util\Tokens', '\PHP_CodeSniffer_Tokens' ); } - if ( ! class_exists( '\PHP_CodeSniffer_Sniff' ) ) { - class_alias( 'PHP_CodeSniffer\Sniffs\Sniff', '\PHP_CodeSniffer_Sniff' ); - } // PHPCS classes which are being extended by WPCS sniffs. if ( ! class_exists( '\PHP_CodeSniffer_Standards_AbstractVariableSniff' ) ) { From 6dc72a5ccc4e9bc009f8deabcbac804a18083e56 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 22 Jul 2017 18:41:15 +0200 Subject: [PATCH 26/37] PHPCS 3.x compat: Implement recognition of the `--ignore-annotations` setting See issue 862 --- WordPress/PHPCSHelper.php | 26 ++++++++++++++++++++++++++ WordPress/Sniff.php | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/WordPress/PHPCSHelper.php b/WordPress/PHPCSHelper.php index 8817900680..9696183716 100644 --- a/WordPress/PHPCSHelper.php +++ b/WordPress/PHPCSHelper.php @@ -112,4 +112,30 @@ public static function get_tab_width( File $phpcsFile ) { return $tab_width; } + /** + * Check whether the `--ignore-annotations` option has been used. + * + * @since 0.13.0 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile Optional. The current file being processed. + * + * @return bool True if annotations should be ignored, false otherwise. + */ + public static function ignore_annotations( File $phpcsFile = null ) { + if ( class_exists( '\PHP_CodeSniffer\Config' ) ) { + // PHPCS 3.x. + if ( isset( $phpcsFile, $phpcsFile->config->annotations ) ) { + return ! $phpcsFile->config->annotations; + } else { + $annotations = \PHP_CodeSniffer\Config::getConfigData( 'annotations' ); + if ( isset( $annotations ) ) { + return ! $annotations; + } + } + } + + // PHPCS 2.x does not support `--ignore-annotations`. + return false; + } + } diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index 5cb011120f..c6ae169128 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -1066,6 +1066,11 @@ protected function get_last_ptr_on_line( $stackPtr ) { */ protected function has_whitelist_comment( $comment, $stackPtr ) { + // Respect the PHPCS 3.x --ignore-annotations setting. + if ( true === PHPCSHelper::ignore_annotations( $this->phpcsFile ) ) { + return false; + } + $lastPtr = $this->get_last_ptr_on_line( $stackPtr ); $end_of_line = $lastPtr; From e7f2af57f1d8dabb07d00395eca076b76f979310 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 26 Jul 2017 10:11:11 +0200 Subject: [PATCH 27/37] Update the pre-commit hook code This update presume that devs using this pre-commit hook will be using PHPCS 3.x. --- bin/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pre-commit b/bin/pre-commit index 24de124e3e..79e5b4231f 100755 --- a/bin/pre-commit +++ b/bin/pre-commit @@ -11,4 +11,4 @@ fi find . \( -name '*.php' \) -exec php -lf {} \; -phpunit --filter WordPress "$phpcs_root/tests/AllTests.php" +phpunit --bootstrap="./../Test/phpcs3-bootstrap.php" --filter WordPress "$phpcs_root/tests/AllTests.php" From eeee6cf27e805f168fd8f743f272d0838bff3595 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 26 Jul 2017 09:53:20 -0700 Subject: [PATCH 28/37] Update call to phpunit with required env var and fxed bootstrap path --- bin/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pre-commit b/bin/pre-commit index 79e5b4231f..4b3e665d70 100755 --- a/bin/pre-commit +++ b/bin/pre-commit @@ -11,4 +11,4 @@ fi find . \( -name '*.php' \) -exec php -lf {} \; -phpunit --bootstrap="./../Test/phpcs3-bootstrap.php" --filter WordPress "$phpcs_root/tests/AllTests.php" +PHPCS_DIR=$phpcs_root phpunit --bootstrap="./Test/phpcs3-bootstrap.php" --filter WordPress "$phpcs_root/tests/AllTests.php" From be1cd3bc49d27430112a14bb1336c2dc068a4e77 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 27 Jul 2017 13:19:35 +0200 Subject: [PATCH 29/37] Move `CONTRIBUTING.MD` to `.github` Helps keep the root directory clean of GH specific files. --- CONTRIBUTING.md => .github/CONTRIBUTING.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CONTRIBUTING.md => .github/CONTRIBUTING.md (100%) diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md From 45d841c7cef05175c319de8e2c2f97aa41af0a51 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 27 Jul 2017 14:27:00 +0200 Subject: [PATCH 30/37] Minor code style fix Old habits die hard ;-) --- Test/phpcs2-bootstrap.php | 2 +- Test/phpcs3-bootstrap.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/phpcs2-bootstrap.php b/Test/phpcs2-bootstrap.php index 2a0cb3c7cb..16da9abc0d 100644 --- a/Test/phpcs2-bootstrap.php +++ b/Test/phpcs2-bootstrap.php @@ -21,7 +21,7 @@ if ( false === $phpcsDir && is_dir( dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer' ) ) { $phpcsDir = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer'; -} else if ( false !== $phpcsDir ) { +} elseif ( false !== $phpcsDir ) { $phpcsDir = realpath( $phpcsDir ); } diff --git a/Test/phpcs3-bootstrap.php b/Test/phpcs3-bootstrap.php index e01cf155de..eb56399a59 100644 --- a/Test/phpcs3-bootstrap.php +++ b/Test/phpcs3-bootstrap.php @@ -27,7 +27,7 @@ // This may be a Composer install. if ( false === $phpcsDir && is_dir( dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer' ) ) { $phpcsDir = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer'; -} else if ( false !== $phpcsDir ) { +} elseif ( false !== $phpcsDir ) { $phpcsDir = realpath( $phpcsDir ); } From e4c86d718ca54b3e73ae89ee0fee2f734a131088 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 27 Jul 2017 15:39:54 +0200 Subject: [PATCH 31/37] Adjust the URL to the contributing file where relevant --- README.md | 2 +- Test/bootstrap.php | 2 +- Test/phpcs2-bootstrap.php | 2 +- Test/phpcs3-bootstrap.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 49453cc543..19ac98430b 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,7 @@ You can find information on how to deal with some of the more frequent issues in ## Contributing -See [CONTRIBUTING](CONTRIBUTING.md), including information about [unit testing](CONTRIBUTING.md#unit-testing) the standard. +See [CONTRIBUTING](.github/CONTRIBUTING.md), including information about [unit testing](.github/CONTRIBUTING.md#unit-testing) the standard. ## License diff --git a/Test/bootstrap.php b/Test/bootstrap.php index caceb95451..996c887ce3 100644 --- a/Test/bootstrap.php +++ b/Test/bootstrap.php @@ -29,7 +29,7 @@ phpunit --bootstrap="./Test/phpcs2-bootstrap.php" --filter WordPress ./Test/AllTests.php Please read the contributors guidelines for more information: -https://is.gd/WPCScontributing +https://is.gd/contributing2WPCS '; die( 1 ); diff --git a/Test/phpcs2-bootstrap.php b/Test/phpcs2-bootstrap.php index 2a0cb3c7cb..694e221cfa 100644 --- a/Test/phpcs2-bootstrap.php +++ b/Test/phpcs2-bootstrap.php @@ -35,7 +35,7 @@ pointing to the PHPCS directory. Please read the contributors guidelines for more information: -https://is.gd/WPCScontributing +https://is.gd/contributing2WPCS '; die( 1 ); diff --git a/Test/phpcs3-bootstrap.php b/Test/phpcs3-bootstrap.php index e01cf155de..ff48998900 100644 --- a/Test/phpcs3-bootstrap.php +++ b/Test/phpcs3-bootstrap.php @@ -42,7 +42,7 @@ pointing to the PHPCS directory. Please read the contributors guidelines for more information: -https://is.gd/WPCScontributing +https://is.gd/contributing2WPCS '; die( 1 ); From c33af153d885b9f0e3a897c94f84856fec86f063 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 27 Jul 2017 13:17:10 +0200 Subject: [PATCH 32/37] Add a .gitattributes file This PR adds a `.gitattributes` file to keep the archives GH creates of the repo clean of development related files. People using Composer can still get these files in their setup if they really want to, by using `--prefer-source`. Refs: * [Reddit: I don't need your tests in my production](https://www.reddit.com/r/PHP/comments/2jzp6k/i_dont_need_your_tests_in_my_production) * [Blog: I don't need your tests in my production](https://blog.madewithlove.be/post/gitattributes/) --- .gitattributes | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..3fb449f721 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,25 @@ +# +# Exclude these files from release archives. +# This will also make them unavailable when using Composer with `--prefer-dist`. +# If you develop for WPCS using Composer, use `--prefer-source`. +# https://blog.madewithlove.be/post/gitattributes/ +# +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/.github export-ignore +/bin export-ignore +/Test export-ignore +/WordPress/Tests export-ignore + +# +# Auto detect text files and perform LF normalization +# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ +# +* text=auto + +# +# The above will handle all files NOT found below +# +*.md text +*.php text +*.inc text From 1450f419759794b6b55fcdbcf1203763e39e725d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 27 Jul 2017 16:12:55 +0200 Subject: [PATCH 33/37] CONTRIBUTING: add note about using `--prefer-source` with Composer --- .github/CONTRIBUTING.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index cc7eddf2db..66e89a3a2a 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -72,6 +72,8 @@ The WordPress Coding Standards use the PHP CodeSniffer native unit test suite fo Presuming you have installed PHP CodeSniffer and the WordPress-Coding-Standards as [noted in the README](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#how-to-use-this), all you need now is `PHPUnit`. +N.B.: If you installed WPCS using Composer, make sure you used `--prefer-source` or run `composer install --prefer-source` now to make sure the unit tests are available. + If you already have PHPUnit installed on your system: Congrats, you're all set. If not, you can navigate to the directory where the `PHP_CodeSniffer` repo is checked out and do `composer install` to install the `dev` dependencies. From 1b54c0930e7804151b94500666d95f0fd234cb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Fri, 28 Jul 2017 10:12:22 +0200 Subject: [PATCH 34/37] Add issue template (#1072) Add issue template --- .github/issue_template.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/issue_template.md diff --git a/.github/issue_template.md b/.github/issue_template.md new file mode 100644 index 0000000000..f6e4f49872 --- /dev/null +++ b/.github/issue_template.md @@ -0,0 +1,11 @@ + \ No newline at end of file From 4f41c4b2827e56bc5fa3949062fe0cccb75f3c48 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 1 Aug 2017 17:14:40 +0200 Subject: [PATCH 35/37] Explicitly recommend v0.4.1+ of the DealerDirect Composer plugin Version 0.4.0 of the DealerDirect Composer plugin registers each standard individually as if they were root-directory standards, not a collection of standards which exposes a bug in PHPCS 3.x. Refs: * squizlabs/PHP_CodeSniffer/pull/1581 * DealerDirect/phpcodesniffer-composer-installer/issues/33 --- README.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 19ac98430b..9466fce082 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ For the convenience of using `phpcs` as a global command, you may want to add th When installing the WordPress Coding Standards as a dependency in a larger project, the above mentioned step 3 will not be executed automatically. There are two actively maintained Composer plugins which can handle the registration of standards with PHP_CodeSniffer for you: -* [composer-phpcodesniffer-standards-plugin](https://github.com/higidi/composer-phpcodesniffer-standards-plugin) +* [composer-phpcodesniffer-standards-plugin](https://github.com/higidi/composer-phpcodesniffer-standards-plugin):"^0.4.1" * [phpcodesniffer-composer-installer](https://github.com/DealerDirect/phpcodesniffer-composer-installer) It is strongly suggested to `require` one of these plugins in your project to handle the registration of external standards with PHPCS for you. diff --git a/composer.json b/composer.json index 1b5e513a83..9a5568fd5a 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "squizlabs/php_codesniffer": "^2.9.0 || ^3.0.2" }, "suggest" : { - "dealerdirect/phpcodesniffer-composer-installer": "*" + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1" }, "minimum-stability" : "RC", "support" : { From 76b4e1bb365d118f92f41b2b250cb5149464983e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 2 Aug 2017 00:00:02 +0200 Subject: [PATCH 36/37] Fix the version recommendation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9466fce082..92b076eb3e 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ For the convenience of using `phpcs` as a global command, you may want to add th When installing the WordPress Coding Standards as a dependency in a larger project, the above mentioned step 3 will not be executed automatically. There are two actively maintained Composer plugins which can handle the registration of standards with PHP_CodeSniffer for you: -* [composer-phpcodesniffer-standards-plugin](https://github.com/higidi/composer-phpcodesniffer-standards-plugin):"^0.4.1" -* [phpcodesniffer-composer-installer](https://github.com/DealerDirect/phpcodesniffer-composer-installer) +* [composer-phpcodesniffer-standards-plugin](https://github.com/higidi/composer-phpcodesniffer-standards-plugin) +* [phpcodesniffer-composer-installer](https://github.com/DealerDirect/phpcodesniffer-composer-installer):"^0.4.1" It is strongly suggested to `require` one of these plugins in your project to handle the registration of external standards with PHPCS for you. From aebfc8a87788e395c65d2d7f9721dcb48015b8a4 Mon Sep 17 00:00:00 2001 From: Juliette Date: Thu, 3 Aug 2017 03:51:21 +0200 Subject: [PATCH 37/37] Changelog for the 0.13.0 release. (#1058) Changelog for the 0.13.0 release. --- CHANGELOG.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e816e00ca..81b6d466e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,28 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C _Nothing yet._ +## [0.13.0] - 2017-08-03 + +### Added +- Support for PHP CodeSniffer 3.0.2+. The minimum required PHPCS version (2.9.0) stays the same. +- Support for the PHPCS 3 `--ignore-annotations` command line option. If you pass this option, both PHPCS native `@ignore ...` annotations as well as the WPCS specific [whitelist flags](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Whitelisting-code-which-flags-errors) will be ignored. + +### Changed +- The minimum required PHP version is now 5.3 when used in combination with PHPCS 2.x and PHP 5.4 when used in combination with PHPCS 3.x. +- The way the unit tests can be run is now slightly different for PHPCS 2.x versus 3.x. For more details, please refer to the updated information in the [Contributing Guidelines](CONTRIBUTING.md). +- Release archives will no longer contain the unit tests and other typical development files. You can still get these by using Composer with `--prefer-source` or by checking out a git clone of the repository. +- Various textual improvements to the Readme. +- Various textual improvements to the Contributing Guidelines. +- Minor internal changes. + +### Removed +- The `WordPress.Arrays.ArrayDeclaration` sniff has been deprecated. The last remaining checks this sniff contained have been moved to the `WordPress.Arrays.ArrayDeclarationSpacing` sniff. +- Work-arounds which were in place to support PHP 5.2. + +### Fixed +- A minor bug where the auto-fixer could accidentally remove a comment near an array opener. + + ## [0.12.0] - 2017-07-21 ### Added @@ -436,6 +458,7 @@ See the comparison for full list. Initial tagged release. [Unreleased]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/master...HEAD +[0.13.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/0.12.0...0.13.0 [0.12.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/0.11.0...0.12.0 [0.11.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/0.10.0...0.11.0 [0.10.0]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/compare/0.9.0...0.10.0