Completed
Push — master ( c7a943...2e4ffb )
by Jean-Christophe
02:55
created

CheckboxTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 12.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 2
dl 8
loc 63
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
addToPropertyCtrl() 0 1 ?
A setType() 0 3 1
A attachEvent() 0 3 1
A attachEvents() 0 3 1
A getField() 0 3 1
A getHtmlCk() 0 3 1
A setName() 0 4 1
A getDataField() 0 6 2
A setChecked() 8 8 2

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\traits;
4
5
use Ajax\semantic\html\base\constants\CheckboxType;
6
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
7
use Ajax\semantic\html\collections\form\HtmlFormField;
8
9
/**
10
 * @author jc
11
 * @property mixed $content
12
 */
13
trait CheckboxTrait {
14
15
	abstract public function addToPropertyCtrl($name, $value, $typeCtrl);
16
17
	public function setType($checkboxType) {
18
		return $this->getHtmlCk()->addToPropertyCtrl("class", $checkboxType, CheckboxType::getConstants());
19
	}
20
21
22
	/**
23
	 * Attach $this to $selector and fire $action
24
	 * @param string $selector jquery selector of the associated element
25
	 * @param string $action action to execute : check, uncheck or NULL for toggle
26
	 * @return HtmlFormField
27
	 */
28
	public function attachEvent($selector, $action=NULL) {
29
		return $this->getHtmlCk()->attachEvent($selector, $action);
30
	}
31
32
	/**
33
	 * Attach $this to an array of $action=>$selector
34
	 * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"]
35
	 * @return HtmlFormField
36
	 */
37
	public function attachEvents($events=array()) {
38
		return $this->getHtmlCk()->attachEvents($events);
39
	}
40
41
	public function getField(){
42
		return $this->content["field"];
43
	}
44
45
	public function getHtmlCk(){
46
		return $this->content["field"];
47
	}
48
49
	public function setName($name){
50
		$this->getDataField()->setProperty("name", $name);
51
		return $this;
52
	}
53
54
	public function getDataField(){
55
		$field= $this->getField();
56
		if($field instanceof AbstractCheckbox)
57
			$field=$field->getField();
58
		return $field;
59
	}
60
61
	/**
62
	 * Check the checkbox
63
	 * @param boolean $value
64
	 * @return HtmlFormField
65
	 */
66 View Code Duplication
	public function setChecked($value=true){
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...
67
		if($value===true){
68
			$this->getDataField()->setProperty("checked", "checked");
69
		}else{
70
			$this->getDataField()->removeProperty("checked");
71
		}
72
		return $this;
73
	}
74
75
}
76