Completed
Push — master ( 0c3815...012895 )
by Jean-Christophe
03:40
created

HtmlGridRow::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\content;
4
5
use Ajax\semantic\html\base\constants\Wide;
6
use Ajax\semantic\html\base\constants\Color;
7
use Ajax\JsUtils;
8
use Phalcon\Mvc\View;
9
use Ajax\semantic\html\base\HtmlSemCollection;
10
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
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 HtmlSemCollection{
20
	use TextAlignmentTrait;
21
22
	private $_colSize;
23
	private $_implicite=false;
24
25
	public function __construct( $identifier,$numCols=NULL,$colSizing=false,$implicite=false){
26
		parent::__construct( $identifier,"div","row");
27
		$this->_implicite=$implicite;
28
		$width=null;
29
		if(isset($numCols)){
30
			$numCols=min(16,$numCols);
31
			$numCols=max(1,$numCols);
32
			if($colSizing)
33
				$width=(int)(16/$numCols);
34
			else
35
				$this->_colSize=16/$numCols;
36
			for ($i=0;$i<$numCols;$i++){
37
				$this->addItem($width);
38
			}
39
		}
40
	}
41
42
	/**
43
	 * Defines the row width
44
	 * @param int $width
45
	 * @return \Ajax\semantic\html\content\HtmlGridRow
46
	 */
47 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...
48
		if(\is_int($width)){
49
			$width=Wide::getConstants()["W".$width];
50
		}
51
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
52
		return $this->addToPropertyCtrl("class", "column",array("column"));
53
	}
54
55
	/**
56
	 * return the col at $index
57
	 * @param int $index
58
	 * @return \Ajax\semantic\html\collections\HtmlGridCol
59
	 */
60
	public function getCol($index){
61
		return $this->getItem($index);
62
	}
63
64
	public function setColsCount($colsCount,$toCreate=true){
65
		$this->setWidth($colsCount);
66
		if($toCreate===true){
67
			$count=$this->count();
68
			for($i=$count;$i<$colsCount;$i++){
69
				$this->addItem(null);
70
			}
71
		}
72
		return $this;
73
	}
74
75
	public function addCols($colCount){
76
		for($i=0;$i<$colCount;$i++){
77
			$this->addItem(null);
78
		}
79
		return $this;
80
	}
81
82
	/**
83
	 * stretch the row contents to take up the entire column height
84
	 * @return \Ajax\semantic\html\content\HtmlGridRow
85
	 */
86
	public function setStretched(){
87
		return $this->addToProperty("class", "stretched");
88
	}
89
90
	/**
91
	 * @param string $color
92
	 * @return \Ajax\semantic\html\content\HtmlGridRow
93
	 */
94
	public function setColor($color){
95
		return $this->addToPropertyCtrl("class", $color,Color::getConstants());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->addToPrope...Color::getConstants()); (Ajax\semantic\html\content\HtmlGridRow) is incompatible with the return type of the parent method Ajax\semantic\html\base\...SemCollection::setColor of type Ajax\semantic\html\base\HtmlSemDoubleElement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
	}
97
98
	public function setValues($values,$force=false){
99
		$count=$this->count();
100
		if($force===true){
101
			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...
102
				$this->addItem(new HtmlGridCol($this->identifier."-col-".($this->count()+1),null));
103
			}
104
		}
105
		$count=\min(array($this->count(),\sizeof($values)));
106
		for($i=0;$i<$count;$i++){
107
			$this->content[$i]->setValue($values[$i]);
108
		}
109
		return $this;
110
	}
111
112
	/**
113
	 * {@inheritDoc}
114
	 * @see \Ajax\common\html\HtmlCollection::createItem()
115
	 */
116
	protected function createItem($value){
117
		$col=new HtmlGridCol($this->identifier."-col-".($this->count()+1),$value);
118
		return $col;
119
	}
120
121
	public function compile(JsUtils $js=NULL,View $view=NULL){
122
		if($this->_implicite===true){
123
			$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
124
		}
125
		return parent::compile($js,$view);
126
	}
127
}