Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

Dialog::_dialog()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 7.9668
c 0
b 0
f 0
cc 6
nc 32
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
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...
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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;
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...
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'));
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...
220
		$buttonCancelElement = $type === 'confirm' || $type === 'prompt' ? $buttonElement
221
			->copy()
222
			->addClass($this->_optionArray['className']['buttonCancel'])
223
			->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...
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