Completed
Branch master (51e6bf)
by
unknown
30:16
created

HTMLFormElement::initializeHTMLFormElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 14
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Allows custom data specific to HTMLFormField to be set for OOjs UI forms. A matching JS widget
5
 * (defined in htmlform.Element.js) picks up the extra config when constructed using OO.ui.infuse().
6
 *
7
 * Currently only supports passing 'hide-if' data.
8
 */
9
trait HTMLFormElement {
10
11
	protected $hideIf = null;
12
13
	public function initializeHTMLFormElement( array $config = [] ) {
14
		// Properties
15
		$this->hideIf = isset( $config['hideIf'] ) ? $config['hideIf'] : null;
16
17
		// Initialization
18
		if ( $this->hideIf ) {
19
			$this->addClasses( [ 'mw-htmlform-hide-if' ] );
0 ignored issues
show
Bug introduced by
It seems like addClasses() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
		}
21
		$this->registerConfigCallback( function( &$config ) {
0 ignored issues
show
Bug introduced by
It seems like registerConfigCallback() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
			if ( $this->hideIf !== null ) {
23
				$config['hideIf'] = $this->hideIf;
24
			}
25
		} );
26
	}
27
}
28
29
class HTMLFormFieldLayout extends OOUI\FieldLayout {
30
	use HTMLFormElement;
31
32
	public function __construct( $fieldWidget, array $config = [] ) {
33
		// Parent constructor
34
		parent::__construct( $fieldWidget, $config );
35
		// Traits
36
		$this->initializeHTMLFormElement( $config );
37
	}
38
39
	protected function getJavaScriptClassName() {
40
		return 'mw.htmlform.FieldLayout';
41
	}
42
}
43
44
class HTMLFormActionFieldLayout extends OOUI\ActionFieldLayout {
45
	use HTMLFormElement;
46
47
	public function __construct( $fieldWidget, $buttonWidget = false, array $config = [] ) {
48
		// Parent constructor
49
		parent::__construct( $fieldWidget, $buttonWidget, $config );
50
		// Traits
51
		$this->initializeHTMLFormElement( $config );
52
	}
53
54
	protected function getJavaScriptClassName() {
55
		return 'mw.htmlform.ActionFieldLayout';
56
	}
57
}
58