Completed
Push — master ( 1d23a1...916f6f )
by Jean-Christophe
03:30
created

HtmlHeader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 14
c 6
b 0
f 0
lcom 2
cbo 6
dl 6
loc 72
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A asPageHeader() 0 3 1
A asContentHeader() 0 7 2
A asIcon() 0 5 1
A asImage() 0 5 1
A asTitle() 6 16 3
A getImage() 0 3 1
A setDividing() 0 3 1
A image() 0 6 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\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\Size;
7
use Ajax\common\html\HtmlDoubleElement;
8
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
9
use Ajax\common\html\html5\HtmlImg;
10
11
class HtmlHeader extends HtmlSemDoubleElement {
12
	use TextAlignmentTrait;
13
	protected $image;
14
15
	public function __construct($identifier, $niveau=1, $content=NULL, $type="page") {
16
		parent::__construct($identifier, "div", "ui header");
17
		$this->_template="<%tagName% %properties%>%image%%content%</%tagName%>";
18
		if (isset($type)) {
19
			if ($type=="page") {
20
				$this->asPageHeader($niveau);
21
			} else
22
				$this->asContentHeader($niveau);
23
		}
24
		$this->content=$content;
25
	}
26
27
	public function asPageHeader($niveau) {
28
		$this->tagName="h".$niveau;
29
	}
30
31
	public function asContentHeader($niveau) {
32
		$this->tagName="div";
33
		if (\is_int($niveau)) {
34
			$niveau=Size::getConstantValues()[$niveau];
35
		}
36
		$this->setSize($niveau);
37
	}
38
39
	public function asIcon($icon, $title, $subHeader=NULL) {
40
		$this->addToProperty("class", "icon");
41
		$this->image=new HtmlIcon("icon-".$this->identifier, $icon);
42
		return $this->asTitle($title, $subHeader);
43
	}
44
45
	public function asImage($src, $title, $subHeader=NULL) {
46
		$this->image=new HtmlImg("img-".$this->identifier, $src, $title);
47
		$this->image->setClass("ui image");
48
		return $this->asTitle($title, $subHeader);
49
	}
50
51
	public function asTitle($title, $subHeader=NULL) {
52
		if (!\is_object($title)) {
53
			$this->content=new HtmlDoubleElement("content-".$this->identifier, "div");
54
			$this->content->setContent($title);
55
		} else {
56
			$this->content=$title;
57
		}
58
		$this->content->setClass("content");
59 View Code Duplication
		if (isset($subHeader)) {
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...
60
			$sub=new HtmlDoubleElement("subheader-".$this->identifier, "div");
61
			$sub->setClass("sub header");
62
			$sub->setContent($subHeader);
63
			$this->content->addContent($sub);
64
		}
65
		return $this;
66
	}
67
68
	public function getImage() {
69
		return $this->image;
70
	}
71
72
	public function setDividing() {
73
		$this->addToProperty("class", "dividing");
74
	}
75
76
	public static function image($identifier, $image, $niveau=1, $header=NULL, $subHeader=NULL) {
77
		$result=new HtmlHeader($identifier, $niveau, $header);
78
		$result->asImage($image, $header, $subHeader);
79
		$result->getImage()->addToProperty("class", "mini rounded");
80
		return $result;
81
	}
82
}