Completed
Push — master ( 5df1fc...70601d )
by Jean-Christophe
03:41
created

HtmlGrid::colCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\common\html\HtmlCollection;
6
use Ajax\semantic\html\content\HtmlGridRow;
7
use Ajax\semantic\html\base\constants\Wide;
8
use Ajax\semantic\html\base\constants\VerticalAlignment;
9
use Ajax\semantic\html\base\HtmlSemCollection;
10
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
11
12
/**
13
 * Semantic Grid component
14
 * @see http://semantic-ui.com/collections/grid.html
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlGrid extends HtmlSemCollection{
19
	use TextAlignmentTrait;
20
	private $_createCols;
21
	private $_colSizing=true;
22
	private $_implicitRows=false;
23
	public function __construct( $identifier,$numRows=1,$numCols=NULL,$createCols=true,$implicitRows=false){
24
		parent::__construct( $identifier, "div","ui grid");
25
		$this->_implicitRows=$implicitRows;
26
		$this->_createCols=$createCols;
27
		if(isset($numCols)){
28
			//if($this->_createCols){
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
				$this->_colSizing=false;
30
			//}
31
			$this->setWide($numCols);
32
		}
33
		$this->setNumRows($numRows,$numCols);
34
	}
35
36
	public function setWide($wide){
37
		$wide=Wide::getConstants()["W".$wide];
38
		$this->addToPropertyCtrl("class", $wide, Wide::getConstants());
39
		return $this->addToPropertyCtrl("class","column",array("column"));
40
	}
41
42
	/**
43
	 * Create $numRows rows
44
	 * @param int $numRows
45
	 * @param int $numCols
46
	 * @return \Ajax\semantic\html\collections\HtmlGrid
47
	 */
48 View Code Duplication
	public function setNumRows($numRows,$numCols=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...
49
		$count=$this->count();
50
		for($i=$count;$i<$numRows;$i++){
51
			$this->addItem($numCols);
52
		}
53
		return $this;
54
	}
55
56
	public function setNumCols($numCols){
57
		$count=$this->count();
58
		for($i=0;$i<$count;$i++){
59
			$this->getItem($i)->setNumCols($numCols);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajax\common\html\HtmlDoubleElement as the method setNumCols() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\semantic\html\collections\HtmlGrid, Ajax\semantic\html\content\HtmlGridRow. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
60
		}
61
		//if($this->_colSizing===false)
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
			$this->setWide($numCols);
63
		return $this;
64
	}
65
66
	/**
67
	 * return the row at $index
68
	 * @param int $index
69
	 * @return \Ajax\semantic\html\collections\HtmlGridRow
70
	 */
71
	public function getRow($index){
72
		return $this->getItem($index);
73
	}
74
75
	/**
76
	 * @param int $row
77
	 * @param int $col
78
	 * @return \Ajax\semantic\html\collections\HtmlGridCol
79
	 */
80
	public function getCell($row,$col){
81
		$row=$this->getItem($row);
82
		if(isset($row)){
83
			$col=$row->getItem($col);
84
		}
85
		return $col;
86
	}
87
88
	/**
89
	 * Adds dividers between columns ($vertically=false) or between rows ($vertically=true)
90
	 * @param boolean $vertically
91
	 * @return \Ajax\semantic\html\collections\HtmlGrid
92
	 */
93
	public function setDivided($vertically=false){
94
		$value=($vertically===true)?"vertically divided":"divided";
95
		return $this->addToPropertyCtrl("class", $value,array("divided"));
96
	}
97
98
	/**
99
	 * Divides rows into cells
100
	 * @param boolean $internal true for internal cells
101
	 * @return \Ajax\semantic\html\collections\HtmlGrid
102
	 */
103
	public function setCelled($internal=false){
104
		$value=($internal===true)?"internal celled":"celled";
105
		return $this->addToProperty("class", $value);
106
	}
107
108
	/**
109
	 * automatically resize all elements to split the available width evenly
110
	 * @return \Ajax\semantic\html\collections\HtmlGrid
111
	 */
112
	public function setEqualWidth(){
113
		return $this->addToProperty("class", "equal width");
114
	}
115
116
	/**
117
	 * Adds vertical or/and horizontal gutters
118
	 * @param string $value
119
	 * @return \Ajax\semantic\html\collections\HtmlGrid
120
	 */
121
	public function setPadded($value=NULL){
122
		if(isset($value))
123
			$this->addToPropertyCtrl("class", $value,array("vertically","horizontally"));
124
		return $this->addToProperty("class", "padded");
125
	}
126
127
	/**
128
	 * @param boolean $very
129
	 * @return \Ajax\semantic\html\collections\HtmlGrid
130
	 */
131
	public function setRelaxed($very=false){
132
		$value=($very===true)?"very relaxed":"relaxed";
133
		return $this->addToProperty("class", $value);
134
	}
135
136
	public function setVerticalAlignment($value=VerticalAlignment::MIDDLE){
137
		return $this->addToPropertyCtrl("class", $value." aligned",VerticalAlignment::getConstantValues("aligned"));
138
	}
139
140
	/**
141
	 * {@inheritDoc}
142
	 * @see \Ajax\common\html\HtmlCollection::createItem()
143
	 */
144
	protected function createItem($value){
145
		if($this->_createCols===false)
146
			$value=null;
147
		$item=new HtmlGridRow($this->identifier."-row-".($this->count()+1),$value,$this->_colSizing,$this->_implicitRows);
148
		return $item;
149
	}
150
151
	public function setValues($values){
152
		$count=$this->count();
153
		if($this->_createCols===false){
154
			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...
155
				$colSize=\sizeof($values[$i]);
156
				$this->addItem(new HtmlGridRow($this->identifier."-row-".($this->count()+1),$colSize,$this->_colSizing,$this->_implicitRows));
157
			}
158
		}
159
		$count=\min(array($this->count(),\sizeof($values)));
160
		for($i=0;$i<$count;$i++){
161
			$this->content[$i]->setValues($values[$i],$this->_createCols===false);
162
		}
163
	}
164
165
	public function rowCount(){
166
		return $this->count();
167
	}
168
169
	public function colCount(){
170
		$result=0;
171
		if($this->count()>0){
172
			$result=$this->content[0]->count();
173
		}
174
		return $result;
175
	}
176
177
}