Completed
Push — master ( cd0fd2...97e05d )
by Jean-Christophe
03:14
created

HtmlGridRow   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 17
c 5
b 0
f 0
lcom 3
cbo 5
dl 7
loc 93
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A setWidth() 7 7 2
A getCol() 0 3 1
A setStretched() 0 3 1
A setColor() 0 3 1
A setTextAlignment() 0 3 1
A setValues() 0 12 4
A createItem() 0 4 1
A compile() 0 6 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\content;
4
5
use Ajax\common\html\HtmlCollection;
6
use Ajax\semantic\html\base\Wide;
7
use Ajax\semantic\html\base\Color;
8
use Ajax\semantic\html\base\TextAlignment;
9
use Ajax\JsUtils;
10
use Phalcon\Mvc\View;
11
12
13
/**
14
 * A row for the Semantic Grid component
15
 * @see http://semantic-ui.com/collections/grid.html
16
 * @author jc
17
 * @version 1.001
18
 */
19
class HtmlGridRow extends HtmlCollection{
20
21
	private $_colSize;
22
	private $_implicite=false;
23
	public function __construct( $identifier,$numCols=NULL,$colSizing=false,$implicite=false){
24
		parent::__construct( $identifier,"div");
25
		$this->_implicite=$implicite;
26
		$this->setClass("row");
27
		$width=null;
28
		if(isset($numCols)){
29
			$numCols=min(16,$numCols);
30
			$numCols=max(1,$numCols);
31
			if($colSizing)
32
				$width=(int)(16/$numCols);
33
			else
34
				$this->_colSize=16/$numCols;
35
			for ($i=0;$i<$numCols;$i++){
36
				$this->addItem($width);
37
			}
38
		}
39
	}
40
41
	/**
42
	 * Defines the row width
43
	 * @param int $width
44
	 * @return \Ajax\semantic\html\content\HtmlGridRow
45
	 */
46 View Code Duplication
	public function setWidth($width){
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...
47
		if(\is_int($width)){
48
			$width=Wide::getConstants()["W".$width];
49
		}
50
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
51
		return $this->addToPropertyCtrl("class", "column",array("column"));
52
	}
53
54
	/**
55
	 * return the col at $index
56
	 * @param int $index
57
	 * @return \Ajax\semantic\html\collections\HtmlGridCol
58
	 */
59
	public function getCol($index){
60
		return $this->getItem($index);
61
	}
62
63
	/**
64
	 * stretch the row contents to take up the entire column height
65
	 * @return \Ajax\semantic\html\content\HtmlGridRow
66
	 */
67
	public function setStretched(){
68
		return $this->addToProperty("class", "stretched");
69
	}
70
71
	/**
72
	 * @param string $color
73
	 * @return \Ajax\semantic\html\content\HtmlGridRow
74
	 */
75
	public function setColor($color){
76
		return $this->addToPropertyCtrl("class", $color,Color::getConstants());
77
	}
78
79
	public function setTextAlignment($value=TextAlignment::LEFT){
80
		return $this->addToPropertyCtrl("class", $value,TextAlignment::getConstants());
81
	}
82
83
	public function setValues($values,$force=false){
84
		$count=$this->count();
85
		if($force===true){
86
			for($i=$count;$i<\sizeof($values);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
87
				$this->addItem(new HtmlGridCol($this->identifier."-col-".($this->count()+1),null));
88
			}
89
		}
90
		$count=\min(array($this->count(),\sizeof($values)));
91
		for($i=0;$i<$count;$i++){
92
			$this->content[$i]->setValue($values[$i]);
93
		}
94
	}
95
96
	/**
97
	 * {@inheritDoc}
98
	 * @see \Ajax\common\html\HtmlCollection::createItem()
99
	 */
100
	protected function createItem($value){
101
		$col=new HtmlGridCol($this->identifier."-col-".($this->count()+1),$value);
102
		return $col;
103
	}
104
105
	public function compile(JsUtils $js=NULL,View $view=NULL){
106
		if($this->_implicite===true){
107
			$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
108
		}
109
		return parent::compile($js,$view);
110
	}
111
}