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

Dialog::prompt()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
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);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->_request->getStream('message') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::alert() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $title defined by $this->_request->getStream('title') on line 95 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::alert() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
			}
103
			if ($thirdParameter === 'confirm')
104
			{
105
				echo $dialog->confirm($message, $title);
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->_request->getStream('message') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::confirm() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $title defined by $this->_request->getStream('title') on line 95 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::confirm() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106
			}
107
			if ($thirdParameter === 'prompt')
108
			{
109
				echo $dialog->prompt($message, $title);
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->_request->getStream('message') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::prompt() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $title defined by $this->_request->getStream('title') on line 95 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::prompt() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
Bug introduced by
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
Bug introduced by
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