Skip to content

Commit 2bef074

Browse files
committed
[static-analysis] Fix rule - Access to an undefined property object .
1 parent 28776a0 commit 2bef074

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
@@ -994,10 +994,13 @@ private static function migrate_install_plan_to_plan_id( FS_Storage $storage, $b
994994
$module_type = $storage->get_module_type();
995995
$module_slug = $storage->get_module_slug();
996996

997+
/**
998+
* @var FS_Site[] $installs
999+
*/
9971000
$installs = self::get_all_sites( $module_type, $blog_id );
9981001
$install = isset( $installs[ $module_slug ] ) ? $installs[ $module_slug ] : null;
9991002

1000-
if ( ! is_object( $install ) ) {
1003+
if ( is_null( $install ) ) {
10011004
return;
10021005
}
10031006

@@ -1865,7 +1868,6 @@ private function unregister_uninstall_hook() {
18651868
*/
18661869
private function clear_module_main_file_cache( $store_prev_path = true ) {
18671870
if ( ! isset( $this->_storage->plugin_main_file ) ||
1868-
! is_object( $this->_storage->plugin_main_file ) ||
18691871
empty( $this->_storage->plugin_main_file->path )
18701872
) {
18711873
return;
@@ -1882,6 +1884,9 @@ private function clear_module_main_file_cache( $store_prev_path = true ) {
18821884
*/
18831885
unset( $this->_storage->plugin_main_file->path );
18841886
} else {
1887+
/**
1888+
* @var stdClass $plugin_main_file
1889+
*/
18851890
$plugin_main_file = clone $this->_storage->plugin_main_file;
18861891

18871892
// Store cached path (2nd layer cache).
@@ -17130,12 +17135,14 @@ function opt_in(
1713017135
* @link https://themes.trac.wordpress.org/ticket/46134#comment:9
1713117136
* @link https://themes.trac.wordpress.org/ticket/46134#comment:12
1713217137
* @link https://themes.trac.wordpress.org/ticket/46134#comment:14
17138+
*
17139+
* @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
1713317140
*/
1713417141
$decoded = is_string( $response['body'] ) ?
1713517142
json_decode( $response['body'] ) :
1713617143
null;
1713717144

17138-
if ( empty( $decoded ) && ! is_object( $decoded ) ) {
17145+
if ( ! is_object( $decoded ) ) {
1713917146
return false;
1714017147
}
1714117148

@@ -19595,9 +19602,9 @@ private function _store_site( $store = true, $network_level_or_blog_id = null, F
1959519602
$site = $this->_site;
1959619603
}
1959719604

19598-
if ( ! is_object( $site ) || empty( $site->id ) ) {
19605+
// @phpstan-ignore-next-line Variable $site in isset() always exists and is not nullable
19606+
if ( !isset( $site ) || !is_object( $site ) || empty( $site->id ) ) {
1959919607
$this->_logger->error( "Empty install ID, can't store site." );
19600-
1960119608
return;
1960219609
}
1960319610

@@ -20044,7 +20051,11 @@ private function _handle_account_user_sync() {
2004420051

2004520052
$api = $this->get_api_user_scope();
2004620053

20047-
// Get user's information.
20054+
/**
20055+
* Get user's information.
20056+
*
20057+
* @var FS_User $user
20058+
*/
2004820059
$user = $api->get( '/', true );
2004920060

2005020061
if ( isset( $user->id ) ) {
@@ -20973,7 +20984,7 @@ private function _sync_plugin_license(
2097320984

2097420985
// Find the current context install.
2097520986
$site = null;
20976-
if ( is_array( $result ) ) {
20987+
if ( is_object( $result ) && is_array( $result->installs ) ) {
2097720988
foreach ( $result->installs as $install ) {
2097820989
if ( $install->id == $this->_site->id ) {
2097920990
$site = new FS_Site( $install );
@@ -22382,7 +22393,7 @@ private function init_change_owner( $candidate_email, $transfer_type ) {
2238222393
/**
2238322394
* Retrieves all module sites.
2238422395
*
22385-
* @return array $all_sites Get all module sites.
22396+
* @return array $all_sites
2238622397
*/
2238722398
public static function get_all_modules_sites() {
2238822399
$all_sites = [];
@@ -22525,8 +22536,8 @@ private function complete_change_owner() {
2252522536
* @author Leo Fajardo (@leorw)
2252622537
* @since 2.3.2
2252722538
*
22528-
* @param number $user_id
22529-
* @param string[]|int[] $install_ids_by_slug_map
22539+
* @param number $user_id
22540+
* @param array<string, int> $install_ids_by_slug_map
2253022541
*
2253122542
*/
2253222543
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)