From ab7cbd91aec8d3c13aa61bb5aa422459619d8e0e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 13:11:16 +0200 Subject: [PATCH 1/3] 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 ea76a402679ac0caa2c4242e69717d80ebbe3e38 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 20 Jul 2017 13:13:19 +0200 Subject: [PATCH 2/3] 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 3/3] 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 ) {