Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

modules/Dialog/Dialog.php (2 issues)

Check for mismatching type of a variable.

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\Dialog;
3
4
use Redaxscript\Head;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
8
/**
9
 * shared module to replace alert, confirm and prompt
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Modules
15
 * @author Henry Ruhs
16
 */
17
18
class Dialog extends Module\Module
19
{
20
	/**
21
	 * array of the module
22
	 *
23
	 * @var array
24
	 */
25
26
	protected static $_moduleArray =
27
	[
28
		'name' => 'Dialog',
29
		'alias' => 'Dialog',
30
		'author' => 'Redaxmedia',
31
		'description' => 'Shared module to replace alert, confirm and prompt',
32
		'version' => '5.0.0',
33
		'license' => 'MIT'
34
	];
35
36
	/**
37
	 * array of the option
38
	 *
39
	 * @var array
40
	 */
41
42
	protected $_optionArray =
43
	[
44
		'className' =>
45
		[
46
			'overlay' => 'rs-overlay-dialog',
47
			'component' => 'rs-component-dialog',
48
			'title' => 'rs-title-dialog',
49
			'box' => 'rs-box-dialog',
50
			'text' => 'rs-text-dialog',
51
			'field' => 'rs-js-input rs-field-default rs-field-text',
52
			'button' => 'rs-button-default',
53
			'buttonOk' => 'rs-js-ok rs-button-ok',
54
			'buttonCancel' => 'rs-js-cancel rs-button-cancel'
55
		]
56
	];
57
58
	/**
59
	 * renderStart
60
	 *
61
	 * @since 4.0.0
62
	 */
63
64
	public function renderStart() : void
65
	{
66
		$firstParameter = $this->_registry->get('firstParameter');
67
		$secondParameter = $this->_registry->get('secondParameter');
68
		$thirdParameter = $this->_registry->get('thirdParameter');
69
70
		/* link */
71
72
		$link = Head\Link::getInstance();
73
		$link
74
			->init()
75
			->appendFile('modules/Dialog/dist/styles/dialog.min.css');
76
77
		/* script */
78
79
		$script = Head\Script::getInstance();
80
		$script
81
			->init('foot')
82
			->appendFile(
83
			[
84
				'modules/Dialog/assets/scripts/init.js',
85
				'modules/Dialog/dist/scripts/dialog.min.js'
86
			]);
87
88
		/* route as needed */
89
90
		if ($firstParameter === 'module' && ($secondParameter === 'dialog' || $secondParameter === 'admin-dialog'))
91
		{
92
			$this->_registry->set('renderBreak', true);
93
			$dialog = $secondParameter === 'admin-dialog' ? new Admin\Dialog($this->_registry, $this->_request, $this->_language, $this->_config) : $this;
94
			$message = $this->_request->getStream('message');
95
			$title = $this->_request->getStream('title');
96
97
			/* run as needed */
98
99
			if ($thirdParameter === 'alert')
100
			{
101
				echo $dialog->alert($message, $title);
102
			}
103
			if ($thirdParameter === 'confirm')
104
			{
105
				echo $dialog->confirm($message, $title);
106
			}
107
			if ($thirdParameter === 'prompt')
108
			{
109
				echo $dialog->prompt($message, $title);
110
			}
111
		}
112
	}
113
114
	/**
115
	 * alert
116
	 *
117
	 * @since 4.0.0
118
	 *
119
	 * @param string $message message of the alert
120
	 * @param string $title title of the alert
121
	 *
122
	 * @return string
123
	 */
124
125
	public function alert(string $message = null, string $title = null) : string
126
	{
127
		return $this->_dialog('alert', $message, $title ? : $this->_language->get('_dialog')['alert']);
128
	}
129
130
	/**
131
	 * confirm
132
	 *
133
	 * @since 4.0.0
134
	 *
135
	 * @param string $message message of the confirm
136
	 * @param string $title title of the confirm
137
	 *
138
	 * @return string
139
	 */
140
141
	public function confirm(string $message = null, string $title = null) : string
142
	{
143
		return $this->_dialog('confirm', $message, $title ? : $this->_language->get('_dialog')['confirm']);
144
	}
145
146
	/**
147
	 * prompt
148
	 *
149
	 * @since 4.0.0
150
	 *
151
	 * @param string $message message of the prompt
152
	 * @param string $title title of the prompt
153
	 *
154
	 * @return string
155
	 */
156
157
	public function prompt(string $message = null, string $title = null) : string
158
	{
159
		return $this->_dialog('prompt', $message, $title ? : $this->_language->get('_dialog')['prompt']);
160
	}
161
162
	/**
163
	 * dialog
164
	 *
165
	 * @since 4.0.0
166
	 *
167
	 * @param string $type type of the dialog
168
	 * @param string $message message of the dialog
169
	 * @param string $title title of the dialog
170
	 *
171
	 * @return string
172
	 */
173
174
	protected function _dialog(string $type = null, string $message = null, string $title = null) : string
175
	{
176
		$output = null;
177
178
		/* html elements */
179
180
		$element = new Html\Element();
181
		$overlayElement = $element
182
			->copy()
183
			->init('div',
184
			[
185
				'class' => $this->_optionArray['className']['overlay']
186
			]);
187
		$dialogElement = $element
188
			->copy()
189
			->init('div',
190
			[
191
				'class' => $this->_optionArray['className']['component']
192
			]);
193
		$titleElement = $element
194
			->copy()
195
			->init('h3',
196
			[
197
				'class' => $this->_optionArray['className']['title']
198
			])
199
			->text($title);
200
		$boxElement = $element
201
			->copy()
202
			->init('div',
203
			[
204
				'class' => $this->_optionArray['className']['box']
205
			]);
206
		$textElement = $message ? $element
207
			->copy()
208
			->init('p',
209
			[
210
				'class' => $this->_optionArray['className']['text']
211
			])
212
			->text($message) : null;
213
		$fieldElement = $type === 'prompt' ? $element
214
			->copy()
215
			->init('input',
216
			[
217
				'class' => $this->_optionArray['className']['field']
218
			]) : null;
219
		$buttonElement = $element
220
			->copy()
221
			->init('button',
222
			[
223
				'class' => $this->_optionArray['className']['button']
224
			]);
225
		$buttonOkElement = $buttonElement
226
			->copy()
227
			->addClass($this->_optionArray['className']['buttonOk'])
228
			->text($this->_language->get('ok'));
0 ignored issues
show
It seems like $this->_language->get('ok') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
229
		$buttonCancelElement = $type === 'confirm' || $type === 'prompt' ? $buttonElement
230
			->copy()
231
			->addClass($this->_optionArray['className']['buttonCancel'])
232
			->text($this->_language->get('cancel')) : null;
0 ignored issues
show
It seems like $this->_language->get('cancel') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
233
234
		/* collect output */
235
236
		$output = $overlayElement->html(
237
			$dialogElement->html(
238
				$titleElement .
239
				$boxElement->html(
240
					$textElement .
241
					$fieldElement .
242
					$buttonOkElement .
243
					$buttonCancelElement
244
				)
245
			)
246
		);
247
		return $output;
248
	}
249
}
250