Completed
Push — master ( 98392d...8b7375 )
by Jean-Christophe
04:05
created

HtmlCard   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 117
Duplicated Lines 17.09 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 27
c 3
b 0
f 0
lcom 1
cbo 7
dl 20
loc 117
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createContent() 6 6 1
A addElementInContent() 0 7 2
A getPart() 0 8 3
A addHeader() 0 6 2
A addImage() 0 7 2
A addReveal() 7 7 2
A addRevealImage() 7 7 2
A addExtraContent() 0 3 1
A addContent() 0 6 2
A addCardHeaderContent() 0 4 1
A addCardContent() 0 4 1
A getCardContent() 0 3 1
A getCardExtraContent() 0 3 1
A getCardImage() 0 3 1
A getCardHeader() 0 3 1
A compile() 0 4 1
A asLink() 0 7 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\views;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\elements\HtmlHeader;
7
use Ajax\JsUtils;
8
use Ajax\service\JArray;
9
use Phalcon\Mvc\View;
10
use Ajax\semantic\html\elements\HtmlImage;
11
use Ajax\semantic\html\content\card\HtmlCardContent;
12
use Ajax\semantic\html\content\card\HtmlCardHeaderContent;
13
use Ajax\semantic\html\elements\HtmlReveal;
14
use Ajax\semantic\html\base\constants\RevealType;
15
16
class HtmlCard extends HtmlSemDoubleElement {
17
18
	public function __construct($identifier) {
19
		parent::__construct($identifier, "div", "ui card", array ());
20
	}
21
22 View Code Duplication
	private function createContent($content, $baseClass="content") {
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...
23
		$count=\sizeof($this->content);
24
		$result=new HtmlCardContent("content-" . $count . "-" . $this->identifier, $content);
25
		$result->setClass($baseClass);
26
		return $result;
27
	}
28
29
	private function addElementInContent($key, $element) {
30
		if (\array_key_exists($key, $this->content) === false) {
31
			$this->content[$key]=array ();
32
		}
33
		$this->content[$key][]=$element;
34
		return $element;
35
	}
36
37
	private function getPart($part, $index=NULL) {
38
		if (\array_key_exists($part, $this->content)) {
39
			if (isset($index))
40
				return $this->content[$part][$index];
41
			return $this->content[$part];
42
		}
43
		return NULL;
44
	}
45
46
	public function addHeader($header, $niveau=4, $type="page") {
47
		if (!$header instanceof HtmlHeader) {
48
			$header=new HtmlHeader("header-" . $this->identifier, $niveau, $header, $type);
49
		}
50
		$this->content["header"]=$this->createContent($header);
51
	}
52
53
	public function addImage($image, $title="") {
54
		if (!$image instanceof HtmlImage) {
55
			$image=new HtmlImage("image-" . $this->identifier, $image, $title);
56
		}
57
		$image->setClass("image");
58
		return $this->addElementInContent("content", $image);
59
	}
60
61 View Code Duplication
	public function addReveal($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
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...
62
		$reveal=$visibleContent;
63
		if (!$visibleContent instanceof HtmlReveal) {
64
			$reveal=new HtmlReveal("reveral-" . $this->identifier, $visibleContent, $hiddenContent, $type, $attributeType);
65
		}
66
		return $this->addElementInContent("content", $reveal);
67
	}
68
69 View Code Duplication
	public function addRevealImage($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
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...
70
		$reveal=$visibleContent;
71
		if (!$visibleContent instanceof HtmlReveal) {
72
			return $this->addReveal(new HtmlImage("", $visibleContent), new HtmlImage("", $hiddenContent), $type, $attributeType);
73
		}
74
		return $this->addElementInContent("content", $reveal);
75
	}
76
77
	public function addExtraContent($content=array()) {
78
		return $this->addElementInContent("extra-content", $this->createContent($content, "extra content"));
79
	}
80
81
	public function addContent($content=array(), $before=false) {
82
		if (!$content instanceof HtmlCardContent) {
83
			$content=$this->createContent($content);
84
		}
85
		return $this->addElementInContent("content", $content);
86
	}
87
88
	public function addCardHeaderContent($header, $metas=array(), $description=NULL) {
89
		$count=\sizeof($this->content);
90
		return $this->addElementInContent("content", new HtmlCardHeaderContent("content-" . $count . "-" . $this->identifier, $header, $metas, $description));
91
	}
92
93
	public function addCardContent($content=array()) {
94
		$count=\sizeof($this->content);
95
		return $this->addElementInContent("content", new HtmlCardContent("content-" . $count . "-" . $this->identifier, $content));
96
	}
97
98
	public function getCardContent($index=NULL) {
99
		return $this->getPart("content", $index);
100
	}
101
102
	public function getCardExtraContent() {
103
		return $this->getPart("extra-content");
104
	}
105
106
	public function getCardImage() {
107
		return $this->getPart("image");
108
	}
109
110
	public function getCardHeader() {
111
		return $this->getPart("header");
112
	}
113
114
	/**
115
	 *
116
	 * {@inheritDoc}
117
	 *
118
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
119
	 */
120
	public function compile(JsUtils $js=NULL, View $view=NULL) {
121
		$this->content=JArray::sortAssociative($this->content, [ "header","image","content","extra-content" ]);
122
		return parent::compile($js, $view);
123
	}
124
125
	public function asLink($href="") {
126
		$this->addToProperty("class", "link");
127
		if ($href !== "") {
128
			$this->setProperty("href", $href);
129
		}
130
		return $this;
131
	}
132
}