Skip to content

Commit

Permalink
Closes #6833: Refactor to comply with phpstan level 4 (#6971)
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Perona <[email protected]>
  • Loading branch information
Miraeld and remyperona authored Sep 30, 2024
1 parent 09fada8 commit 7348869
Show file tree
Hide file tree
Showing 127 changed files with 425 additions and 812 deletions.
2 changes: 1 addition & 1 deletion inc/Addon/Cloudflare/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private function save_cloudflare_auto_settings( $auto_settings, $old_settings )
$result = [];

// Set Cache Level to Aggressive.
$cf_cache_level = isset( $cf_old_settings[0] ) && 0 === $auto_settings ? $cf_old_settings[0] : 'aggressive';
$cf_cache_level = 0 === $auto_settings ? $cf_old_settings[0] : 'aggressive';
$result[] = $this->save_cache_level( $cf_cache_level );

// Deactivate Rocket Loader to prevent conflicts.
Expand Down
12 changes: 6 additions & 6 deletions inc/Addon/WebP/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Subscriber extends AbstractWebp implements Subscriber_Interface {
/**
* \WP_Filesystem_Direct instance.
*
* @var \WP_Filesystem_Direct
* @var null|\WP_Filesystem_Direct
*/
private $filesystem;

Expand All @@ -58,11 +58,11 @@ public function __construct( Options_Data $options_data, Options $options_api, C
$this->options_data = $options_data;
$this->options_api = $options_api;

if ( ! isset( $server ) && ! empty( $_SERVER ) && is_array( $_SERVER ) ) {
if ( ! isset( $server ) && ! empty( $_SERVER ) ) {
$server = $_SERVER;
}

$this->server = $server && is_array( $server ) ? $server : [];
$this->server = $server ?: [];
}

/**
Expand Down Expand Up @@ -202,8 +202,8 @@ public function sync_webp_cache_with_third_party_plugins() {
* @param array $requests Requests to make.
* @return array
*/
public function add_accept_header( $requests ) {
if ( ! is_array( $requests ) || ! $this->options_data->get( 'cache_webp', 0 ) ) {
public function add_accept_header( array $requests ) {
if ( ! $this->options_data->get( 'cache_webp', 0 ) ) {
return $requests;
}

Expand All @@ -214,7 +214,7 @@ function ( $request ) {
return $request;
},
$requests
);
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/Dependencies/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ protected function set_vars( $args = array() ) {
*
* @since 1.0.0
*
* @return \wpdb Database interface, or False if not set
* @return \wpdb|false Database interface, or False if not set
*/
protected function get_db() {

Expand Down
4 changes: 2 additions & 2 deletions inc/Engine/Admin/Beacon/Beacon.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function insert_script() {
* @return string
*/
private function get_user_locale() {
if ( ! isset( $this->locale ) ) {
if ( empty( $this->locale ) ) {
$this->locale = current( array_slice( explode( '_', get_user_locale() ), 0, 1 ) );
}

Expand All @@ -122,7 +122,7 @@ private function get_user_locale() {
*
* @param string $locale The locale ID.
*/
return apply_filters( 'rocket_beacon_locale', $this->locale );
return wpm_apply_filters_typed( 'string', 'rocket_beacon_locale', $this->locale );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Admin/Database/Optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function get_options() {
* @param array $options WP Rocket Database options.
*/
public function process_handler( $options ) {
if ( method_exists( $this->process, 'cancel_process' ) ) {
if ( method_exists( $this->process, 'cancel_process' ) ) { // @phpstan-ignore-line
$this->process->cancel_process();
}

Expand Down
12 changes: 2 additions & 10 deletions inc/Engine/Admin/Metaboxes/PostEditOptionsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ public function display_metabox() {
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );

if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}
$fields = wpm_apply_filters_typed( 'array', 'rocket_meta_boxes_fields', $original_fields );

$fields_attributes = [];

Expand Down Expand Up @@ -179,11 +175,7 @@ public function save_metabox_options() {
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );

if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}
$fields = wpm_apply_filters_typed( 'array', 'rocket_meta_boxes_fields', $original_fields );

$fields = array_keys( $fields );

Expand Down
14 changes: 5 additions & 9 deletions inc/Engine/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ private function advanced_cache_section() {
$ecommerce_plugin = _x( 'iThemes Exchange', 'plugin name', 'rocket' );
} elseif ( defined( 'JIGOSHOP_VERSION' ) && function_exists( 'jigoshop_get_page_id' ) ) {
$ecommerce_plugin = _x( 'Jigoshop', 'plugin name', 'rocket' );
} elseif ( defined( 'WPSHOP_VERSION' ) && class_exists( 'wpshop_tools' ) && method_exists( 'wpshop_tools', 'get_page_id' ) ) {
} elseif ( defined( 'WPSHOP_VERSION' ) && class_exists( 'wpshop_tools' ) && method_exists( 'wpshop_tools', 'get_page_id' ) ) { // @phpstan-ignore-line
$ecommerce_plugin = _x( 'WP-Shop', 'plugin name', 'rocket' );
}

Expand Down Expand Up @@ -1519,11 +1519,7 @@ private function cdn_section() {
*
* @param array $addons Array of addons.
*/
$addons = apply_filters( 'rocket_cdn_helper_addons', [] );

if ( ! is_array( $addons ) ) {
$addons = [];
}
$addons = wpm_apply_filters_typed( 'array', 'rocket_cdn_helper_addons', [] );

$addons = array_unique( $addons );

Expand Down Expand Up @@ -2107,8 +2103,8 @@ private function hidden_fields() {
* @param string $tag_name Name of the HTML tag that will wrap each element of the list.
* @return array
*/
private function sanitize_and_format_list( $list, $tag_name = 'strong' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.listFound
if ( ! is_array( $list ) || empty( $list ) ) {
private function sanitize_and_format_list( array $list, $tag_name = 'strong' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.listFound
if ( empty( $list ) ) {
return [];
}

Expand Down Expand Up @@ -2180,7 +2176,7 @@ public function enable_mobile_cache(): void {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error();
return;
return; // @phpstan-ignore-line
}

$this->options->set( 'cache_mobile', 1 );
Expand Down
4 changes: 0 additions & 4 deletions inc/Engine/Admin/Settings/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ function ( array $item ) use ( $default ) {
* @since 3.0
*/
public function render_form_sections() {
if ( ! isset( $this->settings ) ) {
return;
}

foreach ( $this->settings as $id => $args ) {
$default = [
'title' => '',
Expand Down
14 changes: 4 additions & 10 deletions inc/Engine/Admin/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ public function sanitize_callback( $input ) {

$input['automatic_cleanup_frequency'] = isset( $input['automatic_cleanup_frequency'], $cleanup_frequencies[ $input['automatic_cleanup_frequency'] ] ) ? $input['automatic_cleanup_frequency'] : $this->options->get( 'automatic_cleanup_frequency' );

if ( 1 !== $input['schedule_automatic_cleanup'] && ( 'daily' !== $input['automatic_cleanup_frequency'] || 'weekly' !== $input['automatic_cleanup_frequency'] || 'monthly' !== $input['automatic_cleanup_frequency'] ) ) {
$allowed_frequencies = [ 'daily', 'weekly', 'monthly' ];

if ( 1 !== $input['schedule_automatic_cleanup'] && ! in_array( $input['automatic_cleanup_frequency'], $allowed_frequencies, true ) ) {
$input['automatic_cleanup_frequency'] = $this->options->get( 'automatic_cleanup_frequency' );
}

Expand Down Expand Up @@ -554,11 +556,7 @@ private function sanitize_fonts( $files ) {
* @param string $file URL or path to a font file.
* @return string|bool
*/
private function sanitize_font( $file ) {
if ( ! is_string( $file ) ) {
return false;
}

private function sanitize_font( string $file ) {
$file = trim( $file );

if ( empty( $file ) ) {
Expand Down Expand Up @@ -604,10 +602,6 @@ private function sanitize_font( $file ) {
* @return array
*/
private function get_hosts() {
if ( isset( $this->hosts ) ) {
return $this->hosts;
}

$urls = (array) $this->options->get( 'cdn_cnames', [] );
$urls[] = home_url();
$urls = array_map( 'rocket_add_url_protocol', $urls );
Expand Down
25 changes: 0 additions & 25 deletions inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,22 @@ public function update_user_token() {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'unauthorized_user' );

return;
}

if ( empty( $_POST['value'] ) ) {
delete_option( 'rocketcdn_user_token' );

wp_send_json_success( 'user_token_deleted' );

return;
}

if ( ! is_string( $_POST['value'] ) ) {
wp_send_json_error( 'invalid_token' );

return;
}

$token = sanitize_key( $_POST['value'] );

if ( 40 !== strlen( $token ) ) {
wp_send_json_error( 'invalid_token_length' );

return;
}

update_option( 'rocketcdn_user_token', $token );
Expand All @@ -112,16 +104,12 @@ public function enable() {
$data['message'] = 'unauthorized_user';

wp_send_json_error( $data );

return;
}

if ( empty( $_POST['cdn_url'] ) ) {
$data['message'] = 'cdn_url_empty';

wp_send_json_error( $data );

return;
}

$cdn_url = filter_var( wp_unslash( $_POST['cdn_url'] ), FILTER_VALIDATE_URL );
Expand All @@ -130,8 +118,6 @@ public function enable() {
$data['message'] = 'cdn_url_invalid_format';

wp_send_json_error( $data );

return;
}

$this->cdn_options->enable( esc_url_raw( $cdn_url ) );
Expand Down Expand Up @@ -164,8 +150,6 @@ public function disable() {
$data['message'] = 'unauthorized_user';

wp_send_json_error( $data );

return;
}

$this->cdn_options->disable();
Expand Down Expand Up @@ -234,14 +218,10 @@ public function get_process_status() {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error();

return;
}

if ( get_option( 'rocketcdn_process' ) ) {
wp_send_json_success();

return;
}

wp_send_json_error();
Expand Down Expand Up @@ -285,13 +265,11 @@ public function validate_token_cname() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
return;
}

if ( empty( $_POST['cdn_url'] ) || empty( $_POST['cdn_token'] ) ) {
$data['message'] = 'cdn_values_empty';
wp_send_json_error( $data );
return;
}

$token = sanitize_key( $_POST['cdn_token'] );
Expand All @@ -300,13 +278,11 @@ public function validate_token_cname() {
if ( ! $cdn_url ) {
$data['message'] = 'cdn_url_invalid_format';
wp_send_json_error( $data );
return;
}

if ( 40 !== strlen( $token ) ) {
$data['message'] = 'invalid_token_length';
wp_send_json_error( $data );
return;
}

$current_token = get_option( 'rocketcdn_user_token' );
Expand All @@ -315,7 +291,6 @@ public function validate_token_cname() {
if ( ! empty( $current_token ) ) {
$data['message'] = 'token_already_set';
wp_send_json_error( $data );
return;
}

update_option( 'rocketcdn_user_token', $token );
Expand Down
6 changes: 0 additions & 6 deletions inc/Engine/CDN/RocketCDN/NoticesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ public function dismiss_notice() {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'no permissions' );

return;
}

update_user_meta( get_current_user_id(), 'rocketcdn_dismiss_notice', true );
Expand Down Expand Up @@ -284,14 +282,10 @@ public function toggle_cta() {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'no permissions' );

return;
}

if ( ! isset( $_POST['status'] ) ) {
wp_send_json_error( 'missing status' );

return;
}

if ( 'big' === $_POST['status'] ) {
Expand Down
6 changes: 1 addition & 5 deletions inc/Engine/Cache/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function purge_dates_archives( $post ) {
* @param boolean $pagination Purge also pagination.
* @return void
*/
public function purge_url( $url, $pagination = false ) {
if ( ! is_string( $url ) ) {
return;
}

public function purge_url( string $url, $pagination = false ) {
global $wp_rewrite;

$parsed_url = $this->parse_url( $url );
Expand Down
8 changes: 4 additions & 4 deletions inc/Engine/Cache/PurgeExpired/PurgeExpiredCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PurgeExpiredCache {
*
* @since 3.4
*
* @var \WP_Filesystem_Direct
* @var null|\WP_Filesystem_Direct
*/
private $filesystem;

Expand Down Expand Up @@ -60,7 +60,7 @@ public function purge_expired_files( $lifespan ) {
* @param array $urls URLs that will be searched for old cache files.
* @param int $file_age_limit Timestamp of the maximum age files must have.
*/
$urls = apply_filters( 'rocket_automatic_cache_purge_urls', $urls, $file_age_limit );
$urls = wpm_apply_filters_typed( 'array', 'rocket_automatic_cache_purge_urls', $urls, $file_age_limit );

if ( ! is_array( $urls ) ) {
// I saw what you did ಠ_ಠ.
Expand Down Expand Up @@ -380,8 +380,8 @@ private function is_dir_empty( $dir_path ) {
*
* @since 3.8
*
* @param int $old_lifespan Old value in minutes.
* @param int $old_lifespan_unit Old value of unit.
* @param int $old_lifespan Old value in minutes.
* @param string $old_lifespan_unit Old value of unit.
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Common/Database/Queries/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get_row( string $url, bool $is_mobile = false ) {
*
* @param int $row_id DB Row ID.
*
* @return array|false
* @return object|array|false false if no row found, array or object if row found.
*/
public function get_row_by_id( int $row_id ) {
if ( ! self::$table_exists && ! $this->table_exists() ) {
Expand Down
Loading

0 comments on commit 7348869

Please sign in to comment.