Skip to content

Commit

Permalink
Switch to sirbrillig/phpcs-variable-analysis
Browse files Browse the repository at this point in the history
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

Requires ^2.8.3, since 2.6.3 – 2.8.2 had PHP 5.6 as the minimum version, but 2.8.3 changes it down to PHP 5.4 as the minimum.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
  • Loading branch information
GaryJones committed Jul 13, 2020
1 parent 6eb7b0c commit a896729
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 1,329 deletions.
20 changes: 10 additions & 10 deletions WordPress-VIP-Go/ruleset-test.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

$file = ''; $fp = ''; $dir = ''; $test = ''; $bar = ''; $array = []; $query_args = []; $url = ''; $query = ''; $page_title = ''; $true = true; $some_nasty_var = ''; $data = ''; $group = ''; $testing = ''; $needle = ''; $some_var = ''; $blogid = 1; $text = ''; $category_id = 123; $foo = ''; $bar = ''; $var = ''; $wp_rewrite = ''; $count = 1; $loop = 1; $a = ''; $b = ''; $obj = ''; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- All set for VariableAnalysis checks.
// WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_delete
delete( $file ); // Warning + Message.

Expand Down Expand Up @@ -163,7 +163,7 @@ rawurlencode(); // Ok.

// WordPress.PHP.DontExtract
extract( array( 'a' => 1 ) ); // Error.
$this->extract(); // Ok.
$obj->extract(); // Ok.

// WordPress.PHP.StrictComparisons.LooseComparison
true == $true; // Warning.
Expand All @@ -188,7 +188,7 @@ $wpdb = 'test'; // Error.
$GLOBALS['domain']['subkey'] = 'something else'; // Error.

// WordPress.WP.EnqueuedResources.NonEnqueuedScript
echo wp_kses( '<script src="' . esc_url( $script ) . '">', [ 'script' => [ 'src' => [], ], ] ); // Warning + Message.
echo wp_kses( '<script src="' . esc_url( $url ) . '">', [ 'script' => [ 'src' => [], ], ] ); // Warning + Message.
?> <script src="http://someurl/somefile.js"></script> <!-- Warning + Message. -->

<!-- WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -->
Expand All @@ -207,12 +207,12 @@ require_once "my_file.php"; // Warning.
require '../../my_file.php'; // Warning.
include("http://www.google.com/bad_file.php"); // Warning.

// WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable
// VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
function foo_bar_bar() {
$b . 'test'; // Warning.
}

// WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
// VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
function foo_bar_foo() {
$a = 'Hello'; // Warning
}
Expand Down Expand Up @@ -339,7 +339,7 @@ require_once __DIR__ . "/my_file.svg"; // Error.
// WordPressVIPMinimum.Functions.CheckReturnValue
$my_theme_options = get_option( 'my_theme', false );
if ( array_key_exists( 'key', $my_theme_options ) ) { } // Error.
echo '<a href="' . esc_url( get_term_link( $link ) ) . '">My term link</a>'; // Error.
echo '<a href="' . esc_url( get_term_link( $var ) ) . '">My term link</a>'; // Error.

// WordPressVIPMinimum.Functions.DynamicCalls
$my_notokay_func = 'extract';
Expand All @@ -349,7 +349,7 @@ $my_notokay_func(); // Error.
wp_cache_get_multi(); // Error.
opcache_reset(); // Error.
opcache_invalidate( 'test_script.php' ); // Error.
opcache_compile_file( $test_script ); // Error.
opcache_compile_file( $var ); // Error.
opcache_​is_​script_​cached( 'test_script.php' ); // Error.
opcache_​get_​status(); // Error.
opcache_​get_​configuration(); // Error.
Expand Down Expand Up @@ -425,7 +425,7 @@ wpcom_vip_get_term_by(); // Warning.
wpcom_vip_get_category_by_slug(); // Warning.

// WordPressVIPMinimum.Functions.StripTagsSniff
strip_tags( 'Test', $html ); // Warning.
strip_tags( 'Test', $text ); // Warning.

// WordPressVIPMinimum.Hooks.AlwaysReturnInFilter
function bad_example_function_thing() { // Error.
Expand Down Expand Up @@ -489,12 +489,12 @@ $query_args = [
];

// WordPressVIPMinimum.Performance.RemoteRequestTimeout
wp_remote_post( $this->endpoint, array(
wp_remote_post( $obj->endpoint, array(
'method' => 'POST',
'timeout' => 45, // Error.
'httpversion' => '1.1',
'blocking' => false,
'body' => wp_json_encode( $this->logs, JSON_UNESCAPED_SLASHES ),
'body' => wp_json_encode( $obj->logs, JSON_UNESCAPED_SLASHES ),
)
);

Expand Down
1 change: 1 addition & 0 deletions WordPress-VIP-Go/ruleset-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
417 => 1,
418 => 1,
419 => 1,
420 => 2,
421 => 1,
423 => 1,
424 => 1,
Expand Down
4 changes: 2 additions & 2 deletions WordPress-VIP-Go/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@
<type>warning</type>
<severity>3</severity>
</rule>
<rule ref="WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable">
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable">
<severity>3</severity>
</rule>
<rule ref="WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable">
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable">
<severity>1</severity>
</rule>
<rule ref="WordPressVIPMinimum.UserExperience.AdminBarRemoval">
Expand Down
73 changes: 0 additions & 73 deletions WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php

This file was deleted.

Loading

0 comments on commit a896729

Please sign in to comment.