Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

TValidationSummary   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 376
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 376
rs 5.1724
c 0
b 0
f 0
wmc 57

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getScrollToSummary() 0 3 1
A setScrollToSummary() 0 3 1
A getEnableClientScript() 0 3 1
A getClientScriptOptions() 0 21 4
A setDisplayMode() 0 3 1
A getAutoUpdate() 0 3 1
A getHeaderText() 0 3 1
A getValidationGroup() 0 3 1
A renderList() 0 10 3
A __construct() 0 4 1
B renderJsSummary() 0 22 5
A getShowMessageBox() 0 3 1
A getShowAnchor() 0 3 1
A getDisplay() 0 3 1
A setShowSummary() 0 3 1
A createClientScript() 0 3 1
A getShowSummary() 0 3 1
A renderSingleParagraph() 0 8 2
A setShowMessageBox() 0 3 1
A setAutoUpdate() 0 3 1
A renderHeaderOnly() 0 3 1
A setShowAnchor() 0 3 1
B renderContents() 0 20 6
A getDisplayMode() 0 3 1
A getClientSide() 0 5 2
A setDisplay() 0 3 1
A setValidationGroup() 0 3 1
A setEnableClientScript() 0 3 1
A setHeaderText() 0 3 1
B addAttributesToRender() 0 13 5
A getErrorMessages() 0 11 4
A renderBulletList() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like TValidationSummary often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TValidationSummary, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * TValidationSummary class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Web\UI\WebControls
10
 */
11
12
namespace Prado\Web\UI\WebControls;
13
14
use Prado\TPropertyValue;
15
use Prado\Web\Javascripts\TJavaScript;
16
17
/**
18
 * TValidationSummary class
19
 *
20
 * TValidationSummary displays a summary of validation errors inline on a Web page,
21
 * in a message box, or both. By default, a validation summary will collect
22
 * {@link TBaseValidator::getErrorMessage ErrorMessage} of all failed validators
23
 * on the page. If {@link getValidationGroup ValidationGroup} is not
24
 * empty, only those validators who belong to the group will show their error messages
25
 * in the summary.
26
 *
27
 * The summary can be displayed as a list, as a bulleted list, or as a single
28
 * paragraph based on the {@link setDisplayMode DisplayMode} property.
29
 * The messages shown can be prefixed with {@link setHeaderText HeaderText}.
30
 *
31
 * The summary can be displayed on the Web page and in a message box by setting
32
 * the {@link setShowSummary ShowSummary} and {@link setShowMessageBox ShowMessageBox}
33
 * properties, respectively. Note, the latter is only effective when
34
 * {@link setEnableClientScript EnableClientScript} is true.
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @package Prado\Web\UI\WebControls
38
 * @since 3.0
39
 */
40
class TValidationSummary extends \Prado\Web\UI\WebControls\TWebControl
41
{
42
	/**
43
	 * @var TClientSideValidationSummaryOptions validation client side options.
44
	 */
45
	private $_clientSide;
46
47
	/**
48
	 * Constructor.
49
	 * This method sets the foreground color to red.
50
	 */
51
	public function __construct()
52
	{
53
		parent::__construct();
54
		$this->setForeColor('red');
55
	}
56
57
	/**
58
	 * @return TValidationSummaryDisplayStyle the style of displaying the error messages. Defaults to TValidationSummaryDisplayStyle::Fixed.
59
	 */
60
	public function getDisplay()
61
	{
62
		return $this->getViewState('Display', TValidationSummaryDisplayStyle::Fixed);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getViewSta...aryDisplayStyle::Fixed) also could return the type string which is incompatible with the documented return type Prado\Web\UI\WebControls...tionSummaryDisplayStyle.
Loading history...
63
	}
64
65
	/**
66
	 * @param TValidationSummaryDisplayStyle the style of displaying the error messages
67
	 */
68
	public function setDisplay($value)
