Skip to content

Commit 3e52b58

Browse files
Merge pull request #1200 from cloudinary/phpstan-level4
Bump PHPStan to level 4 and fix all resulting issues
2 parents 0601302 + 9e09d5a commit 3e52b58

38 files changed

Lines changed: 102 additions & 153 deletions

php/cache/class-cache-point.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ function ( $post ) {
406406
*
407407
* @param string $url The URL to convert.
408408
*
409-
* @return string
409+
* @return string|null
410410
*/
411411
public function url_to_path( $url ) {
412412
$url = $this->clean_url( $url );
@@ -451,7 +451,7 @@ protected function load_cache_point( $url ) {
451451
*
452452
* @param string $url The cache point url to get.
453453
*
454-
* @return \WP_Post
454+
* @return \WP_Post|null
455455
*/
456456
public function get_cache_point( $url ) {
457457
// Lets check if the cache_point is a file.
@@ -647,7 +647,7 @@ public function create_cache_point( $url, $src_path, $version ) {
647647
*/
648648
protected function check_version( $url, $version ) {
649649
$cache_point = $this->get_cache_point( $url );
650-
if ( ! is_numeric( $cache_point ) ) {
650+
if ( $cache_point instanceof \WP_Post ) {
651651
$prev_version = get_post_meta( $cache_point->ID, self::META_KEYS['version'], true );
652652
if ( $prev_version !== $version ) {
653653
update_post_meta( $cache_point->ID, self::META_KEYS['version'], $version );

php/cache/class-file-system.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class File_System {
2727
/**
2828
* WP file system
2929
*
30-
* @var \WP_Filesystem_Direct
30+
* @var \WP_Filesystem_Direct|null
3131
*/
3232
public $wp_file_system;
3333

@@ -210,9 +210,9 @@ public function get_src_path( $file_url ) {
210210
/**
211211
* Get the URL's for a list of src files.
212212
*
213-
* @param string $path The path to get urls for.
214-
* @param array $files The list of files.
215-
* @param null $version The version.
213+
* @param string $path The path to get urls for.
214+
* @param array $files The list of files.
215+
* @param string|null $version The version.
216216
*
217217
* @return array|false|mixed
218218
*/

php/class-admin.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function init_components( $template, $slug ) {
367367
/**
368368
* Filter out non-setting params.
369369
*
370-
* @param numeric-string $key The key to filter out.
370+
* @param int|string $key The key to filter out.
371371
*
372372
* @return bool
373373
*/
@@ -465,12 +465,12 @@ protected function save_settings( $submission, $data ) {
465465
/**
466466
* Set an error/notice for a setting.
467467
*
468-
* @param string $error_code The error code/slug.
469-
* @param string $error_message The error text/message.
470-
* @param string $type The error type.
471-
* @param bool $dismissible If notice is dismissible.
472-
* @param int $duration How long it's dismissible for.
473-
* @param string $icon Optional icon.
468+
* @param string $error_code The error code/slug.
469+
* @param string|array $error_message The error text/message.
470+
* @param string $type The error type.
471+
* @param bool $dismissible If notice is dismissible.
472+
* @param int $duration How long it's dismissible for.
473+
* @param string $icon Optional icon.
474474
*/
475475
public function add_admin_notice( $error_code, $error_message, $type = 'error', $dismissible = true, $duration = 0, $icon = null ) {
476476

php/class-analytics.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function maybe_send_pending_activation() {
160160
if ( ! empty( $pending['previous_version'] ) ) {
161161
$params['previous_version'] = $pending['previous_version'];
162162
}
163-
if ( isset( $pending['days_since_last_active'] ) && null !== $pending['days_since_last_active'] ) {
163+
if ( isset( $pending['days_since_last_active'] ) ) {
164164
$params['days_since_last_active'] = $pending['days_since_last_active'];
165165
}
166166

@@ -193,7 +193,7 @@ public function maybe_first_api_consumption( $attachment_id, $result ) {
193193
update_option( self::FIRST_API_FLAG, $cloud, false );
194194

195195
$asset_type = '';
196-
if ( is_array( $result ) && ! empty( $result['resource_type'] ) ) {
196+
if ( ! empty( $result['resource_type'] ) ) {
197197
$asset_type = $result['resource_type'];
198198
}
199199

@@ -260,7 +260,7 @@ public function track( $event_name, $category, $funnel_step = null, $params = ar
260260
'event_category' => $category,
261261
'event_timestamp' => gmdate( 'Y-m-d\TH:i:s\Z' ),
262262
),
263-
is_array( $params ) ? $params : array()
263+
$params
264264
);
265265

266266
if ( null !== $funnel_step ) {
@@ -365,7 +365,7 @@ protected function get_session_id() {
365365
*/
366366
protected function get_user_role() {
367367
$user = wp_get_current_user();
368-
if ( $user && ! empty( $user->roles ) ) {
368+
if ( ! empty( $user->roles ) ) {
369369
return (string) reset( $user->roles );
370370
}
371371

php/class-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Cache extends Settings_Component implements Setup {
3737
/**
3838
* File System
3939
*
40-
* @var File_System
40+
* @var File_System|null
4141
*/
4242
public $file_system;
4343

php/class-connect.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Connect extends Settings_Component implements Config, Setup, Notice {
4646
*
4747
* @since 0.1
4848
*
49-
* @var array
49+
* @var array|mixed
5050
*/
5151
public $usage;
5252

@@ -156,7 +156,7 @@ public function rest_test_connection( WP_REST_Request $request ) {
156156
/**
157157
* The analytics component.
158158
*
159-
* @var \Cloudinary\Analytics $analytics
159+
* @var \Cloudinary\Analytics|null $analytics
160160
*/
161161
$analytics = $this->plugin->get_component( 'analytics' );
162162
if ( $analytics ) {
@@ -248,7 +248,7 @@ public function rest_save_wizard( WP_REST_Request $request ) {
248248
/**
249249
* The analytics component.
250250
*
251-
* @var \Cloudinary\Analytics $analytics
251+
* @var \Cloudinary\Analytics|null $analytics
252252
*/
253253
$analytics = $this->plugin->get_component( 'analytics' );
254254
if ( $analytics ) {
@@ -774,7 +774,6 @@ public function usage_stats( $refresh = false ) {
774774
$stats = $this->api->usage();
775775
if (
776776
! is_wp_error( $stats )
777-
&& is_array( $stats )
778777
&& isset( $stats['media_limits'] )
779778
&& is_array( $stats['media_limits'] )
780779
) {

php/class-delivery.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function maybe_filter_out_metadata( $check, $object_id, $meta_key, $meta_
232232
/**
233233
* Filter out Cloudinary URLS and replace with local.
234234
*
235-
* @param string $content The content to filter.
235+
* @param mixed $content The content to filter.
236236
*
237237
* @return string
238238
*/
@@ -541,7 +541,7 @@ public function unsync_size_relationship( $attachment_id ) {
541541
*/
542542
public function get_sized( $attachment_id ) {
543543
static $sizes = array(), $registered_sizes;
544-
if ( ! $registered_sizes && is_callable( 'wp_get_registered_image_subsizes' ) ) {
544+
if ( ! $registered_sizes ) {
545545
$registered_sizes = wp_get_registered_image_subsizes();
546546
}
547547
if ( empty( $sizes[ $attachment_id ] ) ) {
@@ -1125,7 +1125,7 @@ function ( $tag ) use ( $content ) {
11251125
if ( empty( $cloudinary_url ) ) {
11261126
continue;
11271127
}
1128-
if ( ! empty( $relation['slashed'] ) && $relation['slashed'] ) {
1128+
if ( ! empty( $relation['slashed'] ) ) {
11291129
$aliases[ $base . '?_i=AA' ] = addcslashes( $cloudinary_url, '/' );
11301130
$aliases[ $base . '?' ] = addcslashes( $cloudinary_url . '&', '/' );
11311131
$aliases[ $base ] = addcslashes( $cloudinary_url, '/' );
@@ -1939,7 +1939,7 @@ protected function set_usability( $item, $auto_sync = null ) {
19391939
/**
19401940
* Sanitize a url.
19411941
*
1942-
* @param string $url URL to sanitize.
1942+
* @param mixed $url URL to sanitize.
19431943
*
19441944
* @return string|null
19451945
*/

php/class-media.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ class Media extends Settings_Component implements Setup {
5151
*/
5252
private $cloudinary_folder;
5353

54-
/**
55-
* Holds the found Cloudinary ID's
56-
*
57-
* @since 0.1
58-
*
59-
* @var array
60-
*/
61-
private $cloudinary_ids = array();
62-
6354
/**
6455
* Cloudinary credentials.
6556
*
@@ -98,7 +89,7 @@ class Media extends Settings_Component implements Setup {
9889
/**
9990
* Gallery instance.
10091
*
101-
* @var \Cloudinary\Media\Gallery
92+
* @var \Cloudinary\Media\Gallery|null
10293
*/
10394
public $gallery;
10495

@@ -112,7 +103,7 @@ class Media extends Settings_Component implements Setup {
112103
/**
113104
* Sync instance.
114105
*
115-
* @var \Cloudinary\Sync
106+
* @var \Cloudinary\Sync|null
116107
*/
117108
public $sync;
118109

@@ -1572,7 +1563,7 @@ public function prepare_size( $attachment_id, $size ) {
15721563
'full' => true,
15731564
);
15741565
}
1575-
} elseif ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) {
1566+
} elseif ( is_string( $size ) || 3 === count( $size ) ) {
15761567
$intermediate = image_get_intermediate_size( $attachment_id, $size );
15771568
// PDF's do not have intermediate URL.
15781569
if ( is_array( $intermediate ) && ! empty( $intermediate['url'] ) ) {
@@ -1646,7 +1637,7 @@ public function get_cloudinary_folder( $add_trailing_slash = true ) {
16461637
* @param int $attachment_id The Attachment ID.
16471638
* @param bool $suffixed Flag to get suffixed version of ID.
16481639
*
1649-
* @return string
1640+
* @return string|null
16501641
*/
16511642
public function get_public_id( $attachment_id, $suffixed = false ) {
16521643
// Check for a public_id.
@@ -1880,13 +1871,13 @@ public function convert_url( $url, $attachment_id, $transformations = array(), $
18801871
/**
18811872
* Get the responsive breakpoints for the image.
18821873
*
1883-
* @param array $sources The original sources array.
1884-
* @param array $size_array The size array.
1885-
* @param string $image_src The original image source.
1886-
* @param array $image_meta The image meta array.
1887-
* @param int $attachment_id The attachment id.
1874+
* @param array|false $sources The original sources array.
1875+
* @param array $size_array The size array.
1876+
* @param string $image_src The original image source.
1877+
* @param array $image_meta The image meta array.
1878+
* @param int $attachment_id The attachment id.
18881879
*
1889-
* @return array Altered or same sources array.
1880+
* @return array|false Altered or same sources array.
18901881
*/
18911882
public function image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
18921883

@@ -2107,7 +2098,7 @@ public function editor_assets() {
21072098
* @param array $asset The asset array data.
21082099
* @param string $public_id The cloudinary public id.
21092100
*
2110-
* @return int|WP_Error
2101+
* @return int
21112102
*/
21122103
private function create_attachment( $asset, $public_id ) {
21132104

@@ -3262,7 +3253,7 @@ public function is_enabled( $enabled ) {
32623253
*/
32633254
public function upgrade_settings( $previous_version, $new_version ) {
32643255

3265-
if ( 2.4 === $previous_version ) {
3256+
if ( version_compare( $previous_version, '2.5', '<' ) ) {
32663257
// Setup new data from old.
32673258
$images = get_option( 'cloudinary_global_transformations', array() );
32683259
$video = get_option( self::GLOBAL_VIDEO_TRANSFORMATIONS, array() );

php/class-plugin.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Cloudinary\Sync\Storage;
2121
use Cloudinary\UI\State;
2222
use const E_USER_WARNING;
23-
use const WPCOM_IS_VIP_ENV;
2423

2524
/**
2625
* Main plugin bootstrap file.
@@ -45,7 +44,7 @@ final class Plugin {
4544
/**
4645
* The core Settings object.
4746
*
48-
* @var Settings
47+
* @var Settings|null
4948
*/
5049
public $settings;
5150

@@ -147,7 +146,7 @@ public function plugins_loaded() {
147146
*
148147
* @param mixed $component The component.
149148
*
150-
* @return Admin|CLD_Assets|Connect|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL|null
149+
* @return Admin|CLD_Assets|Connect|Cron|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL|null
151150
*/
152151
public function get_component( $component ) {
153152
$return = null;
@@ -699,7 +698,7 @@ public function relative_path( $path, $start, $sep ) {
699698
* @return bool
700699
*/
701700
public function is_wpcom_vip_prod() {
702-
return ( defined( '\WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV );
701+
return ( defined( '\WPCOM_IS_VIP_ENV' ) && constant( '\WPCOM_IS_VIP_ENV' ) ); // @phpstan-ignore booleanAnd.rightAlwaysFalse
703702
}
704703

705704
/**
@@ -734,7 +733,7 @@ public function add_script_data( $slug, $value, $handle = null ) {
734733
* Output script data if set.
735734
*/
736735
public function print_script_data() {
737-
if ( ! isset( $this->settings ) || ! method_exists( $this->settings, 'get_param' ) ) {
736+
if ( ! isset( $this->settings ) ) {
738737
return;
739738
}
740739

php/class-settings.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,9 @@ public function set_value( $slug, $value ) {
503503
$set = true;
504504
}
505505
} else {
506-
$found = $this->find_setting( $slug );
507-
if ( $found ) {
508-
$storage_path = $found->get_param( self::META_KEYS['storage'], $found->get_slug() );
509-
$set = $this->set_value( $storage_path, $value );
510-
}
506+
$found = $this->find_setting( $slug );
507+
$storage_path = $found->get_param( self::META_KEYS['storage'], $found->get_slug() );
508+
$set = $this->set_value( $storage_path, $value );
511509
}
512510

513511
return $set;

0 commit comments

Comments
 (0)