Completed
Push — master ( 751bb4...735cac )
by Jean-Christophe
04:44
created

HtmlSearch   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 4.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 4
dl 3
loc 71
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createField() 0 10 3
A createResult() 0 4 1
A addResult() 0 4 1
A addResults() 0 4 1
A setUrl() 0 4 1
A setType() 0 4 1
A getType() 0 3 1
A resultsToJson() 0 4 1
A run() 3 13 3

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\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\elements\HtmlInput;
7
use Ajax\JsUtils;
8
use Ajax\semantic\html\base\constants\Direction;
9
10
class HtmlSearch extends HtmlSemDoubleElement {
11
	private $_elements=array ();
12
	private $_params=array ();
13
	private $_searchFields=array ("title" );
14
15
	public function __construct($identifier, $placeholder=NULL, $icon=NULL) {
16
		parent::__construct("search-" . $identifier, "div", "ui search", array ());
17
		$this->createField($placeholder, $icon);
18
		$this->createResult();
19
		$this->_params["type"]="standard";
20
	}
21
22
	private function createField($placeholder=NULL, $icon=NULL) {
23
		$field=new HtmlInput($this->identifier);
24
		if (isset($placeholder) === true)
25
			$field->setPlaceholder($placeholder);
26
		if (isset($icon) === true)
27
			$field->addIcon($icon, Direction::RIGHT);
28
		$field->getField()->setClass("prompt");
29
		$this->content["field"]=$field;
30
		return $field;
31
	}
32
33
	private function createResult() {
34
		$this->content["result"]=new HtmlSemDoubleElement("results-" . $this->identifier, "div", "results");
35
		return $this->content["result"];
36
	}
37
38
	public function addResult($object) {
39
		$this->_elements[]=$object;
40
		return $this;
41
	}
42
43
	public function addResults($objects) {
44
		$this->_elements=\array_merge($this->_elements, $objects);
45
		return $this;
46
	}
47
48
	public function setUrl($url) {
49
		$this->_params["apiSettings"]="%{url: %quote%" . $url . "%quote%}%";
50
		return $this;
51
	}
52
53
	public function setType($type) {
54
		$this->_params["type"]=$type;
55
		return $this;
56
	}
57
58
	public function getType() {
59
		return $this->_params["type"];
60
	}
61
62
	private function resultsToJson() {
63
		$result=\json_encode($this->_elements);
64
		return $result;
65
	}
66
67
	public function run(JsUtils $js) {
68
		$searchFields=\json_encode($this->_searchFields);
69
		$searchFields=str_ireplace("\"", "%quote%", $searchFields);
70
		$this->_params["searchFields"]="%" . $searchFields . "%";
71
		if ($this->getType() === "standard")
72
			$this->_params["source"]="%content%";
73
		$this->addEvent("beforeExecute", "var content=" . $this->resultsToJson() . ";");
74 View Code Duplication
		if (isset($this->_bsComponent) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
			$this->_bsComponent=$js->semantic()->search("#" . $this->identifier, $this->_params);
76
		}
77
		$this->addEventsOnRun($js);
78
		return $this->_bsComponent;
79
	}
80
}