Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

modules/Dialog/Dialog.php (3 issues)

call_checks.maybe_mismatching_type_passed

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
 * javascript powered dialog
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' => 'JavaScript powered dialog',
32
		'version' => '4.0.0'
33
	];
34
35
	/**
36
	 * array of the option
37
	 *
38
	 * @var array
39
	 */
40
41
	protected $_optionArray =
42
	[
43
		'className' =>
44
		[
45
			'overlay' => 'rs-overlay-dialog',
46
			'component' => 'rs-component-dialog',
47
			'title' => 'rs-title-dialog',
48
			'box' => 'rs-box-dialog',
49
			'field' => 'rs-field-default rs-field-text',
50
			'button' => 'rs-button-default',
51
			'buttonOk' => 'js-ok',
52
			'buttonCancel' => 'js-cancel'
53
		]
54
	];
55
56
	/**
57
	 * renderStart
58
	 *
59
	 * @since 4.0.0
60
	 */
61
62
	public function renderStart() : void
63
	{
64
		$firstParameter = $this->_registry->get('firstParameter');
65
		$secondParameter = $this->_registry->get('secondParameter');
66
		$thirdParameter = $this->_registry->get('thirdParameter');
67
68
		/* link */
69
70
		$link = Head\Link::getInstance();
71
		$link
72
			->init()
73
			->appendFile('modules/Dialog/dist/styles/dialog.min.css');
74
75
		/* script */
76
77
		$script = Head\Script::getInstance();
78
		$script
79
			->init('foot')
80
			->appendFile(
81
			[
82
				'modules/Dialog/assets/scripts/init.js',
83
				'modules/Dialog/dist/scripts/dialog.min.js'
84
			]);
85
86
		/* router */
87
88
		if ($firstParameter === 'module' && ($secondParameter === 'dialog' || $secondParameter === 'admin-dialog'))
89
		{
90
			$this->_registry->set('renderBreak', true);
91
			$dialog = $secondParameter === 'admin-dialog' ? new Admin\Dialog($this->_registry, $this->_request, $this->_language, $this->_config) : $this;
92
			if ($thirdParameter === 'alert')
93
			{
94
				echo $dialog->alert();
95
			}
96
			if ($thirdParameter === 'confirm')
97
			{
98
				echo $dialog->confirm($this->_language->get('continue_question', '_dialog') . $this->_language->get('question_mark'));
99
			}
100
			if ($thirdParameter === 'prompt')
101
			{
102
				echo $dialog->confirm();
103
			}
104
		}
105
	}
106
107
	/**
108
	 * alert
109
	 *
110
	 * @since 4.0.0
111
	 *
112
	 * @param string $message message of the alert
113
	 * @param string $title title of the alert
114
	 *
115
	 * @return string
116
	 */
117
118
	public function alert(string $message = null, string $title = null) : string
119
	{
120
		return $this->_dialog('alert', $message, $title ? : $this->_language->get('alert', '_dialog'));
0 ignored issues
show
It seems like $title ?: $this->_langua...get('alert', '_dialog') can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::_dialog() 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...
121
	}
122
123
	/**
124
	 * confirm
125
	 *
126
	 * @since 4.0.0
127
	 *
128
	 * @param string $message message of the confirm
129
	 * @param string $title title of the confirm
130
	 *
131
	 * @return string
132
	 */
133
134
	public function confirm(string $message = null, string $title = null) : string
135
	{
136
		return $this->_dialog('confirm', $message, $title ? : $this->_language->get('confirm', '_dialog'));
0 ignored issues
show
It seems like $title ?: $this->_langua...t('confirm', '_dialog') can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::_dialog() 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...
137
	}
138
139
	/**
140
	 * prompt
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param string $message message of the prompt
145
	 * @param string $title title of the prompt
146
	 *
147
	 * @return string
148
	 */
149
150
	public function prompt(string $message = null, string $title = null) : string
151
	{
152
		return $this->_dialog('prompt', $message, $title ? : $this->_language->get('prompt', '_dialog'));
0 ignored issues
show
It seems like $title ?: $this->_langua...et('prompt', '_dialog') can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::_dialog() 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...
153
	}
154
155
	/**
156
	 * dialog
157
	 *
158
	 * @since 4.0.0
159
	 *
160
	 * @param string $type type of the dialog
161
	 * @param string $message message of the dialog
162
	 * @param string $title title of the dialog
163
	 *
164
	 * @return string
165
	 */
166
167
	protected function _dialog(string $type = null, string $message = null, string $title = null) : string
168
	{
169
		$output = null;
170
171
		/* html elements */
172
173
		$element = new Html\Element();
174
		$overlayElement = $element
175
			->copy()
176
			->init('div',
177
			[
178
				'class' => $this->_optionArray['className']['overlay']
179
			]);
180
		$dialogElement = $element
181
			->copy()
182
			->init('div',
183
			[
184
				'class' => $this->_optionArray['className']['component']
185
			]);
186
		$titleElement = $element
187
			->copy()
188
			->init('h3',
189
			[
190
				'class' => $this->_optionArray['className']['title']
191
			])
192
			->text($title);
193
		$boxElement = $element
194
			->copy()
195
			->init('div',
196
			[
197
				'class' => $this->_optionArray['className']['box']
198
			]);
199
		$textElement = $type === 'alert' || $type === 'confirm' ? $element
200
			->copy()
201
			->init('p')
202
			->text($message) : null;
203
		$fieldElement = $type === 'prompt' ? $element
204
			->copy()
205
			->init('input',
206
			[
207
				'class' => $this->_optionArray['className']['field'],
208
				'placeholder' => $message
209
			]) : null;
210
		$buttonElement = $element
211
			->copy()
212
			->init('button',
213
			[
214
				'class' => $this->_optionArray['className']['button']
215
			]);
216
		$buttonOkElement = $buttonElement
217
			->copy()
218
			->addClass($this->_optionArray['className']['buttonOk'])
219
			->text($this->_language->get('ok'));
220
		$buttonCancelElement = $type === 'confirm' || $type === 'prompt' ? $buttonElement
221
			->copy()
222
			->addClass($this->_optionArray['className']['buttonCancel'])
223
			->text($this->_language->get('cancel')) : null;
224
225
		/* collect output */
226
227
		$output = $overlayElement->html(
228
			$dialogElement->html(
229
				$titleElement .
230
				$boxElement->html(
231
					$textElement .
232
					$fieldElement .
233
					$buttonOkElement .
234
					$buttonCancelElement
235
				)
236
			)
237
		);
238
		return $output;
239
	}
240
}
241