Skip to content

Commit f4a6e68

Browse files
committed
[static-analysis] Fix rule - Access to an undefined property object .
1 parent c7ba082 commit f4a6e68

File tree

8 files changed

+82
-33
lines changed

8 files changed

+82
-33
lines changed

includes/class-freemius.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,13 @@ private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $b
987987
$module_type = $storage->get_module_type();
988988
$module_slug = $storage->get_module_slug();
989989

990+
/**
991+
* @var FS_Site[] $installs
992+
*/
990993
$installs = self::get_all_sites( $module_type, $blog_id );
991994
$install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
992995

993-
if ( ! is_object( $install ) ) {
996+
if ( is_null( $install ) ) {
994997
return;
995998
}
996999

@@ -1872,7 +1875,6 @@ private function unregister_uninstall_hook() {
18721875
*/
18731876
private function clear_module_main_file_cache( $store_prev_path = true ) {
18741877
if ( ! isset( $this->_storage->plugin_main_file ) ||
1875-
! is_object( $this->_storage->plugin_main_file ) ||
18761878
empty( $this->_storage->plugin_main_file->path )
18771879
) {
18781880
return;
@@ -1889,6 +1891,9 @@ private function clear_module_main_file_cache( $store_prev_path = true ) {
18891891
*/
18901892
unset( $this->_storage->plugin_main_file->path );
18911893
} else {
1894+
/**
1895+
* @var stdClass $plugin_main_file
1896+
*/
18921897
$plugin_main_file = clone $this->_storage->plugin_main_file;
18931898

18941899
// Store cached path (2nd layer cache).
@@ -17141,12 +17146,14 @@ function opt_in(
1714117146
* @link https://themes.trac.wordpress.org/ticket/46134#comment:9
1714217147
* @link https://themes.trac.wordpress.org/ticket/46134#comment:12
1714317148
* @link https://themes.trac.wordpress.org/ticket/46134#comment:14
17149+
*
17150+
* @var stdClass $decoded The decoded object is expected to be UserPlugin entity, but since we do not have this Entity in the SDK, we made this a StdClass
1714417151
*/
1714517152
$decoded = is_string( $response['body'] ) ?
1714617153
json_decode( $response['body'] ) :
1714717154
null;
1714817155

17149-
if ( empty( $decoded ) && ! is_object( $decoded ) ) {
17156+
if ( ! is_object( $decoded ) ) {
1715017157
return false;
1715117158
}
1715217159

@@ -19634,9 +19641,9 @@ private function _store_site( $store = true, $network_level_or_blog_id = null, F
1963419641
$site = $this->_site;
1963519642
}
1963619643

19637-
if ( ! is_object( $site ) || empty( $site->id ) ) {
19644+
// @phpstan-ignore-next-line Variable $site in isset() always exists and is not nullable
19645+
if ( !isset( $site ) || !is_object( $site ) || empty( $site->id ) ) {
1963819646
$this->_logger->error( "Empty install ID, can't store site." );
19639-
1964019647
return;
1964119648
}
1964219649

@@ -20083,7 +20090,11 @@ private function _handle_account_user_sync() {
2008320090

2008420091
$api = $this->get_api_user_scope();
2008520092

20086-
// Get user's information.
20093+
/**
20094+
* Get user's information.
20095+
*
20096+
* @var FS_User $user
20097+
*/
2008720098
$user = $api->get( '/', true );
2008820099

2008920100
if ( isset( $user->id ) ) {
@@ -21012,7 +21023,7 @@ private function _sync_plugin_license(
2101221023

2101321024
// Find the current context install.
2101421025
$site = null;
21015-
if ( is_array( $result ) ) {
21026+
if ( is_object( $result ) && is_array( $result->installs ) ) {
2101621027
foreach ( $result->installs as $install ) {
2101721028
if ( $install->id == $this->_site->id ) {
2101821029
$site = new FS_Site( $install );
@@ -22442,7 +22453,7 @@ private function init_change_owner( $candidate_email, $transfer_type ) {
2244222453
/**
2244322454
* Retrieves all module sites.
2244422455
*
22445-
* @return array $all_sites Get all module sites.
22456+
* @return array $all_sites
2244622457
*/
2244722458
public static function get_all_modules_sites() {
2244822459
$all_sites = [];
@@ -22585,8 +22596,8 @@ private function complete_change_owner() {
2258522596
* @author Leo Fajardo (@leorw)
2258622597
* @since 2.3.2
2258722598
*
22588-
* @param number $user_id
22589-
* @param string[]|int[] $install_ids_by_slug_map
22599+
* @param number $user_id
22600+
* @param array<string, int> $install_ids_by_slug_map
2259022601
*
2259122602
*/
2259222603
private function complete_ownership_change_by_license( $user_id, $install_ids_by_slug_map ) {

includes/class-fs-plugin-updater.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ function pre_set_site_transient_update_plugins_filter( $transient_data ) {
534534
return $transient_data;
535535
}
536536

537-
if ( defined( 'WP_FS__UNINSTALL_MODE' ) ) {
537+
// @phpstan-ignore-next-line
538+
if ( empty( $transient_data ) || defined( 'WP_FS__UNINSTALL_MODE' ) ) {
538539
return $transient_data;
539540
}
540541

@@ -869,7 +870,7 @@ function delete_update_data() {
869870
* @param string $action
870871
* @param object $args
871872
*
872-
* @return object The plugin information or false on failure.
873+
* @return bool|object The plugin information or false on failure.
873874
*/
874875
static function _fetch_plugin_info_from_repository( $action, $args ) {
875876
$url = $http_url = 'http://api.wordpress.org/plugins/info/1.2/';
@@ -889,7 +890,7 @@ static function _fetch_plugin_info_from_repository( $action, $args ) {
889890
$request = wp_remote_get( $url, array( 'timeout' => 15 ) );
890891

891892
if ( is_wp_error( $request ) ) {
892-
return;
893+
return false;
893894
}
894895

895896
$res = json_decode( wp_remote_retrieve_body( $request ), true );
@@ -900,7 +901,7 @@ static function _fetch_plugin_info_from_repository( $action, $args ) {
900901
}
901902

902903
if ( ! is_object( $res ) || isset( $res->error ) ) {
903-
return;
904+
return false;
904905
}
905906

906907
return $res;
@@ -1110,7 +1111,11 @@ function plugins_api_filter( $data, $action = '', $args = null ) {
11101111
if ( ! $plugin_in_repo ) {
11111112
$data = $args;
11121113

1113-
// Fetch as much as possible info from local files.
1114+
/**
1115+
* Fetch as much as possible info from local files.
1116+
*
1117+
* @var stdClass $data
1118+
*/
11141119
$plugin_local_data = $this->_fs->get_plugin_data();
11151120
$data->name = $plugin_local_data['Name'];
11161121
$data->author = $plugin_local_data['Author'];
@@ -1130,7 +1135,12 @@ function plugins_api_filter( $data, $action = '', $args = null ) {
11301135
$addon_version :
11311136
$this->_fs->get_plugin_version();
11321137

1133-
// Get plugin's newest update.
1138+
/**
1139+
* Get plugin's newest update.
1140+
*
1141+
* @var FS_Plugin_Tag $new_version
1142+
* @var object $data
1143+
*/
11341144
$new_version = $this->get_latest_download_details( $is_addon ? $addon->id : false, $plugin_version );
11351145

11361146
if ( ! is_object( $new_version ) || empty( $new_version->version ) ) {

includes/fs-essential-functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ function fs_update_sdk_newest_version( $sdk_relative_path, $plugin_file = false
250250

251251
global $fs_active_plugins;
252252

253+
/**
254+
* @var stdClass $newest_sdk
255+
*/
253256
$newest_sdk = $fs_active_plugins->plugins[ $sdk_relative_path ];
254257

255258
if ( ! is_string( $plugin_file ) ) {

includes/fs-plugin-info-dialog.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ function __construct( Freemius $fs ) {
7878
* @author Vova Feldman (@svovaf)
7979
* @since 1.0.6
8080
*
81-
* @param array $data
82-
* @param string $action
83-
* @param object|null $args
81+
* @param object|array $data
82+
* @param string $action
83+
* @param object|null $args
8484
*
8585
* @return array|null
8686
*/
@@ -188,6 +188,9 @@ function _get_addon_info_filter( $data, $action = '', $args = null ) {
188188

189189
$latest = null;
190190

191+
/**
192+
* @var stdClass $data
193+
*/
191194
if ( ! $has_paid_plan && $selected_addon->is_wp_org_compliant ) {
192195
$repo_data = FS_Plugin_Updater::_fetch_plugin_info_from_repository(
193196
'plugin_information', (object) array(
@@ -202,7 +205,6 @@ function _get_addon_info_filter( $data, $action = '', $args = null ) {
202205
) );
203206

204207
if ( ! is_object( $repo_data ) ) {
205-
$data = $repo_data;
206208
$data->wp_org_missing = false;
207209
} else {
208210
// Couldn't find plugin on .org.
@@ -535,6 +537,9 @@ private function get_checkout_cta( $api, $plan = false ) {
535537
restore_current_blog();
536538
}
537539

540+
/**
541+
* @var stdClass $api
542+
*/
538543
return '<a class="button button-primary fs-checkout-button right" href="' . $addon_checkout_url . '" target="_parent">' .
539544
esc_html( ! $plan->has_trial() ?
540545
(
@@ -763,6 +768,9 @@ private function get_plugin_actions( $api ) {
763768

764769
$download_latest_action = '';
765770

771+
/**
772+
* @var stdClass $api
773+
*/
766774
if (
767775
! empty( $api->download_link ) &&
768776
( $can_download_free_version || $can_download_premium_version )
@@ -1092,6 +1100,7 @@ function install_plugin_information() {
10921100

10931101
/**
10941102
* @var FS_Plugin_Plan $plan
1103+
* @var stdClass $api
10951104
*/
10961105
?>
10971106
<?php $first_pricing = $plan->pricing[0] ?>
@@ -1310,6 +1319,11 @@ class="fs-annual-discount"><?php printf(
13101319
<div>
13111320
<h3><?php fs_echo_inline( 'Details', 'details', $api->slug ) ?></h3>
13121321
<ul>
1322+
<?php
1323+
/**
1324+
* @var stdClass $api
1325+
*/
1326+
?>
13131327
<?php if ( ! empty( $api->version ) ) { ?>
13141328
<li>
13151329
<strong><?php fs_esc_html_echo_x_inline( 'Version', 'product version', 'version', $api->slug ); ?>

includes/managers/class-fs-cache-manager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ function has_valid( $key, $expiration = null ) {
160160
function get( $key, $default = null ) {
161161
$this->_logger->entrance( 'key = ' . $key );
162162

163+
/**
164+
* @var stdClass $cache_entry
165+
*/
163166
$cache_entry = $this->_options->get_option( $key, false );
164167

165168
if ( is_object( $cache_entry ) &&
@@ -184,6 +187,9 @@ function get( $key, $default = null ) {
184187
function get_valid( $key, $default = null ) {
185188
$this->_logger->entrance( 'key = ' . $key );
186189

190+
/**
191+
* @var stdClass $cache_entry
192+
*/
187193
$cache_entry = $this->_options->get_option( $key, false );
188194

189195
if ( is_object( $cache_entry ) &&
@@ -271,6 +277,9 @@ function purge( $key ) {
271277
function update_expiration( $key, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
272278
$this->_logger->entrance( 'key = ' . $key );
273279

280+
/**
281+
* @var stdClass $cache_entry
282+
*/
274283
$cache_entry = $this->_options->get_option( $key, false );
275284

276285
if ( ! is_object( $cache_entry ) ||

phpstan.neon

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,3 @@ parameters:
2020
message: '#Variable \$VARS might not be defined#'
2121
paths:
2222
- templates/*
23-
-
24-
message: '#Access to an undefined property object#'
25-
paths:
26-
- start.php
27-
- includes/class-freemius.php
28-
- includes/fs-plugin-info-dialog.php
29-
- includes/fs-essential-functions.php
30-
- includes/class-fs-plugin-updater.php
31-
- includes/managers/class-fs-cache-manager.php

start.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,17 @@ function_exists( 'wp_is_json_request' ) &&
102102
$fs_active_plugins = new stdClass();
103103
}
104104

105+
/**
106+
* @var stdClass $fs_active_plugins
107+
*/
105108
if ( ! isset( $fs_active_plugins->plugins ) ) {
106109
$fs_active_plugins->plugins = array();
107110
}
108111
}
109112

113+
/**
114+
* @var stdClass $fs_active_plugins
115+
*/
110116
if ( empty( $fs_active_plugins->abspath ) ) {
111117
/**
112118
* Store the WP install absolute path reference to identify environment change
@@ -229,6 +235,9 @@ function_exists( 'wp_is_json_request' ) &&
229235
$is_newest_sdk_plugin_active = is_plugin_active( $fs_newest_sdk->plugin_path );
230236
} else {
231237
$current_theme = wp_get_theme();
238+
/**
239+
* @var stdClass $fs_newest_sdk
240+
*/
232241
$is_newest_sdk_plugin_active = ( $current_theme->stylesheet === $fs_newest_sdk->plugin_path );
233242

234243
$current_theme_parent = $current_theme->parent();

templates/plugin-info/features.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
$features_plan_map = array();
2323
foreach ( $plans as $plan ) {
24+
/**
25+
* @var FS_Plugin_Plan $plan
26+
*/
2427
if (!empty($plan->features) && is_array($plan->features)) {
2528
foreach ( $plan->features as $feature ) {
26-
$features_plan_map[ $feature->id ] = array( 'feature' => $feature, 'plans' => array() );
27-
28-
if ( ! empty( $plan->id ) ) {
29-
$features_plan_map[ $feature->id ]['plans'][ $plan->id ] = $feature;
29+
if ( ! isset( $features_plan_map[ $feature->id ] ) ) {
30+
$features_plan_map[ $feature->id ] = array( 'feature' => $feature, 'plans' => array() );
3031
}
32+
$features_plan_map[ $feature->id ]['plans'][ $plan->id ] = $feature;
3133
}
3234
}
3335

0 commit comments

Comments
 (0)