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
Pull Request — master (#1034)
by Tom
02:28
created

Notices::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Class for managing notices
7
 *
8
 * Notices are messages along with an associated context, by default
9
 * they are stored in the db and thus persist between page loads.
10
 */
11
class Notices {
12
13
	/**
14
	 * The array of notice messages
15
	 *
16
	 * This is a multidimensional array of
17
	 * `array( context => array( 'message' ) );``
18
	 *
19
	 * @var array
20
	 */
21
	private $notices = array();
22
23
	/**
24
	 * Contains the instantiated Notices instance
25
	 *
26
	 * @var Path $this->get_instance
27
	 */
28
	private static $instance;
29
30
	/**
31
	 * Protected constructor to prevent creating a new instance of the
32
	 * *Singleton* via the `new` operator from outside of this class.
33
	 */
34
	protected function __construct() {}
35
36
	/**
37
	 * Private clone method to prevent cloning of the instance of the
38
	 * *Singleton* instance.
39
	 */
40
	private function __clone() {}
41
42
	/**
43
	 * Private unserialize method to prevent unserializing of the *Singleton*
44
	 * instance.
45
	 */
46
	private function __wakeup() {}
47
48
	/**
49
	 * Returns the *Singleton* instance of this class.
50
	 *
51
	 * @staticvar Notices $instance The *Singleton* instances of this class.
52
	 *
53
	 * @return Notices The *Singleton* instance.
54
	 */
55
	public static function get_instance() {
56
57
		if ( ! ( self::$instance instanceof Notices ) ) {
58
			self::$instance = new Notices();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \HM\BackUpWordPress\Notices() of type object<HM\BackUpWordPress\Notices> is incompatible with the declared type object<HM\BackUpWordPress\Path> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
		}
60
61
		return self::$instance;
62
63
	}
64
65
	/**
66
	 * Set an array of notice messages for a specific context
67
	 *
68
	 * @param string  $context    The context of the notice message
69
	 * @param array   $messages   The array of messages
70
	 * @param boolean $persistent Whether the notice should persist via the database. Defaults to true.
71
	 */
72
	public function set_notices( $context, array $messages, $persistent = true ) {
73
74
		// Clear any empty messages and avoid duplicates
75
		$messages = array_unique( array_filter( $messages ) );
76
77
		if ( empty( $context ) || empty( $messages ) ) {
78
			return false;
79
		}
80
81
		$this->notices[ $context ] = array_merge( $this->get_notices( $context ), $messages );
82
83
		if ( $persistent ) {
84
85
			$new_notices = $notices = $this->get_persistent_notices();
86
87
			// Make sure we merge in any existing notices
88
			if ( ! empty( $notices[ $context ] ) ) {
89
				$new_notices[ $context ] = array_merge( $notices[ $context ], $messages );
90
			} else {
91
				$new_notices[ $context ] = $messages;
92
			}
93
94
			// Only update the database if the notice array has changed
95
			if ( $new_notices !== $notices ) {
96
				update_option( 'hmbkp_notices', $new_notices );
97
			}
98
		}
99
100
		return true;
101
102
	}
103
104
	/**
105
	 * Fetch an array of notices messages
106
	 *
107
	 * If you specify a context then you'll just get messages for that context otherwise
108
	 * you get multidimensional array of all contexts and their messages.
109
	 *
110
	 * @param string $context The context that you'd like the messages for
111
	 *
112
	 * @return array The array of notice messages
113
	 */
114
	public function get_notices( $context = '' ) {
115
116
		$notices = $this->get_all_notices();
117
118
		if ( $notices ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notices of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
119
120
			if ( $context ) {
121
				return isset( $notices[ $context ] ) ? $notices[ $context ] : array();
122
			}
123
124
			return $notices;
125
		}
126
127
		return array();
128
129
	}
130
131
	/**
132
	 * Get both standard and persistent notices
133
	 *
134
	 * @return array The array of contexts and notices
135
	 */
136
	private function get_all_notices() {
137
		return array_map( 'array_unique', array_merge_recursive( $this->notices, $this->get_persistent_notices() ) );
138
	}
139
140
	/**
141
	 * Load the persistent notices from the database
142
	 *
143
	 * @return array The array of notices
144
	 */
145
	private function get_persistent_notices() {
146
		$notices = get_option( 'hmbkp_notices' );
147
		if ( ! empty( $notices ) ) {
148
			return $notices;
149
		}
150
		return array();
151
	}
152
153
	/**
154
	 * Clear all notices including persistent ones
155
	 */
156
	public function clear_all_notices() {
157
		$this->notices = array();
158
		delete_option( 'hmbkp_notices' );
159
	}
160
}
161