Completed
Push — master ( 09bbc1...c831e1 )
by Jean-Christophe
03:24
created

HtmlGridSystem::setContentForAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Ajax\bootstrap\html;
3
4
use Ajax\bootstrap\html\base\HtmlDoubleElement;
5
use Ajax\bootstrap\html\content\HtmlGridRow;
6
use Ajax\bootstrap\html\content\HtmlGridCol;
7
use Ajax\JsUtils;
8
use Phalcon\Mvc\View;
9
10
/**
11
 * Composant Twitter Bootstrap Grid system
12
 * @see http://getbootstrap.com/css/#grid
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlGridSystem extends HtmlDoubleElement {
17
	private $rows;
18
	
19
	public function __construct($identifier,$numRows=1,$numCols=NULL){
20
		parent::__construct($identifier,"div");
21
		$this->setProperty("class", "container-fluid");
22
		$this->rows=array();
23
		$this->setNumRows($numRows,$numCols);
24
	}
25
	
26
	/**
27
	 * Add a new row
28
	 * @param int $numCols 
29
	 * @return \Ajax\bootstrap\html\content\HtmlGridRow
30
	 */
31
	public function addRow($numCols=NULL){
32
		$row=new HtmlGridRow($this->identifier."-row-".(sizeof($this->rows)+1),$numCols);
33
		$this->rows[]=$row;
34
		return $row;
35
	}
36
	
37
	/**
38
	 * return the row at $index
39
	 * @param int $index
40
	 * @param boolean $force add the row at $index if true
41
	 * @return \Ajax\bootstrap\html\content\HtmlGridRow
42
	 */
43
	public function getRow($index,$force=true){
44 View Code Duplication
		if($index<sizeof($this->rows)){
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...
45
			$result=$this->rows[$index-1];
46
		}else if ($force){
47
			$this->setNumRows($index);
48
			$result=$this->rows[$index-1];
49
		}
50
		return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
51
	}
52
	
53
	/**
54
	 * Create $numRows rows
55
	 * @param int $numRows
56
	 * @param int $numCols
57
	 * @return \Ajax\bootstrap\html\HtmlGridSystem
58
	 */
59
	public function setNumRows($numRows,$numCols=NULL){
60
		for($i=sizeof($this->rows);$i<$numRows;$i++){
61
			$this->addRow($numCols);
62
		}
63
		return $this;
64
	}
65
	
66
	/**
67
	 * @param int $row
68
	 * @param int $col
69
	 * @param $force add the cell at $row,$col if true
70
	 * @return HtmlGridCol
71
	 */
72
	public function getCell($row,$col,$force=true){
73
		$row=$this->getRow($row,$force);
74
		if(isset($row)){
75
			$col=$row->getCol($col,$force);
76
		}
77
		return $col;
78
	}
79
	
80
	/**
81
	 * @param int $row
82
	 * @param int $col
83
	 * @return HtmlGridCol
84
	 */
85
	public function getCellAt($row,$col,$force=true){
86
		$row=$this->getRow($row,$force);
87
		if(isset($row)){
88
			$col=$row->getColAt($col,$force);
89
		}
90
		return $col;
91
	}
92
	
93
	public function compile(JsUtils $js=NULL, View $view=NULL) {
94
		foreach ($this->rows as $row){
95
			$this->addContent($row);
96
		}
97
		return parent::compile($js,$view);
98
	}
99
	public function setContentForAll($content){
100
		foreach ($this->rows as $row){
101
			$row->setContentForAll($content);
102
		}
103
	}
104
}