Completed
Push — master ( c6c18d...79b035 )
by Jean-Christophe
03:25
created

HtmlFormField   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 1
cbo 2
dl 7
loc 75
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setLabel() 0 9 2
A setField() 0 3 1
A getLabel() 0 4 2
A getField() 0 3 1
A swapLabel() 0 5 1
A setWidth() 7 7 2
A setError() 0 3 1
A jsState() 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
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\semantic\html\base\constants\State;
8
9
class HtmlFormField extends HtmlSemDoubleElement {
10
	public function __construct($identifier, $field,$label=NULL) {
11
		parent::__construct($identifier, "div","field");
12
		$this->content=array();
13
		$this->_states=[State::ERROR,State::DISABLED];
14
		if(isset($label))
15
			$this->setLabel($label);
16
		$this->setField($field);
17
	}
18
19
	public function setLabel($label){
20
		$labelO=$label;
21
		if(\is_string($label)){
22
			$labelO=new HtmlSemDoubleElement("","label");
23
			$labelO->setContent($label);
24
			$labelO->setProperty("for", \str_replace("field-", "",$this->identifier));
25
		}
26
		$this->content["label"]=$labelO;
27
	}
28
29
	public function setField($field){
30
		$this->content["field"]=$field;
31
	}
32
33
	/**
34
	 * Returns the label or null
35
	 * @return mixed
36
	 */
37
	public function getLabel(){
38
		if(\array_key_exists("label", $this->content))
39
			return $this->content["label"];
40
	}
41
42
	/**
43
	 * Return the field
44
	 * @return mixed
45
	 */
46
	public function getField(){
47
		return $this->content["field"];
48
	}
49
50
	/**
51
	 * puts the label before or behind
52
	 */
53
	public function swapLabel(){
54
		$label=$this->getLabel();
55
		unset($this->content["label"]);
56
		$this->content["label"]=$label;
57
	}
58
59
	/**
60
	 * Defines the field width
61
	 * @param int $width
62
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
63
	 */
64 View Code Duplication
	public function setWidth($width){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
		if(\is_int($width)){
66
			$width=Wide::getConstants()["W".$width];
67
		}
68
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
69
		return $this->addToPropertyCtrl("class", "wide",array("wide"));
70
	}
71
72
	/**
73
	 * Field displays an error state
74
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
75
	 */
76
	public function setError(){
77
		return $this->addToProperty("class", "error");
78
	}
79
80
	public function jsState($state){
81
		return $this->jsDoJquery("addClass",$state);
82
	}
83
}