69
	{
70
		$this->setViewState('Display', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TValidationSummaryDisplayStyle'), TValidationSummaryDisplayStyle::Fixed);
71
	}
72
73
	/**
74
	 * @return string the header text displayed at the top of the summary
75
	 */
76
	public function getHeaderText()
77
	{
78
		return $this->getViewState('HeaderText', '');
79
	}
80
81
	/**
82
	 * Sets the header text to be displayed at the top of the summary
83
	 * @param string the header text
84
	 */
85
	public function setHeaderText($value)
86
	{
87
		$this->setViewState('HeaderText', $value, '');
88
	}
89
90
	/**
91
	 * @return TValidationSummaryDisplayMode the mode of displaying error messages. Defaults to TValidationSummaryDisplayMode::BulletList.
92
	 */
93
	public function getDisplayMode()
94
	{
95
		return $this->getViewState('DisplayMode', TValidationSummaryDisplayMode::BulletList);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getViewSta...isplayMode::BulletList) also could return the type string which is incompatible with the documented return type Prado\Web\UI\WebControls...ationSummaryDisplayMode.
Loading history...
96
	}
97
98
	/**
99
	 * @param TValidationSummaryDisplayMode the mode of displaying error messages
100
	 */
101
	public function setDisplayMode($value)
102
	{
103
		$this->setViewState('DisplayMode', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TValidationSummaryDisplayMode'), TValidationSummaryDisplayMode::BulletList);
104
	}
105
106
	/**
107
	 * @return boolean whether the TValidationSummary component updates itself using client-side script. Defaults to true.
108
	 */
109
	public function getEnableClientScript()
110
	{
111
		return $this->getViewState('EnableClientScript', true);
112
	}
113
114
	/**
115
	 * @param boolean whether the TValidationSummary component updates itself using client-side script.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\whether was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
	 */
117
	public function setEnableClientScript($value)
118
	{
119
		$this->setViewState('EnableClientScript', TPropertyValue::ensureBoolean($value), true);
120
	}
121
122
	/**
123
	 * @return boolean whether the validation summary is displayed in a message box. Defaults to false.
124
	 */
125
	public function getShowMessageBox()
126
	{
127
		return $this->getViewState('ShowMessageBox', false);
128
	}
129
130
	/**
131
	 * @param boolean whether the validation summary is displayed in a message box.
132
	 */
133
	public function setShowMessageBox($value)
134
	{
135
		$this->setViewState('ShowMessageBox', TPropertyValue::ensureBoolean($value), false);
136
	}
137
138
	/**
139
	 * @return boolean whether the validation summary is displayed inline. Defaults to true.
140
	 */
141
	public function getShowSummary()
142
	{
143
		return $this->getViewState('ShowSummary', true);
144
	}
145
146
	/**
147
	 * @param boolean whether the validation summary is displayed inline.
148
	 */
149
	public function setShowSummary($value)
150
	{
151
		$this->setViewState('ShowSummary', TPropertyValue::ensureBoolean($value), true);
152
	}
153
154
	/**
155
	 * @return boolean whether scroll summary into viewport or not. Defaults to true.
156
	 */
157
	public function getScrollToSummary()
158
	{
159
		return $this->getViewState('ScrollToSummary', true);
160
	}
161
162
	/**
163
	 * @param boolean whether scroll summary into viewport or not.
164
	 */
165
	public function setScrollToSummary($value)
166
	{
167
		$this->setViewState('ScrollToSummary', TPropertyValue::ensureBoolean($value), true);
168
	}
169
170
	/**
171
	 * @return boolean whether the validation summary should be anchored. Defaults to false.
172
	 */
173
	public function getShowAnchor()
174
	{
175
		return $this->getViewState('ShowAnchor', false);
176
	}
177
178
	/**
179
	 * @param boolean whether the validation summary should be anchored.
180
	 */
181
	public function setShowAnchor($value)
182
	{
183
		$this->setViewState('ShowAnchor', TPropertyValue::ensureBoolean($value), false);
184
	}
185
186
	/**
187
	 * Gets the auto-update for this summary.
188
	 * @return boolean automatic client-side summary updates. Defaults to true.
189
	 */
190
	public function getAutoUpdate()
191
	{
192
		return $this->getViewState('AutoUpdate', true);
193
	}
194
195
	/**
196
	 * Sets the summary to auto-update on the client-side
197
	 * @param boolean true for automatic summary updates.
198
	 */
199
	public function setAutoUpdate($value)
200
	{
201
		$this->setViewState('AutoUpdate', TPropertyValue::ensureBoolean($value), true);
202
	}
203
204
	/**
205
	 * @return string the group which this validator belongs to
206
	 */
207
	public function getValidationGroup()
208
	{
209
		return $this->getViewState('ValidationGroup', '');
210
	}
211
212
	/**
213
	 * @param string the group which this validator belongs to
214
	 */
215
	public function setValidationGroup($value)
216
	{
217
		$this->setViewState('ValidationGroup', $value, '');
218
	}
219
220
	protected function addAttributesToRender($writer)
221
	{
222
		$display = $this->getDisplay();
223
		$visible = $this->getEnabled(true) && count($this->getErrorMessages()) > 0;
224
		if(!$visible)
225
		{
226
			if($display === TValidationSummaryDisplayStyle::None || $display === TValidationSummaryDisplayStyle::Dynamic)
0 ignored issues
show
introduced by
The condition $display === Prado\Web\U...ryDisplayStyle::Dynamic can never be true.
Loading history...
227
				$writer->addStyleAttribute('display', 'none');
228
			else
229
				$writer->addStyleAttribute('visibility', 'hidden');
230
		}
231
		$writer->addAttribute('id', $this->getClientID());
232
		parent::addAttributesToRender($writer);
233
	}
234
235
	/**
236
	 * Render the javascript for validation summary.
237
	 * @param array list of options for validation summary.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
238
	 */
239
	protected function renderJsSummary()
240
	{
241
		if(!$this->getEnabled(true) || !$this->getEnableClientScript())
242
			return;
243
		$cs = $this->getPage()->getClientScript();
244
		$cs->registerPradoScript('validator');
245
246
		//need to register the validation manager is validation summary is alone.
247
		$formID = $this->getPage()->getForm()->getClientID();
248
		$scriptKey = "TBaseValidator:$formID";
249
		if($this->getEnableClientScript() && !$cs->isEndScriptRegistered($scriptKey))
250
		{
251
			$manager['FormID'] = $formID;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$manager was never initialized. Although not strictly required by PHP, it is generally a good practice to add $manager = array(); before regardless.
Loading history...
252
			$options = TJavaScript::encode($manager);
253
			$cs->registerPradoScript('validator');
254
			$cs->registerEndScript($scriptKey, "new Prado.ValidationManager({$options});");
255
		}
256
257
258
		$options = TJavaScript::encode($this->getClientScriptOptions());
259
		$script = "new Prado.WebUI.TValidationSummary({$options});";
260
		$cs->registerEndScript($this->getClientID(), $script);
261
	}
262
263
	/**
264
	 * Get a list of options for the client-side javascript validation summary.
265
	 * @return array list of options for the summary
266
	 */
267
	protected function getClientScriptOptions()
268
	{
269
		$options['ID'] = $this->getClientID();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
270
		$options['FormID'] = $this->getPage()->getForm()->getClientID();
271
		if($this->getShowMessageBox())
272
			$options['ShowMessageBox'] = true;
273
		if(!$this->getShowSummary())
274
			$options['ShowSummary'] = false;
275
276
		$options['ScrollToSummary'] = $this->getScrollToSummary();
277
		$options['HeaderText'] = $this->getHeaderText();
278
		$options['DisplayMode'] = $this->getDisplayMode();
279
280
		$options['Refresh'] = $this->getAutoUpdate();
281
		$options['ValidationGroup'] = $this->getValidationGroup();
282
		$options['Display'] = $this->getDisplay();
283
284
		if($this->_clientSide !== null)
285
			$options = array_merge($options, $this->_clientSide->getOptions()->toArray());
286
287
		return $options;
288
	}
289
290
	/**
291
	 * @return TClientSideValidationSummaryOptions client-side validation summary
292
	 * event options.
293
	 */
294
	public function getClientSide()
295
	{
296
		if($this->_clientSide === null)
297
			$this->_clientSide = $this->createClientScript();
298
		return $this->_clientSide;
299
	}
300
301
	/**
302
	 * @return TClientSideValidationSummaryOptions javascript validation summary
303
	 * event options.
304
	 */
305
	protected function createClientScript()
306
	{
307
		return new TClientSideValidationSummaryOptions;
308
	}
309
	/**
310
	 * Get the list of validation error messages.
311
	 * @return array list of validator error messages.
312
	 */
313
	protected function getErrorMessages()
314
	{
315
		$validators = $this->getPage()->getValidators($this->getValidationGroup());
316
		$messages = [];
317
		foreach($validators as $validator)
318
		{
319
			if(!$validator->getIsValid() && ($msg = $validator->getErrorMessage()) !== '')
320
				//$messages[] = $validator->getAnchoredMessage($msg);
321
				$messages[] = $msg;
322
		}
323
		return $messages;
324
	}
325
326
	/**
327
	 * Overrides parent implementation by rendering TValidationSummary-specific presentation.
328
	 * @return string the rendering result
329
	 */
330
	public function renderContents($writer)
331
	{
332
		$this->renderJsSummary();
333
		if($this->getShowSummary())
334
		{
335
//		    $this->setStyle('display:block');
336
			switch($this->getDisplayMode())
337
			{
338
				case TValidationSummaryDisplayMode::SimpleList:
339
					$this->renderList($writer);
340
					break;
341
				case TValidationSummaryDisplayMode::SingleParagraph:
342
					$this->renderSingleParagraph($writer);
343
					break;
344
				case TValidationSummaryDisplayMode::BulletList:
345
					$this->renderBulletList($writer);
346
					break;
347
				case TValidationSummaryDisplayMode::HeaderOnly:
348
					$this->renderHeaderOnly($writer);
349
					break;
350
			}
351
		}
352
	}
353
354
	/**
355
	 * Render the validation summary as a simple list.
356
	 * @param array list of messages
357
	 * @param string the header text
358
	 * @return string summary list
359
	 */
360
	protected function renderList($writer)
361
	{
362
		$header = $this->getHeaderText();
363
		$messages = $this->getErrorMessages();
364
		$content = '';
365
		if(strlen($header))
366
			$content .= $header . "<br/>\n";
367
		foreach($messages as $message)
368
			$content .= "$message<br/>\n";
369
		$writer->write($content);
370
	}
371
372
	/**
373
	 * Render the validation summary as a paragraph.
374
	 * @param array list of messages
375
	 * @param string the header text
376
	 * @return string summary paragraph
377
	 */
378
	protected function renderSingleParagraph($writer)
379
	{
380
		$header = $this->getHeaderText();
381
		$messages = $this->getErrorMessages();
382
		$content = $header;
383
		foreach($messages as $message)
384
			$content .= ' ' . $message;
385
		$writer->write($content);
386
	}
387
388
	/**
389
	 * Render the validation summary as a bullet list.
390
	 * @param array list of messages
391
	 * @param string the header text
392
	 * @return string summary bullet list
393
	 */
394
	protected function renderBulletList($writer)
395
	{
396
		$header = $this->getHeaderText();
397
		$messages = $this->getErrorMessages();
398
		$content = $header;
399
		if(count($messages) > 0)
400
		{
401
			$content .= "<ul>\n";
402
			foreach($messages as $message)
403
				$content .= '<li>' . $message . "</li>\n";
404
			$content .= "</ul>\n";
405
		}
406
		$writer->write($content);
407
	}
408
409
	/**
410
	 * Render the validation summary header text only.
411
	 * @param THtmlWriter the writer used for the rendering purpose
412
	 */
413
	protected function renderHeaderOnly($writer)
414
	{
415
		$writer->write($this->getHeaderText());
416
	}
417
}
418