GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bb6361...a2a740 )
by Brad
04:28
created

FS_Plugin_Manager::load()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 38
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 20
nop 0
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	/**
3
	 * @package     Freemius
4
	 * @copyright   Copyright (c) 2015, Freemius, Inc.
5
	 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
	 * @since       1.0.6
7
	 */
8
9
	if ( ! defined( 'ABSPATH' ) ) {
10
		exit;
11
	}
12
13
	class FS_Plugin_Manager {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
		/**
15
		 * @since 1.2.2
16
		 *
17
		 * @var string|number
18
		 */
19
		protected $_module_id;
20
		/**
21
		 * @since 1.2.2
22
		 *
23
		 * @var FS_Plugin
24
		 */
25
		protected $_module;
26
27
		/**
28
		 * @var FS_Plugin_Manager[]
29
		 */
30
		private static $_instances = array();
31
		/**
32
		 * @var FS_Logger
33
		 */
34
		protected $_logger;
35
36
		/**
37
		 * Option names
38
		 *
39
		 * @author Leo Fajardo (@leorw)
40
		 * @since  1.2.2
41
		 */
42
		const OPTION_NAME_PLUGINS = 'plugins';
43
		const OPTION_NAME_THEMES  = 'themes';
44
45
		/**
46
		 * @param  string|number $module_id
47
		 *
48
		 * @return FS_Plugin_Manager
49
		 */
50
		static function instance( $module_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
			$key = 'm_' . $module_id;
52
53
			if ( ! isset( self::$_instances[ $key ] ) ) {
54
				self::$_instances[ $key ] = new FS_Plugin_Manager( $module_id );
55
			}
56
57
			return self::$_instances[ $key ];
58
        }
59
60
		/**
61
		 * @param string|number $module_id
62
		 */
63
		protected function __construct( $module_id ) {
64
			$this->_logger    = FS_Logger::get_logger( WP_FS__SLUG . '_' . $module_id . '_' . 'plugins', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
65
			$this->_module_id = $module_id;
66
67
			$this->load();
68
		}
69
70
		protected function get_option_manager() {
71
			return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
72
		}
73
74
		/**
75
		 * @author Leo Fajardo (@leorw)
76
		 * @since  1.2.2
77
		 *
78
		 * @param  string|false $module_type "plugin", "theme", or "false" for all modules.
79
		 *
80
		 * @return array
81
		 */
82
		protected function get_all_modules( $module_type = false ) {
83
			$option_manager = $this->get_option_manager();
84
85
			if ( false !== $module_type ) {
86
				return $option_manager->get_option( $module_type . 's', array() );
87
			}
88
89
			return array(
90
				self::OPTION_NAME_PLUGINS => $option_manager->get_option( self::OPTION_NAME_PLUGINS, array() ),
91
				self::OPTION_NAME_THEMES  => $option_manager->get_option( self::OPTION_NAME_THEMES, array() ),
92
			);
93
		}
94
95
		/**
96
		 * Load plugin data from local DB.
97
		 *
98
		 * @author Vova Feldman (@svovaf)
99
		 * @since  1.0.6
100
		 */
101
		function load() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
			$all_modules = $this->get_all_modules();
103
104
			if ( ! is_numeric( $this->_module_id ) ) {
105
				unset( $all_modules[ self::OPTION_NAME_THEMES ] );
106
			}
107
108
			foreach ( $all_modules as $modules ) {
0 ignored issues
show
Bug introduced by
The expression $all_modules of type integer|double|string|nu...boolean|resource|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
109
				/**
110
				 * @since 1.2.2
111
				 *
112
				 * @var $modules FS_Plugin[]
113
				 */
114
				foreach ( $modules as $module ) {
115
					$found_module = false;
116
117
					/**
118
					 * If module ID is not numeric, it must be a plugin's slug.
119
					 *
120
					 * @author Leo Fajardo (@leorw)
121
					 * @since  1.2.2
122
					 */
123
					if ( ! is_numeric( $this->_module_id ) ) {
124
						if ( $this->_module_id === $module->slug ) {
125
							$this->_module_id = $module->id;
126
							$found_module     = true;
127
						}
128
					} else if ( $this->_module_id == $module->id ) {
129
						$found_module = true;
130
					}
131
132
					if ( $found_module ) {
133
						$this->_module = $module;
134
						break;
135
					}
136
				}
137
			}
138
		}
139
140
		/**
141
		 * Store plugin on local DB.
142
		 *
143
		 * @author Vova Feldman (@svovaf)
144
		 * @since  1.0.6
145
		 *
146
		 * @param bool|FS_Plugin $module
147
		 * @param bool           $flush
148
		 *
149
		 * @return bool|\FS_Plugin
150
		 */
151
		function store( $module = false, $flush = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
			if ( false !== $module ) {
153
				$this->_module = $module;
0 ignored issues
show
Documentation Bug introduced by
It seems like $module can also be of type boolean. However, the property $_module is declared as type object<FS_Plugin>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
154
			}
155
156
			$all_modules = $this->get_all_modules( $this->_module->type );
157
			$all_modules[ $this->_module->slug ] = $this->_module;
158
159
			$options_manager = $this->get_option_manager();
160
			$options_manager->set_option( $this->_module->type . 's', $all_modules, $flush );
161
162
			return $this->_module;
163
		}
164
165
		/**
166
		 * Update local plugin data if different.
167
		 *
168
		 * @author Vova Feldman (@svovaf)
169
		 * @since  1.0.6
170
		 *
171
		 * @param \FS_Plugin $plugin
172
		 * @param bool       $store
173
		 *
174
		 * @return bool True if plugin was updated.
175
		 */
176
		function update( FS_Plugin $plugin, $store = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
177
			if ( ! ($this->_module instanceof FS_Plugin ) ||
178
			     $this->_module->slug != $plugin->slug ||
179
			     $this->_module->public_key != $plugin->public_key ||
180
			     $this->_module->secret_key != $plugin->secret_key ||
181
			     $this->_module->parent_plugin_id != $plugin->parent_plugin_id ||
182
			     $this->_module->title != $plugin->title
183
			) {
184
				$this->store( $plugin, $store );
185
186
				return true;
187
			}
188
189
			return false;
190
		}
191
192
		/**
193
		 * @author Vova Feldman (@svovaf)
194
		 * @since  1.0.6
195
		 *
196
		 * @param FS_Plugin $plugin
197
		 * @param bool      $store
198
		 */
199
		function set( FS_Plugin $plugin, $store = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
			$this->_module = $plugin;
201
202
			if ( $store ) {
203
				$this->store();
204
			}
205
		}
206
207
		/**
208
		 * @author Vova Feldman (@svovaf)
209
		 * @since  1.0.6
210
		 *
211
		 * @return bool|\FS_Plugin
212
		 */
213
		function get() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
214
			return isset( $this->_module ) ?
215
				$this->_module :
216
				false;
217
		}
218
219
220
	}