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:30
created

Notices::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
6
/**
7
 * Class for managing notices
8
 *
9
 * Notices are messages along with an associated context, by default
10
 * they are stored in the db and thus persist between page loads.
11
 */
12
class Notices {
13
14
	/**
15
	 * The array of notice messages
16
	 *
17
	 * This is a multidimensional array of
18
	 * `array( context => array( 'message' ) );``
19
	 *
20
	 * @var array
21
	 */
22
	private $notices = array();
23
24
	/**
25
	 * Contains the instantiated Notices instance
26
	 *
27
	 * @var Path $this->get_instance
28
	 */
29
	private static $instance;
30
31
	/**
32
	 * Protected constructor to prevent creating a new instance of the
33
	 * *Singleton* via the `new` operator from outside of this class.
34
	 */
35
	protected function __construct() {}
36
37
	/**
38
	 * Private clone method to prevent cloning of the instance of the
39
	 * *Singleton* instance.
40
	 */
41
	private function __clone() {}
42
43
	/**
44
	 * Private unserialize method to prevent unserializing of the *Singleton*
45
	 * instance.
46
	 */
47
	private function __wakeup() {}
48
49
	/**
50
	 * Returns the *Singleton* instance of this class.
51
	 *
52
	 * @staticvar Notices $instance The *Singleton* instances of this class.
53
	 *
54
	 * @return Notices The *Singleton* instance.
55
	 */
56
	public static function get_instance() {
57
58
		if ( ! ( self::$instance instanceof Notices ) ) {
59
			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...
60
		}
61
62
		return self::$instance;
63
64
	}
65
66
	/**
67
	 * Set an array of notice messages for a specific context
68
	 *
69
	 * @param string  $context    The context of the notice message
70
	 * @param array   $messages   The array of messages
71
	 * @param boolean $persistant Whether the notice should persist via the database. Defaults to true.
72
	 */
73
	public function set_notices( $context, array $messages, $persistant = true ) {
74
75
		// Clear any empty messages and avoid duplicates
76
		$messages = array_unique( array_filter( $messages ) );
77
78
		if ( empty( $context ) || empty( $messages ) ) {
79
			return false;
80
		}
81
82
		$this->notices[ $context ] = array_merge( $this->get_notices( $context ), $messages );
83
84
		if ( $persistant ) {
85
86
			$new_notices = $notices = $this->get_persistant_notices();
87
88
			// Make sure we merge in any existing notices
89
			if ( ! empty( $notices[ $context ] ) ) {
90
				$new_notices[ $context ] = array_merge( $notices[ $context ], $messages );
91
			} else {
92
				$new_notices[ $context ] = $messages;
93
			}
94
95
			// Only update the database if the notice array has changed
96
			if ( $new_notices !== $notices ) {
97
				update_option( 'hmbkp_notices', $new_notices );
98
			}
99
		}
100
101
		return true;
102
103
	}
104
105
	/**
106
	 * Fetch an array of notices messages
107
	 *
108
	 * If you specify a context then you'll just get messages for that context otherwise
109
	 * you get multidimensional array of all contexts and their messages.
110
	 *
111
	 * @param string $context The context that you'd like the messages for
112
	 *
113
	 * @return array The array of notice messages
114
	 */
115
	public function get_notices( $context = '' ) {
116
117
		$notices = $this->get_all_notices();
118
119
		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...
120
121
			if ( $context ) {
122
				return isset( $notices[ $context ] ) ? $notices[ $context ] : array();
123
			}
124
125
			return $notices;
126
		}
127
128
		return array();
129
130
	}
131
132
	/**
133
	 * Get both standard and persistant notices
134
	 *
135
	 * @return array The array of contexts and notices
136
	 */
137
	private function get_all_notices() {
138
		return array_map( 'array_unique', array_merge_recursive( $this->notices, $this->get_persistant_notices() ) );
139
	}
140
141
	/**
142
	 * Load the persistant notices from the database
143
	 *
144
	 * @return array The array of notices
145
	 */
146
	private function get_persistant_notices() {
147
		$notices = get_option( 'hmbkp_notices' );
148
		if ( ! empty( $notices ) ) {
149
			return $notices;
150
		}
151
		return array();
152
	}
153
154
	/**
155
	 * Clear all notices including persistant ones
156
	 */
157
	public function clear_all_notices() {
158
		$this->notices = array();
159
		delete_option( 'hmbkp_notices' );
160
	}
161
}
162