Skip to content

Commit 1461424

Browse files
authored
PHPStan Level 8 and PHPCS adjustements (#241)
* PHPStan Level 8 and PHPCS adjustments * Null check for network_id should return explicitly false
1 parent cdaac05 commit 1461424

12 files changed

Lines changed: 156 additions & 126 deletions

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"composer/installers": "^1.0 || ^2.0"
2121
},
2222
"require-dev": {
23+
"automattic/vipwpcs": "^3.0",
2324
"php-stubs/wp-cli-stubs": "^2.12.0",
2425
"phpcompatibility/phpcompatibility-wp": "^2.1.8",
2526
"phpstan/extension-installer": "^1.4.3",

phpcs.xml.dist

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</properties>
2222
</rule>
2323
<rule ref="PHPCompatibilityWP" />
24+
<rule ref="WordPress-VIP-Go" />
2425
<rule ref="WordPress.WP.Capabilities">
2526
<properties>
2627
<property name="custom_capabilities" type="array">
@@ -33,7 +34,14 @@
3334
</property>
3435
</properties>
3536
</rule>
36-
37+
<rule ref="WordPressVIPMinimum">
38+
<exclude name="WordPressVIPMinimum.JS.Window"/>
39+
<exclude name="WordPressVIPMinimum.JS.DangerouslySetInnerHTML"/>
40+
<exclude name="WordPressVIPMinimum.JS.InnerHTML"/>
41+
<exclude name="WordPressVIPMinimum.JS.StrippingTags"/>
42+
<exclude name="WordPressVIPMinimum.JS.StringConcat"/>
43+
<exclude name="WordPressVIPMinimum.JS.HTMLExecutingFunctions"/>
44+
</rule>
3745
<config name="testVersion" value="7.2-" />
3846

3947
<file>.</file>

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 6
2+
level: 8
33
treatPhpDocTypesAsCertain: false
44
paths:
55
- wp-multi-network/includes

wp-multi-network/includes/classes/class-wp-ms-network-command.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function create( $args, $assoc_args ) {
8484

8585
$clone_network = $assoc_args['clone_network'];
8686

87-
if ( ! empty( $clone_network ) && is_numeric( $clone_network ) && ! get_network( $clone_network ) ) {
87+
if ( ! empty( $clone_network ) && is_numeric( $clone_network ) && ! get_network( (int) $clone_network ) ) {
8888
WP_CLI::error( sprintf( "Clone network %s doesn't exist.", $clone_network ) );
8989
}
9090

@@ -103,6 +103,8 @@ public function create( $args, $assoc_args ) {
103103

104104
if ( is_wp_error( $network_id ) ) {
105105
WP_CLI::error( $network_id );
106+
107+
return;
106108
}
107109

108110
WP_CLI::success( sprintf( 'Created network %d.', $network_id ) );
@@ -231,7 +233,9 @@ public function move_site( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAn
231233
* @return void
232234
*/
233235
public function list_( $args, $assoc_args ) {
234-
$items = get_networks();
236+
/** @var WP_Network[] $items */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
237+
$items = get_networks();
238+
235239
$formatter = $this->get_formatter( $assoc_args );
236240
$formatter->display_items( $items );
237241
}

wp-multi-network/includes/classes/class-wp-ms-networks-admin-bar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function admin_bar( $wp_admin_bar ) {
7474
$networks = user_has_networks();
7575

7676
// Bail if user does not have networks or they can't manage networks.
77-
if ( empty( $networks ) || ! current_user_can( 'manage_networks' ) ) {
77+
if ( ! is_array( $networks ) || array() === $networks || ! current_user_can( 'manage_networks' ) ) {
7878
return;
7979
}
8080

wp-multi-network/includes/classes/class-wp-ms-networks-admin.php

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function network_admin_menu() {
123123
* @return void
124124
*/
125125
public function network_admin_menu_separator() {
126-
$GLOBALS['menu']['-2'] = array( '', 'read', 'separator', '', 'wp-menu-separator' ); // phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited
126+
$GLOBALS['menu']['-2'] = array( '', 'read', 'separator', '', 'wp-menu-separator' );
127127
}
128128

129129
/**
@@ -147,7 +147,7 @@ public function fix_menu_highlight_for_move_page() {
147147
// phpcs:enable
148148

149149
if ( 'move' === $action ) {
150-
$submenu_file = 'sites.php'; // phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited
150+
$submenu_file = 'sites.php';
151151
}
152152
}
153153
}
@@ -169,7 +169,7 @@ public function enqueue_scripts( $page = '' ) {
169169

170170
// Determine if we should load source or minified assets based on WP_SCRIPT_DEBUG.
171171
$suffix = ( defined( 'WP_SCRIPT_DEBUG' ) && WP_SCRIPT_DEBUG ) ? '' : '.min';
172-
$asset_version = ( defined( 'WP_SCRIPT_DEBUG' ) && WP_SCRIPT_DEBUG ) ? time() : wpmn()->asset_version;
172+
$asset_version = ( defined( 'WP_SCRIPT_DEBUG' ) && WP_SCRIPT_DEBUG ) ? (string) time() : wpmn()->asset_version;
173173

174174
wp_register_style( 'wp-multi-network', wpmn()->plugin_url . 'assets/css/wp-multi-network' . $suffix . '.css', array(), $asset_version );
175175
wp_register_script( 'wp-multi-network', wpmn()->plugin_url . 'assets/js/wp-multi-network' . $suffix . '.js', array( 'jquery', 'post' ), $asset_version, true );
@@ -232,7 +232,7 @@ public function network_admin_notices() {
232232
}
233233

234234
// Pass/Fail.
235-
$passed = sanitize_key( $_GET[ $slug ] );
235+
$passed = sanitize_key( (string) $_GET[ $slug ] );
236236

237237
if ( '1' === $passed ) {
238238
// Pass.
@@ -347,7 +347,7 @@ public function route_save_handlers() {
347347

348348
// Bail -- our fields aren't being edited
349349
// Otherwise we'll do an unnecessary nonce check.
350-
if ( empty( $_POST['network_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
350+
if ( empty( $_POST['network_edit'] ) ) {
351351
return;
352352
}
353353

@@ -412,6 +412,12 @@ public function route_save_handlers() {
412412
*/
413413
public function page_edit_network() {
414414

415+
$screen = get_current_screen();
416+
417+
if ( null === $screen ) {
418+
return;
419+
}
420+
415421
$network_id = ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] )
416422
? (int) $_GET['id']
417423
: 0;
@@ -420,18 +426,18 @@ public function page_edit_network() {
420426
? get_network( $network_id )
421427
: null;
422428

423-
add_meta_box( 'wpmn-edit-network-details', esc_html__( 'Details', 'wp-multi-network' ), 'wpmn_edit_network_details_metabox', get_current_screen()->id, 'normal', 'high', array( $network ) );
424-
add_meta_box( 'wpmn-edit-network-publish', esc_html__( 'Network', 'wp-multi-network' ), 'wpmn_edit_network_publish_metabox', get_current_screen()->id, 'side', 'high', array( $network ) );
429+
add_meta_box( 'wpmn-edit-network-details', esc_html__( 'Details', 'wp-multi-network' ), 'wpmn_edit_network_details_metabox', $screen->id, 'normal', 'high', array( $network ) );
430+
add_meta_box( 'wpmn-edit-network-publish', esc_html__( 'Network', 'wp-multi-network' ), 'wpmn_edit_network_publish_metabox', $screen->id, 'side', 'high', array( $network ) );
425431

426432
// Differentiate between a new network and an existing network.
427433
if ( empty( $network ) ) {
428434
$network_title = '';
429435

430-
add_meta_box( 'wpmn-edit-network-new-site', esc_html__( 'Root Site', 'wp-multi-network' ), 'wpmn_edit_network_new_site_metabox', get_current_screen()->id, 'advanced', 'high', array( $network ) );
436+
add_meta_box( 'wpmn-edit-network-new-site', esc_html__( 'Root Site', 'wp-multi-network' ), 'wpmn_edit_network_new_site_metabox', $screen->id, 'advanced', 'high', array( $network ) );
431437
} else {
432438
$network_title = get_network_option( $network->id, 'site_name', '' );
433439

434-
add_meta_box( 'wpmn-edit-network-assign-sites', esc_html__( 'Site Assignment', 'wp-multi-network' ), 'wpmn_edit_network_assign_sites_metabox', get_current_screen()->id, 'advanced', 'high', array( $network ) );
440+
add_meta_box( 'wpmn-edit-network-assign-sites', esc_html__( 'Site Assignment', 'wp-multi-network' ), 'wpmn_edit_network_assign_sites_metabox', $screen->id, 'advanced', 'high', array( $network ) );
435441
}
436442

437443
$add_network_url = $this->admin_url( array( 'page' => 'add-new-network' ) );
@@ -469,12 +475,12 @@ public function page_edit_network() {
469475
</div>
470476

471477
<div id="postbox-container-1" class="postbox-container">
472-
<?php do_meta_boxes( get_current_screen()->id, 'side', $network ); ?>
478+
<?php do_meta_boxes( $screen->id, 'side', $network ); ?>
473479
</div>
474480

475481
<div id="postbox-container-2" class="postbox-container">
476-
<?php do_meta_boxes( get_current_screen()->id, 'normal', $network ); ?>
477-
<?php do_meta_boxes( get_current_screen()->id, 'advanced', $network ); ?>
482+
<?php do_meta_boxes( $screen->id, 'normal', $network ); ?>
483+
<?php do_meta_boxes( $screen->id, 'advanced', $network ); ?>
478484
</div>
479485
</div>
480486
</div>
@@ -553,6 +559,12 @@ private function page_all_networks() {
553559
*/
554560
private function page_move_site() {
555561

562+
$screen = get_current_screen();
563+
564+
if ( null === $screen ) {
565+
return;
566+
}
567+
556568
$site_id = ! empty( $_GET['blog_id'] ) && is_numeric( $_GET['blog_id'] )
557569
? (int) $_GET['blog_id']
558570
: 0;
@@ -571,7 +583,7 @@ private function page_move_site() {
571583
'wpmn-move-site-list',
572584
esc_html__( 'Assign Network', 'wp-multi-network' ),
573585
'wpmn_move_site_list_metabox',
574-
get_current_screen()->id,
586+
$screen->id,
575587
'normal',
576588
'high',
577589
array( $site )
@@ -582,7 +594,7 @@ private function page_move_site() {
582594
'wpmn-move-site-publish',
583595
esc_html__( 'Site', 'wp-multi-network' ),
584596
'wpmn_move_site_assign_metabox',
585-
get_current_screen()->id,
597+
$screen->id,
586598
'side',
587599
'high',
588600
array( $site )
@@ -621,12 +633,12 @@ private function page_move_site() {
621633
<div id="poststuff">
622634
<div id="post-body" class="metabox-holder columns-2">
623635
<div id="postbox-container-1" class="postbox-container">
624-
<?php do_meta_boxes( get_current_screen()->id, 'side', $site ); ?>
636+
<?php do_meta_boxes( $screen->id, 'side', $site ); ?>
625637
</div>
626638

627639
<div id="postbox-container-2" class="postbox-container">
628-
<?php do_meta_boxes( get_current_screen()->id, 'normal', $site ); ?>
629-
<?php do_meta_boxes( get_current_screen()->id, 'advanced', $site ); ?>
640+
<?php do_meta_boxes( $screen->id, 'normal', $site ); ?>
641+
<?php do_meta_boxes( $screen->id, 'advanced', $site ); ?>
630642
</div>
631643
</div>
632644
</div>
@@ -747,7 +759,6 @@ private function page_delete_network() {
747759
* @return void
748760
*/
749761
private function page_delete_networks() {
750-
751762
$network_id = get_main_network_id();
752763

753764
$all_networks = ! empty( $_POST['all_networks'] ) && is_array( $_POST['all_networks'] )
@@ -756,6 +767,7 @@ private function page_delete_networks() {
756767

757768
$all_networks = array_diff( $all_networks, array( $network_id ) );
758769

770+
/** @var WP_Network[] $networks */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
759771
$networks = get_networks(
760772
array(
761773
'network__in' => $all_networks,
@@ -799,7 +811,7 @@ private function page_delete_networks() {
799811

800812
foreach ( $networks as $deleted_network ) {
801813
?>
802-
<li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr( $deleted_network->id ); ?>"><?php echo esc_html( $deleted_network->domain . $deleted_network->path ); ?></li>
814+
<li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr( (string) $deleted_network->id ); ?>"><?php echo esc_html( $deleted_network->domain . $deleted_network->path ); ?></li>
803815
<?php
804816
}
805817

@@ -834,7 +846,7 @@ private function page_delete_networks() {
834846
foreach ( $networks as $deleted_network ) :
835847

836848
?>
837-
<li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr( $deleted_network->id ); ?>"><?php echo esc_html( $deleted_network->domain . $deleted_network->path ); ?></li>
849+
<li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr( (string) $deleted_network->id ); ?>"><?php echo esc_html( $deleted_network->domain . $deleted_network->path ); ?></li>
838850
<?php
839851

840852
endforeach;
@@ -882,9 +894,12 @@ public function page_my_networks() {
882894
<?php
883895

884896
$my_networks = user_has_networks();
897+
if ( ! is_array( $my_networks ) ) {
898+
$my_networks = array();
899+
}
900+
885901
foreach ( $my_networks as $key => $network_id ) {
886-
// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery,WordPress.VIP.DirectDatabaseQuery.NoCaching
887-
$my_networks[ $key ] = $wpdb->get_row(
902+
$my_networks[ $key ] = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
888903
$wpdb->prepare(
889904
'SELECT s.*, sm.meta_value as site_name, b.blog_id FROM ' . $wpdb->site . ' s LEFT JOIN ' . $wpdb->sitemeta . ' as sm ON sm.site_id = s.id AND sm.meta_key = %s LEFT JOIN ' . $wpdb->blogs . ' b ON s.id = b.site_id AND b.path = s.path WHERE s.id = %d',
890905
'site_name',
@@ -967,14 +982,8 @@ public function page_my_networks() {
967982
* @return void
968983
*/
969984
private function handle_add_network() {
970-
971-
// Sanitize options to clone.
972985
$options_to_clone = ! empty( $_POST['options_to_clone'] ) && is_array( $_POST['options_to_clone'] )
973-
? $_POST['options_to_clone']
974-
: array();
975-
976-
$options_to_clone = ! empty( $options_to_clone )
977-
? array_keys( $options_to_clone )
986+
? array_map( 'sanitize_text_field', array_keys( $_POST['options_to_clone'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
978987
: array_keys( network_options_to_copy() );
979988

980989
// Sanitize network ID to clone.
@@ -988,27 +997,17 @@ private function handle_add_network() {
988997

989998
// Unslash posted values.
990999
$network_title = ! empty( $_POST['title'] )
991-
? wp_unslash( $_POST['title'] )
1000+
? sanitize_text_field( wp_unslash( $_POST['title'] ) )
9921001
: '';
9931002
$network_domain = ! empty( $_POST['domain'] )
994-
? wp_unslash( $_POST['domain'] )
1003+
? str_replace( ' ', '', strtolower( sanitize_text_field( wp_unslash( $_POST['domain'] ) ) ) )
9951004
: '';
9961005
$network_path = ! empty( $_POST['path'] )
997-
? wp_unslash( $_POST['path'] )
1006+
? str_replace( ' ', '', strtolower( sanitize_text_field( wp_unslash( $_POST['path'] ) ) ) )
9981007
: '';
9991008
$site_name = ! empty( $_POST['new_site'] )
1000-
? wp_unslash( $_POST['new_site'] )
1001-
: '';
1002-
1003-
// Additional sanitization.
1004-
$network_title = sanitize_text_field( $network_title );
1005-
$network_domain = str_replace( ' ', '', strtolower( sanitize_text_field( $network_domain ) ) );
1006-
$network_path = str_replace( ' ', '', strtolower( sanitize_text_field( $network_path ) ) );
1007-
1008-
// Fallback to network title if not explicitly set.
1009-
$site_name = ! empty( $site_name )
1010-
? sanitize_text_field( $site_name )
1011-
: $network_title;
1009+
? sanitize_text_field( wp_unslash( $_POST['new_site'] ) )
1010+
: $network_title; // Fallback to network title if not explicitly set.
10121011

10131012
// Bail if missing fields.
10141013
if ( empty( $network_domain ) || empty( $network_path ) ) {
@@ -1090,20 +1089,15 @@ private function handle_update_network() {
10901089

10911090
// Unslash posted values.
10921091
$network_title = ! empty( $_POST['title'] )
1093-
? wp_unslash( $_POST['title'] )
1092+
? sanitize_text_field( wp_unslash( $_POST['title'] ) )
10941093
: '';
10951094
$network_domain = ! empty( $_POST['domain'] )
1096-
? wp_unslash( $_POST['domain'] )
1095+
? str_replace( ' ', '', strtolower( sanitize_text_field( wp_unslash( $_POST['domain'] ) ) ) )
10971096
: '';
10981097
$network_path = ! empty( $_POST['path'] )
1099-
? wp_unslash( $_POST['path'] )
1098+
? str_replace( ' ', '', strtolower( sanitize_text_field( wp_unslash( $_POST['path'] ) ) ) )
11001099
: '';
11011100

1102-
// Additional sanitization.
1103-
$network_title = sanitize_text_field( $network_title );
1104-
$network_domain = str_replace( ' ', '', strtolower( sanitize_text_field( $network_domain ) ) );
1105-
$network_path = str_replace( ' ', '', strtolower( sanitize_text_field( $network_path ) ) );
1106-
11071101
// Bail if missing fields.
11081102
if ( empty( $network_title ) || empty( $network_domain ) || empty( $network_path ) ) {
11091103
$this->handle_redirect(

wp-multi-network/includes/classes/class-wp-ms-networks-list-table.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ public function prepare_items() {
8585
'no_found_rows' => false,
8686
);
8787

88-
$query = new WP_Network_Query();
89-
90-
$this->items = $query->query( $args );
88+
$query = new WP_Network_Query();
89+
$this->items = (array) $query->query( $args );
9190
$count = $query->found_networks;
9291

9392
$this->set_pagination_args(
@@ -133,6 +132,10 @@ public function get_bulk_actions() {
133132
* @param string $which Where to display the pagination. Either 'top' or 'bottom'.
134133
*/
135134
public function pagination( $which ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
135+
if ( ! in_array( $which, array( 'top', 'bottom' ), true ) ) {
136+
return;
137+
}
138+
136139
parent::pagination( $which );
137140
}
138141

@@ -198,7 +201,7 @@ public function get_sortable_columns() {
198201
* @return void
199202
*/
200203
public function single_row( $network ) {
201-
$class = (int) get_current_site()->id === (int) $network->id ? 'current' : 'not-current';
204+
$class = get_current_site()->id === ( $network->id ?? 0 ) ? 'current' : 'not-current';
202205

203206
echo '<tr class="' . esc_attr( $class ) . '">';
204207
$this->single_row_columns( $network );

0 commit comments

Comments
 (0)