From c7fdb254ec426d6076007ab53758889b54ea2ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 27 Nov 2024 15:43:09 -0500 Subject: [PATCH] fix errors reported by PHPStan --- classes/Bulk/Bulk.php | 9 ++++----- classes/Context/AbstractContext.php | 9 +++++---- classes/Context/CustomFolders.php | 4 ---- classes/Context/WP.php | 18 ++---------------- classes/Imagifybeat/Actions.php | 2 +- classes/Media/WP.php | 6 +----- classes/Traits/InstanceGetterTrait.php | 4 ++-- classes/User/User.php | 4 +--- inc/functions/admin-ui.php | 7 ++++--- 9 files changed, 20 insertions(+), 43 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index b159ccaf7..bbec0e6f3 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -156,7 +156,7 @@ private function decrease_counter( string $context ) { * @param int $optimization_level Optimization level. */ public function optimize_media( int $media_id, string $context, int $optimization_level ) { - if ( ! $media_id || ! $context || ! is_numeric( $optimization_level ) ) { + if ( ! $media_id || ! $context ) { $this->decrease_counter( $context ); return; @@ -565,7 +565,7 @@ public function bulk_get_stats_callback() { imagify_check_nonce( 'imagify-bulk-optimize' ); $folder_types = filter_input( INPUT_GET, 'types', FILTER_REQUIRE_ARRAY ); - $folder_types = is_array( $folder_types ) ? array_filter( $folder_types, 'is_string' ) : []; + $folder_types = is_array( $folder_types ) ? $folder_types : []; if ( ! $folder_types ) { imagify_die( __( 'Invalid request', 'imagify' ) ); @@ -644,7 +644,7 @@ public function get_contexts() { $types = apply_filters( 'imagify_bulk_page_types', $types ); $types = array_filter( (array) $types ); - if ( isset( $types['library|wp'] ) && ! in_array( 'wp', $contexts, true ) ) { + if ( isset( $types['library|wp'] ) ) { $contexts[] = 'wp'; } @@ -656,12 +656,11 @@ public function get_contexts() { if ( ! in_array( 'wp', $contexts, true ) ) { $contexts[] = 'wp'; } - } elseif ( $folders_instance->has_active_folders() && ! in_array( 'custom-folders', $contexts, true ) ) { + } elseif ( $folders_instance->has_active_folders() ) { $contexts[] = 'custom-folders'; } } return $contexts; } - } diff --git a/classes/Context/AbstractContext.php b/classes/Context/AbstractContext.php index 07dc59d26..759956456 100644 --- a/classes/Context/AbstractContext.php +++ b/classes/Context/AbstractContext.php @@ -16,7 +16,7 @@ abstract class AbstractContext implements ContextInterface { * @since 1.9 * @author Grégory Viguier */ - protected $context; + protected $context = ''; /** * Tell if the media/context is network-wide. @@ -55,7 +55,7 @@ abstract class AbstractContext implements ContextInterface { * @since 1.9 * @author Grégory Viguier */ - protected $thumbnail_sizes; + protected $thumbnail_sizes = []; /** * Tell if the optimization process is allowed to backup in this context. @@ -64,7 +64,7 @@ abstract class AbstractContext implements ContextInterface { * @since 1.9 * @author Grégory Viguier */ - protected $can_backup; + protected $can_backup = false; /** * Get the context "short name". @@ -161,7 +161,7 @@ public function can_backup() { * @return bool */ public function current_user_can( $describer, $media_id = null ) { - return $this->user_can( null, $describer, $media_id ); + return $this->user_can( 0 , $describer, $media_id ); } /** @@ -176,6 +176,7 @@ public function current_user_can( $describer, $media_id = null ) { * @return bool */ public function user_can( $user_id, $describer, $media_id = null ) { + $user = 0; $current_user_id = get_current_user_id(); if ( ! $user_id ) { diff --git a/classes/Context/CustomFolders.php b/classes/Context/CustomFolders.php index 547285484..15e1c849e 100644 --- a/classes/Context/CustomFolders.php +++ b/classes/Context/CustomFolders.php @@ -60,10 +60,6 @@ public function get_resizing_threshold() { * @return bool */ public function can_backup() { - if ( isset( $this->can_backup ) ) { - return $this->can_backup; - } - $this->can_backup = get_imagify_option( 'backup' ); return $this->can_backup; diff --git a/classes/Context/WP.php b/classes/Context/WP.php index 11f1e8d59..f2883e00d 100644 --- a/classes/Context/WP.php +++ b/classes/Context/WP.php @@ -28,7 +28,7 @@ final class WP extends AbstractContext { * @since 1.9.8 * @author Grégory Viguier */ - protected $resizing_threshold; + protected $resizing_threshold = 0; /** * Get the thumbnail sizes for this context, except the full size. @@ -47,10 +47,6 @@ final class WP extends AbstractContext { * } */ public function get_thumbnail_sizes() { - if ( isset( $this->thumbnail_sizes ) ) { - return $this->thumbnail_sizes; - } - $this->thumbnail_sizes = get_imagify_thumbnail_sizes(); return $this->thumbnail_sizes; @@ -66,13 +62,7 @@ public function get_thumbnail_sizes() { * @return int */ public function get_resizing_threshold() { - if ( isset( $this->resizing_threshold ) ) { - return $this->resizing_threshold; - } - - if ( ! get_imagify_option( 'resize_larger' ) ) { - $this->resizing_threshold = 0; - } else { + if ( get_imagify_option( 'resize_larger' ) ) { $this->resizing_threshold = max( 0, get_imagify_option( 'resize_larger_w' ) ); } @@ -88,10 +78,6 @@ public function get_resizing_threshold() { * @return bool */ public function can_backup() { - if ( isset( $this->can_backup ) ) { - return $this->can_backup; - } - $this->can_backup = get_imagify_option( 'backup' ); return $this->can_backup; diff --git a/classes/Imagifybeat/Actions.php b/classes/Imagifybeat/Actions.php index 1c315828d..353db5327 100644 --- a/classes/Imagifybeat/Actions.php +++ b/classes/Imagifybeat/Actions.php @@ -301,7 +301,7 @@ private function get_modified_optimization_statuses( $data ) { // Sanitize the IDs: IDs come as strings, prefixed with an undescore character (to prevent JavaScript from screwing everything). $media_ids = array_keys( $media_statuses ); $media_ids = array_map( function( $media_id ) { - return (int) substr( $media_id, 1 ); + return substr( $media_id, 1 ); }, $media_ids ); $media_ids = array_filter( $media_ids ); diff --git a/classes/Media/WP.php b/classes/Media/WP.php index 2e2e880bf..6a0efc08b 100644 --- a/classes/Media/WP.php +++ b/classes/Media/WP.php @@ -20,7 +20,7 @@ class WP extends AbstractMedia { * @access protected * @author Grégory Viguier */ - protected $is_wp53; + protected $is_wp53 = false; /** * The constructor. @@ -419,10 +419,6 @@ protected function update_media_data_dimensions( $dimensions ) { * @return bool */ protected function is_wp_53() { - if ( isset( $this->is_wp53 ) ) { - return $this->is_wp53; - } - $this->is_wp53 = function_exists( 'wp_get_original_image_path' ); return $this->is_wp53; diff --git a/classes/Traits/InstanceGetterTrait.php b/classes/Traits/InstanceGetterTrait.php index 0b0501c12..5232a0163 100644 --- a/classes/Traits/InstanceGetterTrait.php +++ b/classes/Traits/InstanceGetterTrait.php @@ -15,12 +15,12 @@ trait InstanceGetterTrait { /** * The "not-so-single" instance of the class. * - * @var object + * @var ?object * @since 1.9 * @access protected * @author Grégory Viguier */ - protected static $instance; + protected static $instance = null; /** * Get the main Instance. diff --git a/classes/User/User.php b/classes/User/User.php index 764968ed9..c490372c9 100755 --- a/classes/User/User.php +++ b/classes/User/User.php @@ -1,7 +1,6 @@ next_date_update = $user->next_date_update; $this->is_active = $user->is_active; $this->is_monthly = $user->is_monthly; - $this->error = is_wp_error( $user ); } /** diff --git a/inc/functions/admin-ui.php b/inc/functions/admin-ui.php index 5a501b4b4..85bbacd13 100644 --- a/inc/functions/admin-ui.php +++ b/inc/functions/admin-ui.php @@ -450,9 +450,10 @@ function get_imagify_attachment_delete_nextgen_versions_link( $process ) { * @since 1.9 Function signature changed. * @author Jonathan Buttigieg * - * @param ProcessInterface $process The optimization process object. - * @param bool $with_container Set to false to not return the HTML container. - * @return string The output to print. + * @param \Imagify\Optimization\Process\ProcessInterface $process The optimization process object. + * @param bool $with_container Set to false to not return the HTML container. + * + * @return string The output to print. */ function get_imagify_media_column_content( $process, $with_container = true ) { if ( ! $process->is_valid() ) {