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

HtmlGridRow   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addCol() 0 5 1
A addColAt() 0 4 1
A getCol() 9 9 3
A getColAt() 0 13 4
A compile() 0 7 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\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){
18
		parent::__construct($identifier,"div");
19
		$this->setProperty("class", "row");
20
		$this->cols=array();
21
	}
22
	
23
	public function addCol($size=CssSize::SIZE_MD,$width=1){
24
		$col=new HtmlGridCol($this->identifier."-col-".(sizeof($this->cols)+1),$size,$width);
25
		$this->row[]=$col;
0 ignored issues
show
Bug introduced by
The property row does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
		return $col;
27
	}
28
	
29
	public function addColAt($size=CssSize::SIZE_MD,$width=1,$offset=1){
30
		$col=$this->addCol($size,$width);
31
		return $col->setOffset($size, max($offset,sizeof($this->cols)+1));
32
	}
33
	
34 View Code Duplication
	public function getCol($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...
35
		if($index<sizeof($this->cols)+1){
36
			$result=$this->cols[$index-1];
37
		}else if ($force){
38
			$result=$this->addColAt(CssSize::SIZE_MD,1,$index);
39
			$this->cols[]=$result;
40
		}
41
		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...
42
	}
43
	
44
	public function getColAt($offset,$force=true){
45
		$result=null;
46
		foreach ($this->cols as $col){
47
			$offsets=$col->getOffsets();
48
			if($result=array_search($offset, $offsets)){
49
				break;
50
			}
51
		}
52
		if(!isset($result)){
53
			$result=$this->getCol($offset,$force);
54
		}
55
		return $result;
56
	}
57
	
58
	public function compile(JsUtils $js=NULL, View $view=NULL) {
59
	
60
		foreach ($this->cols as $col){
61
			$this->addContent($col);
62
		}
63
		return parent::compile($js,$view);
64
	}
65
}