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

HtmlGridRow::delete()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 11
nc 4
nop 3
1
<?php
2
namespace Ajax\bootstrap\html\content;
3
4
use Ajax\bootstrap\html\base\HtmlDoubleElement;
5
use Ajax\bootstrap\html\base\CssSize;
6
use Ajax\JsUtils;
7
use Phalcon\Mvc\View;
8
9
/**
10
 * Inner element for Twitter Bootstrap Grid row
11
 * @see http://getbootstrap.com/css/#grid
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlGridRow extends HtmlDoubleElement {
16
	private $cols;
17
	public function __construct($identifier,$numCols=NULL){
18
		parent::__construct($identifier,"div");
19
		$this->setProperty("class", "row");
20
		$this->cols=array();
21
		if(isset($numCols)){
22
			$numCols=min(12,$numCols);
23
			$numCols=max(1,$numCols);
24
			$width=12/$numCols;
25
			for ($i=0;$i<$numCols;$i++){
26
				$this->addCol(CssSize::SIZE_MD,$width);
27
			}
28
		}
29
	}
30
	
31
	public function addCol($size=CssSize::SIZE_MD,$width=1){
32
		$col=new HtmlGridCol($this->identifier."-col-".(sizeof($this->cols)+1),$size,$width);
33
		$this->cols[]=$col;
34
		return $col;
35
	}
36
	
37
	public function addColAt($size=CssSize::SIZE_MD,$width=1,$offset=1){
38
		$col=$this->addCol($size,$width);
39
		return $col->setOffset($size, max($offset,sizeof($this->cols)+1));
40
	}
41
	
42
	public function getCol($index,$force=true){
43 View Code Duplication
		if($index<sizeof($this->cols)+1){
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...
44
			$result=$this->cols[$index-1];
45
		}else if ($force){
46
			$result=$this->addColAt(CssSize::SIZE_MD,1,$index);
47
		}
48
		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...
49
	}
50
	
51
	public function getColAt($offset,$force=true){
52
		$result=null;
53
		foreach ($this->cols as $col){
54
			$offsets=$col->getOffsets();
55
			if($result=array_search($offset, $offsets)){
56
				break;
57
			}
58
		}
59
		if(!$result || isset($result)==false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
60
			$result=$this->getCol($offset,$force);
61
		}
62
		return $result;
63
	}
64
	
65
	public function compile(JsUtils $js=NULL, View $view=NULL) {
66
	
67
		foreach ($this->cols as $col){
68
			$this->addContent($col);
69
		}
70
		return parent::compile($js,$view);
71
	}
72
	public function getCols() {
73
		return $this->cols;
74
	}
75
	
76
	public function setContentForAll($content){
77
		foreach ($this->cols as $col){
78
			$col->setContent($content);
79
		}
80
	}
81
	public function merge($size=CssSize::SIZE_MD,$start,$width){
82
		$col=$this->getColAt($start,false);
83
		if(isset($col)){
84
			$col->setWidth($size,$width+1);
85
			$this->delete($size,$start+1, $width);
86
		}
87
	}
88
	public function delete($size=CssSize::SIZE_MD,$start,$width){
89
		while($start<sizeof($this->cols)+1 && $width>0){
90
			$col=$this->getColAt($start,false);
91
			if(isset($col)){
92
				$widthCol=$col->getWidth($size);
93
				if($widthCol<=$width){
94
					unset($this->cols[$start-1]);
95
					$this->cols = array_values($this->cols);
96
					$width=$width-$widthCol;
97
				}
98
			}else{
99
				$width=0;
100
			}
101
		}
102
	}
103
}