Completed
Branch master (0c9f05)
by
unknown
29:21
created

HTMLSelectField   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 41.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 28
loc 67
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 5 15 3
A getInputHTML() 0 21 4
A getInputOOUI() 23 23 3
A shouldInfuseOOUI() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * A select dropdown field.  Basically a wrapper for Xmlselect class
5
 */
6
class HTMLSelectField extends HTMLFormField {
7
	function validate( $value, $alldata ) {
8
		$p = parent::validate( $value, $alldata );
9
10
		if ( $p !== true ) {
11
			return $p;
12
		}
13
14
		$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
15
16 View Code Duplication
		if ( in_array( strval( $value ), $validOptions, true ) ) {
17
			return true;
18
		} else {
19
			return $this->msg( 'htmlform-select-badoption' )->parse();
20
		}
21
	}
22
23
	function getInputHTML( $value ) {
24
		$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
25
26
		if ( !empty( $this->mParams['disabled'] ) ) {
27
			$select->setAttribute( 'disabled', 'disabled' );
28
		}
29
30
		$allowedParams = [ 'tabindex', 'size' ];
31
		$customParams = $this->getAttributes( $allowedParams );
32
		foreach ( $customParams as $name => $value ) {
33
			$select->setAttribute( $name, $value );
34
		}
35
36
		if ( $this->mClass !== '' ) {
37
			$select->setAttribute( 'class', $this->mClass );
38
		}
39
40
		$select->addOptions( $this->getOptions() );
41
42
		return $select->getHTML();
43
	}
44
45 View Code Duplication
	function getInputOOUI( $value ) {
46
		$disabled = false;
47
		$allowedParams = [ 'tabindex' ];
48
		$attribs = OOUI\Element::configFromHtmlAttributes(
49
			$this->getAttributes( $allowedParams )
50
		);
51
52
		if ( $this->mClass !== '' ) {
53
			$attribs['classes'] = [ $this->mClass ];
54
		}
55
56
		if ( !empty( $this->mParams['disabled'] ) ) {
57
			$disabled = true;
58
		}
59
60
		return new OOUI\DropdownInputWidget( [
61
			'name' => $this->mName,
62
			'id' => $this->mID,
63
			'options' => $this->getOptionsOOUI(),
64
			'value' => strval( $value ),
65
			'disabled' => $disabled,
66
		] + $attribs );
67
	}
68
69
	protected function shouldInfuseOOUI() {
70
		return true;
71
	}
72
}
73