Completed
Push — master ( 63bf24...09bbc1 )
by Jean-Christophe
03:16
created

HtmlGridSystem   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 2
dl 9
loc 73
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addRow() 0 5 1
A getRow() 9 9 3
A setNumRows() 0 6 2
A getCell() 0 7 2
A getCellAt() 0 7 2
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
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){
20
		parent::__construct($identifier,"div");
21
		$this->setProperty("class", "container");
22
		$this->rows=array();
23
		$this->setNumRows($numRows);
24
	}
25
	
26
	/**
27
	 * @return \Ajax\bootstrap\html\content\HtmlGridRow
28
	 */
29
	public function addRow(){
30
		$row=new HtmlGridRow($this->identifier."-row-".(sizeof($this->rows)+1));
31
		$this->rows[]=$row;
32
		return $row;
33
	}
34
	
35
	/**
36
	 * @param int $index
37
	 * @return \Ajax\bootstrap\html\content\HtmlGridRow
38
	 */
39 View Code Duplication
	public function getRow($index,$force=true){
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...
40
		if($index<sizeof($this->rows)){
41
			$result=$this->rows[$index-1];
42
		}else if ($force){
43
			$this->setNumRows($index);
44
			$result=$this->rows[$index-1];
45
		}
46
		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...
47
	}
48
	
49
	public function setNumRows($numRows){
50
		for($i=sizeof($this->rows);$i<$numRows;$i++){
51
			$this->addRow();
52
		}
53
		return $this;
54
	}
55
	
56
	/**
57
	 * @param int $row
58
	 * @param int $col
59
	 * @return HtmlGridCol
60
	 */
61
	public function getCell($row,$col,$force=true){
62
		$row=$this->getRow($row,$force);
63
		if(isset($row)){
64
			$col=$row->getCol($col,$force);
65
		}
66
		return $col;
67
	}
68
	
69
	/**
70
	 * @param int $row
71
	 * @param int $col
72
	 * @return HtmlGridCol
73
	 */
74
	public function getCellAt($row,$col,$force=true){
75
		$row=$this->getRow($row,$force);
76
		if(isset($row)){
77
			$col=$row->getColAt($col,$force);
78
		}
79
		return $col;
80
	}
81
	
82
	public function compile(JsUtils $js=NULL, View $view=NULL) {
83
		foreach ($this->rows as $row){
84
			$this->addContent($row);
85
		}
86
		return parent::compile($js,$view);
87
	}
88
}