Completed
Push — master ( 409900...f20dfb )
by Jean-Christophe
03:24
created

BaseComponent   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 74
loc 74
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 3 1
A getParamsAsJSON() 8 8 2
A setParam() 4 4 1
A getParam() 6 6 2
A getParams() 3 3 1
A compile() 6 6 2
A setParamCtrl() 11 11 4
A setParams() 13 13 3
getScript() 1 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\common\components;
4
5
use Ajax\JsUtils;
6
7
/**
8
 * Base component for JQuery UI visuals components
9
 * @author jc
10
 * @version 1.001
11
 */
12 View Code Duplication
abstract class BaseComponent {
0 ignored issues
show
Duplication introduced by
This class 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...
13
	public $jquery_code_for_compile=array ();
14
	protected $params=array ();
15
16
	/**
17
	 *
18
	 * @var JsUtils
19
	 */
20
	protected $js;
21
22
	public function __construct(JsUtils $js=NULL) {
23
		$this->js=$js;
24
	}
25
26
	protected function getParamsAsJSON($params) {
27
		$result="";
28
		if (sizeof($params)>0) {
29
			$result=json_encode($params, JSON_UNESCAPED_SLASHES);
30
			$result=str_ireplace("%quote%", "\"", $result);
31
		}
32
		return $result;
33
	}
34
35
	public function setParam($key, $value) {
36
		$this->params [$key]=$value;
37
		return $this;
38
	}
39
40
	public function getParam($key) {
41
		$value=null;
42
		if (array_key_exists($key, $this->params))
43
			$value=$this->params [$key];
44
		return $value;
45
	}
46
47
	public function getParams() {
48
		return $this->params;
49
	}
50
51
	public function compile(JsUtils $js=NULL) {
52
		if ($js==NULL)
53
			$js=$this->js;
54
		$script=$this->getScript();
55
		$js->addToCompile($script);
56
	}
57
58
	protected function setParamCtrl($key, $value, $typeCtrl) {
59
		if (is_array($typeCtrl)) {
60
			if (array_search($value, $typeCtrl)===false)
61
				throw new \Exception("La valeur passée a propriété `".$key."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}");
62
		} else {
63
			if (!$typeCtrl($value)) {
64
				throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key);
65
			}
66
		}
67
		$this->setParam($key, $value);
68
	}
69
70
	public function setParams($params) {
71
		foreach ( $params as $k => $v ) {
72
			$method="set".ucfirst($k);
73
			if (method_exists($this, $method))
74
				$this->$method($v);
75
			else {
76
				$this->setParam($k, $v);
77
				trigger_error("`".$k."` property n'existe pas", E_USER_NOTICE);
78
			}
79
		}
80
81
		return $this;
82
	}
83
84
	abstract public function getScript();
85
}