Skip to content

Commit a56daeb

Browse files
Add phpcs rule to detect unused variables; Fix existing issues (WordPress#17300)
1 parent 5327c03 commit a56daeb

9 files changed

+73
-21
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
1515
"squizlabs/php_codesniffer": "^3.4.2",
1616
"phpcompatibility/php-compatibility": "^9.2.0",
17-
"wp-coding-standards/wpcs": "^2.1.1"
17+
"wp-coding-standards/wpcs": "^2.1.1",
18+
"sirbrillig/phpcs-variable-analysis": "^2.7"
1819
},
1920
"require": {
2021
"composer/installers": "~1.0"

composer.lock

+50-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gutenberg.php

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function gutenberg_build_files_notice() {
110110
* @since 1.5.0
111111
*/
112112
function gutenberg_pre_init() {
113+
global $wp_version;
113114
if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && ! file_exists( dirname( __FILE__ ) . '/build/blocks' ) ) {
114115
add_action( 'admin_notices', 'gutenberg_build_files_notice' );
115116
return;

lib/class-wp-rest-block-directory-controller.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ public function register_routes() {
7171
*
7272
* @since 6.5.0
7373
*
74-
* @param WP_REST_Request $request Full details about the request.
7574
* @return WP_Error|bool True if the request has permission, WP_Error object otherwise.
7675
*/
77-
public function permissions_check( $request ) {
76+
public function permissions_check() {
7877
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
7978
return new WP_Error(
8079
'rest_user_cannot_view',
@@ -221,8 +220,8 @@ public function get_items( $request ) {
221220

222221
include( ABSPATH . WPINC . '/version.php' );
223222

224-
$url = 'http://api.wordpress.org/plugins/info/1.2/';
225-
$url = add_query_arg(
223+
$url = 'http://api.wordpress.org/plugins/info/1.2/';
224+
$url = add_query_arg(
226225
array(
227226
'action' => 'query_plugins',
228227
'request[block]' => $search_string,
@@ -231,11 +230,12 @@ public function get_items( $request ) {
231230
),
232231
$url
233232
);
234-
$http_url = $url;
235-
$ssl = wp_http_supports( array( 'ssl' ) );
233+
$ssl = wp_http_supports( array( 'ssl' ) );
236234
if ( $ssl ) {
237235
$url = set_url_scheme( $url, 'https' );
238236
}
237+
238+
global $wp_version;
239239
$http_args = array(
240240
'timeout' => 15,
241241
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),

lib/class-wp-rest-widget-areas-controller.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ public function register_routes() {
5252
'validate_callback' => 'Experimental_WP_Widget_Blocks_Manager::is_valid_sidabar_id',
5353
);
5454

55-
$content_argument = array(
56-
'description' => __( 'Sidebar content.', 'gutenberg' ),
57-
'type' => 'string',
58-
'required' => true,
59-
);
60-
6155
register_rest_route(
6256
$this->namespace,
6357
'/' . $this->rest_base . '/(?P<id>.+)',
@@ -141,6 +135,9 @@ public function get_item_schema() {
141135
*
142136
* @param WP_REST_Request $request Full details about the request.
143137
* @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
138+
*
139+
* This function is overloading a function defined in WP_REST_Controller so it should have the same parameters.
140+
* phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
144141
*/
145142
public function get_items_permissions_check( $request ) {
146143
if ( ! current_user_can( 'edit_theme_options' ) ) {
@@ -152,6 +149,7 @@ public function get_items_permissions_check( $request ) {
152149

153150
return true;
154151
}
152+
/* phpcs:enable */
155153

156154
/**
157155
* Retrieves all widget areas.
@@ -192,6 +190,9 @@ public function get_item( $request ) {
192190
*
193191
* @param WP_REST_Request $request Full details about the request.
194192
* @return WP_Error|bool True if the request has access to update the item, error object otherwise.
193+
*
194+
* This function is overloading a function defined in WP_REST_Controller so it should have the same parameters.
195+
* phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
195196
*/
196197
public function update_item_permissions_check( $request ) {
197198
if ( ! current_user_can( 'edit_theme_options' ) ) {
@@ -203,6 +204,7 @@ public function update_item_permissions_check( $request ) {
203204

204205
return true;
205206
}
207+
/* phpcs:enable */
206208

207209
/**
208210
* Updates a single widget area.

lib/class-wp-rest-widget-updater-controller.php

-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public function compute_new_widget_permissions_check() {
8585
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
8686
*/
8787
public function compute_new_widget( $request ) {
88-
$url_params = $request->get_url_params();
89-
9088
$widget = $request->get_param( 'identifier' );
9189

9290
global $wp_widget_factory;

lib/client-assets.php

-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ function gutenberg_register_scripts_and_styles() {
240240
//
241241
// See: https://core.trac.wordpress.org/ticket/46107 .
242242
// See: https://github.com/WordPress/gutenberg/pull/13451 .
243-
global $wp_scripts;
244243
if ( isset( $wp_scripts->registered['wp-api-fetch'] ) ) {
245244
$wp_scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
246245
}

lib/experiments-page.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
* The main entry point for the Gutenberg experiments page.
1010
*
1111
* @since 6.3.0
12-
*
13-
* @param string $page The page name the function is being called for, `'gutenberg_customizer'` for the Customizer.
1412
*/
15-
function the_gutenberg_experiments( $page = 'gutenberg_page_gutenberg-experiments' ) {
13+
function the_gutenberg_experiments() {
1614
?>
1715
<div
1816
id="experiments-editor"

phpcs.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<rule ref="WordPress.WP.I18n"/>
1111
<config name="text_domain" value="gutenberg,default"/>
1212

13+
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
14+
<properties>
15+
<property name="allowUnusedParametersBeforeUsed" value="true"/>
16+
</properties>
17+
</rule>
1318
<rule ref="WordPress.CodeAnalysis.EmptyStatement"/>
1419

1520
<rule ref="PEAR.Functions.FunctionCallSignature">

0 commit comments

Comments
 (0)