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_Cache_Manager   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 269
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 2
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_manager() 0 9 2
A is_empty() 0 5 1
A clear() 0 5 1
A delete() 0 3 1
A has() 0 8 3
D has_valid() 0 27 9
B get() 0 14 5
B get_valid() 0 15 6
A set() 0 10 1
B get_record_expiration() 0 15 5
A purge() 0 5 1
A expire() 0 14 4

How to fix   Complexity   

Complex Class

Complex classes like FS_Cache_Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FS_Cache_Manager, and based on these observations, apply Extract Interface, too.

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.1.6
7
	 */
8
9
	if ( ! defined( 'ABSPATH' ) ) {
10
		exit;
11
	}
12
13
	class FS_Cache_Manager {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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
		 * @var FS_Option_Manager
16
		 */
17
		private $_options;
18
		/**
19
		 * @var FS_Logger
20
		 */
21
		private $_logger;
22
23
		/**
24
		 * @var FS_Cache_Manager[]
25
		 */
26
		private static $_MANAGERS = array();
27
28
		/**
29
		 * @author Vova Feldman (@svovaf)
30
		 * @since  1.1.3
31
		 *
32
		 * @param string $id
33
		 */
34
		private function __construct( $id ) {
35
			$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_cach_mngr_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
36
37
			$this->_logger->entrance();
38
			$this->_logger->log( 'id = ' . $id );
39
40
			$this->_options = FS_Option_Manager::get_manager( $id, true );
41
		}
42
43
		/**
44
		 * @author Vova Feldman (@svovaf)
45
		 * @since  1.1.6
46
		 *
47
		 * @param $id
48
		 *
49
		 * @return FS_Cache_Manager
50
		 */
51
		static function get_manager( $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...
52
			$id = strtolower( $id );
53
54
			if ( ! isset( self::$_MANAGERS[ $id ] ) ) {
55
				self::$_MANAGERS[ $id ] = new FS_Cache_Manager( $id );
56
			}
57
58
			return self::$_MANAGERS[ $id ];
59
		}
60
61
		/**
62
		 * @author Vova Feldman (@svovaf)
63
		 * @since  1.1.6
64
		 *
65
		 * @return bool
66
		 */
67
		function is_empty() {
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...
68
			$this->_logger->entrance();
69
70
			return $this->_options->is_empty();
71
		}
72
73
		/**
74
		 * @author Vova Feldman (@svovaf)
75
		 * @since  1.1.6
76
		 */
77
		function clear() {
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...
78
			$this->_logger->entrance();
79
80
			$this->_options->clear( true );
81
		}
82
83
		/**
84
		 * Delete cache manager from DB.
85
		 *
86
		 * @author Vova Feldman (@svovaf)
87
		 * @since  1.0.9
88
		 */
89
		function delete() {
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...
90
			$this->_options->delete();
91
		}
92
93
		/**
94
		 * Check if there's a cached item.
95
		 *
96
		 * @author Vova Feldman (@svovaf)
97
		 * @since  1.1.6
98
		 *
99
		 * @param string $key
100
		 *
101
		 * @return bool
102
		 */
103
		function has( $key ) {
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...
104
			$cache_entry = $this->_options->get_option( $key, false );
105
106
			return ( is_object( $cache_entry ) &&
107
			         isset( $cache_entry->timestamp ) &&
108
			         is_numeric( $cache_entry->timestamp )
109
			);
110
		}
111
112
		/**
113
		 * Check if there's a valid cached item.
114
		 *
115
		 * @author Vova Feldman (@svovaf)
116
		 * @since  1.1.6
117
		 *
118
		 * @param string   $key
119
		 * @param null|int $expiration Since 1.2.2.7
120
		 *
121
		 * @return bool
122
		 */
123
		function has_valid( $key, $expiration = null ) {
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...
124
			$cache_entry = $this->_options->get_option( $key, false );
125
126
			$is_valid = ( is_object( $cache_entry ) &&
127
			              isset( $cache_entry->timestamp ) &&
128
			              is_numeric( $cache_entry->timestamp ) &&
129
			              $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME
130
			);
131
132
			if ( $is_valid &&
133
			     is_numeric( $expiration ) &&
134
			     isset( $cache_entry->created ) &&
135
			     is_numeric( $cache_entry->created ) &&
136
			     $cache_entry->created + $expiration < WP_FS__SCRIPT_START_TIME
137
			) {
138
				/**
139
				 * Even if the cache is still valid, since we are checking for validity
140
				 * with an explicit expiration period, if the period has past, return
141
				 * `false` as if the cache is invalid.
142
				 *
143
				 * @since 1.2.2.7
144
				 */
145
				$is_valid = false;
146
			}
147
148
			return $is_valid;
149
		}
150
151
		/**
152
		 * @author Vova Feldman (@svovaf)
153
		 * @since  1.1.6
154
		 *
155
		 * @param string $key
156
		 * @param mixed  $default
157
		 *
158
		 * @return mixed
159
		 */
160
		function get( $key, $default = null ) {
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...
161
			$this->_logger->entrance( 'key = ' . $key );
162
163
			$cache_entry = $this->_options->get_option( $key, false );
164
165
			if ( is_object( $cache_entry ) &&
166
			     isset( $cache_entry->timestamp ) &&
167
			     is_numeric( $cache_entry->timestamp )
168
			) {
169
                return $cache_entry->result;
170
            }
171
172
			return is_object($default) ? clone $default : $default;
173
		}
174
175
		/**
176
		 * @author Vova Feldman (@svovaf)
177
		 * @since  1.1.6
178
		 *
179
		 * @param string $key
180
		 * @param mixed  $default
181
		 *
182
		 * @return mixed
183
		 */
184
		function get_valid( $key, $default = null ) {
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...
185
			$this->_logger->entrance( 'key = ' . $key );
186
187
			$cache_entry = $this->_options->get_option( $key, false );
188
189
			if ( is_object( $cache_entry ) &&
190
			     isset( $cache_entry->timestamp ) &&
191
			     is_numeric( $cache_entry->timestamp ) &&
192
			     $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME
193
			) {
194
				return $cache_entry->result;
195
			}
196
197
            return is_object($default) ? clone $default : $default;
198
		}
199
200
		/**
201
		 * @author Vova Feldman (@svovaf)
202
		 * @since  1.1.6
203
		 *
204
		 * @param string $key
205
		 * @param mixed  $value
206
		 * @param int    $expiration
207
		 */
208
		function set( $key, $value, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
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...
209
			$this->_logger->entrance( 'key = ' . $key );
210
211
			$cache_entry = new stdClass();
212
213
			$cache_entry->result    = $value;
214
			$cache_entry->created   = WP_FS__SCRIPT_START_TIME;
215
			$cache_entry->timestamp = WP_FS__SCRIPT_START_TIME + $expiration;
216
			$this->_options->set_option( $key, $cache_entry, true );
217
		}
218
219
		/**
220
		 * Get cached record expiration, or false if not cached or expired.
221
		 *
222
		 * @author Vova Feldman (@svovaf)
223
		 * @since  1.1.7.3
224
		 *
225
		 * @param string $key
226
		 *
227
		 * @return bool|int
228
		 */
229
		function get_record_expiration( $key ) {
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...
230
			$this->_logger->entrance( 'key = ' . $key );
231
232
			$cache_entry = $this->_options->get_option( $key, false );
233
234
			if ( is_object( $cache_entry ) &&
235
			     isset( $cache_entry->timestamp ) &&
236
			     is_numeric( $cache_entry->timestamp ) &&
237
			     $cache_entry->timestamp > WP_FS__SCRIPT_START_TIME
238
			) {
239
				return $cache_entry->timestamp;
240
			}
241
242
			return false;
243
		}
244
245
		/**
246
		 * Purge cached item.
247
		 *
248
		 * @author Vova Feldman (@svovaf)
249
		 * @since  1.1.6
250
		 *
251
		 * @param string $key
252
		 */
253
		function purge( $key ) {
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...
254
			$this->_logger->entrance( 'key = ' . $key );
255
256
			$this->_options->unset_option( $key, true );
257
		}
258
259
		/**
260
		 * Set cached item as expired.
261
		 *
262
		 * @author Vova Feldman (@svovaf)
263
		 * @since  1.2.2.7
264
		 *
265
		 * @param string $key
266
		 */
267
		function expire( $key ) {
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...
268
			$this->_logger->entrance( 'key = ' . $key );
269
270
			$cache_entry = $this->_options->get_option( $key, false );
271
272
			if ( is_object( $cache_entry ) &&
273
			     isset( $cache_entry->timestamp ) &&
274
			     is_numeric( $cache_entry->timestamp ))
275
			{
276
				// Set to expired.
277
				$cache_entry->timestamp = WP_FS__SCRIPT_START_TIME;
278
				$this->_options->set_option( $key, $cache_entry, true );
279
			}
280
		}
281
	}