Skip to content

Commit d1a4c29

Browse files
committed
Switch to sirbrillig/phpcs-variable-analysis
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS. 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.
1 parent 6eb7b0c commit d1a4c29

File tree

11 files changed

+111
-1329
lines changed

11 files changed

+111
-1329
lines changed

WordPress-VIP-Go/ruleset-test.inc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
$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.
33
// WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_delete
44
delete( $file ); // Warning + Message.
55

@@ -163,7 +163,7 @@ rawurlencode(); // Ok.
163163

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

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

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

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

210-
// WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable
210+
// VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
211211
function foo_bar_bar() {
212212
$b . 'test'; // Warning.
213213
}
214214

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

344344
// WordPressVIPMinimum.Functions.DynamicCalls
345345
$my_notokay_func = 'extract';
@@ -349,7 +349,7 @@ $my_notokay_func(); // Error.
349349
wp_cache_get_multi(); // Error.
350350
opcache_reset(); // Error.
351351
opcache_invalidate( 'test_script.php' ); // Error.
352-
opcache_compile_file( $test_script ); // Error.
352+
opcache_compile_file( $var ); // Error.
353353
opcache_​is_​script_​cached( 'test_script.php' ); // Error.
354354
opcache_​get_​status(); // Error.
355355
opcache_​get_​configuration(); // Error.
@@ -425,7 +425,7 @@ wpcom_vip_get_term_by(); // Warning.
425425
wpcom_vip_get_category_by_slug(); // Warning.
426426

427427
// WordPressVIPMinimum.Functions.StripTagsSniff
428-
strip_tags( 'Test', $html ); // Warning.
428+
strip_tags( 'Test', $text ); // Warning.
429429

430430
// WordPressVIPMinimum.Hooks.AlwaysReturnInFilter
431431
function bad_example_function_thing() { // Error.
@@ -489,12 +489,12 @@ $query_args = [
489489
];
490490

491491
// WordPressVIPMinimum.Performance.RemoteRequestTimeout
492-
wp_remote_post( $this->endpoint, array(
492+
wp_remote_post( $obj->endpoint, array(
493493
'method' => 'POST',
494494
'timeout' => 45, // Error.
495495
'httpversion' => '1.1',
496496
'blocking' => false,
497-
'body' => wp_json_encode( $this->logs, JSON_UNESCAPED_SLASHES ),
497+
'body' => wp_json_encode( $obj->logs, JSON_UNESCAPED_SLASHES ),
498498
)
499499
);
500500

WordPress-VIP-Go/ruleset-test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
417 => 1,
223223
418 => 1,
224224
419 => 1,
225+
420 => 2,
225226
421 => 1,
226227
423 => 1,
227228
424 => 1,

WordPress-VIP-Go/ruleset.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@
244244
<type>warning</type>
245245
<severity>3</severity>
246246
</rule>
247-
<rule ref="WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable">
247+
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable">
248248
<severity>3</severity>
249249
</rule>
250-
<rule ref="WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable">
250+
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable">
251251
<severity>1</severity>
252252
</rule>
253253
<rule ref="WordPressVIPMinimum.UserExperience.AdminBarRemoval">

WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)