Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/SystemMessagesService.php (1 issue)

implicit conversion of array to boolean.

Best Practice Bug Minor
1
<?php
2
namespace Elgg;
3
4
use Elgg\SystemMessages\RegisterSet;
5
6
/**
7
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
8
 *
9
 * Use the elgg_* versions instead.
10
 *
11
 * @access private
12
 *
13
 * @package    Elgg.Core
14
 * @subpackage UX
15
 * @since      1.11.0
16
 */
17
class SystemMessagesService {
18
19
	const SUCCESS = 'success';
20
	const ERROR = 'error';
21
	const SESSION_KEY = '_elgg_msgs';
22
23
	/**
24
	 * @var \ElggSession
25
	 */
26
	protected $session;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param \ElggSession $session The Elgg session
32
	 */
33 191
	public function __construct(\ElggSession $session) {
34 191
		$this->session = $session;
35 191
	}
36
37
	/**
38
	 * Empty and return the given register or all registers. In each case, the return value is
39
	 * a filtered version of the full registers array.
40
	 *
41
	 * @param string $register_name The register. Empty string for all.
42
	 *
43
	 * @return array The array of registers dumped
44
	 */
45 39
	public function dumpRegister($register_name = '') {
46 39
		$set = $this->loadRegisters();
47 39
		$return = [];
48
49 39
		foreach ($set as $prop => $values) {
50 39
			if ($register_name === $prop || $register_name === '') {
51 39
				if ($values || $register_name === $prop) {
52 20
					$return[$prop] = $values;
53
				}
54
55 39
				$set->{$prop} = [];
56
			}
57
		}
58
59
		// support arbitrary registers for 2.0 BC
60 39
		if ($register_name && !isset($return[$register_name])) {
61
			$return[$register_name] = [];
62
		}
63
64 39
		$this->saveRegisters($set);
65 39
		return $return;
66
	}
67
68
	/**
69
	 * Counts the number of messages, either globally or in a particular register
70
	 *
71
	 * @param string $register_name Optionally, the register
72
	 *
73
	 * @return integer The number of messages
74
	 */
75 13
	public function count($register_name = "") {
76 13
		$set = $this->loadRegisters();
77 13
		$count = 0;
78
79 13
		foreach ($set as $prop => $values) {
80 13
			if ($register_name === $prop || $register_name === '') {
81 13
				$count += count($values);
82
			}
83
		}
84
85 13
		return $count;
86
	}
87
88
	/**
89
	 * Display a system message on next page load.
90
	 *
91
	 * @param string|string[] $message Message or messages to add
92
	 *
93
	 * @return void
94
	 */
95 21
	public function addSuccessMessage($message) {
96 21
		$set = $this->loadRegisters();
97 21
		foreach ((array) $message as $str) {
98 21
			$set->success[] = $str;
99
		}
100 21
		$this->saveRegisters($set);
101 21
	}
102
103
	/**
104
	 * Display an error on next page load.
105
	 *
106
	 * @param string|string[] $error Error or errors to add
107
	 *
108
	 * @return void
109
	 */
110 52
	public function addErrorMessage($error) {
111 52
		$set = $this->loadRegisters();
112 52
		foreach ((array) $error as $str) {
113 52
			$set->error[] = $str;
114
		}
115 52
		$this->saveRegisters($set);
116 52
	}
117
118
	/**
119
	 * Load the registers from the session
120
	 *
121
	 * @return RegisterSet
122
	 */
123 100
	public function loadRegisters() {
124 100
		$registers = $this->session->get(self::SESSION_KEY, []);
125 100
		$set = new RegisterSet();
126 100
		foreach ($registers as $key => $register) {
127 38
			$set->{$key} = $register;
128
		}
129 100
		return $set;
130
	}
131
132
	/**
133
	 * Save the registers to the session
134
	 *
135
	 * The method of displaying these messages differs depending upon plugins and
136
	 * viewtypes.  The core default viewtype retrieves messages in
137
	 * {@link views/default/page/shells/default.php} and displays messages as
138
	 * javascript popups.
139
	 *
140
	 * Messages are stored as strings in the Elgg session as ['msg'][$register] array.
141
	 *
142
	 * @param RegisterSet $set The set of registers
143
	 * @return void
144
	 */
145
	public function saveRegisters(RegisterSet $set) {
146 90
		$filter = function ($el) {
147 69
			return is_string($el) && $el !== "";
148 90
		};
149
150
		$data = [];
151
		foreach ($set as $prop => $values) {
152
			if (!is_array($values)) {
153
				continue;
154
			}
155
			$arr = array_filter($values, $filter);
156
			if ($arr) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arr 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...
157
				$data[$prop] = array_values($arr);
158
			}
159
		}
160
161
		$this->session->set(self::SESSION_KEY, $data);
162
	}
163
}
164