Completed
Push — master ( 8898f5...6e5e78 )
by WEBEWEB
04:00
created

AbstractGridTwigExtension::bootstrapGrid()   F

Complexity

Conditions 15
Paths 1280

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 2.9968
cc 15
eloc 12
nc 1280
nop 6

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Grid;
13
14
use WBW\Bundle\BootstrapBundle\Twig\Extension\AbstractBootstrapTwigExtension;
15
16
/**
17
 * Abstract grid Twig extension.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Grid
21
 * @abstract
22
 */
23
abstract class AbstractGridTwigExtension extends AbstractBootstrapTwigExtension {
24
25
    /**
26
     * Constructor.
27
     */
28
    protected function __construct() {
29
        parent::__construct();
30
    }
31
32
    /**
33
     * Displays a Bootstrap grid.
34
     *
35
     * @param string $lg The large column size.
36
     * @param string $md The medium column size.
37
     * @param string $sm The small column size.
38
     * @param string $xs The extra-small column size.
39
     * @param string $recopy Recopy ?
40
     * @param string $prefix The column prefix.
41
     * @return string Returns the Bootstrap grid.
42
     */
43
    final protected function bootstrapGrid($lg, $md, $sm, $xs, $recopy, $prefix) {
44
45
        // Recopy.
46
        foreach ([&$lg, &$md, &$sm, &$xs] as &$current) {
0 ignored issues
show
Bug introduced by
The expression array(&$lg, &$md, &$sm, &$xs) cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
47
            if (1 <= $current && $current <= 12) {
48
                $found = $current;
49
            }
50
            if (null === $current && true === $recopy && true === (isset($found))) {
51
                $current = $found;
0 ignored issues
show
Bug introduced by
The variable $found 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...
52
            }
53
        }
54
55
        // Initialize the columns.
56
        $columns = [];
57
58
        $columns[] = 1 <= $lg && $lg <= 12 ? "col-lg-" . $prefix . $lg : null;
59
        $columns[] = 1 <= $md && $md <= 12 ? "col-md-" . $prefix . $md : null;
60
        $columns[] = 1 <= $sm && $sm <= 12 ? "col-sm-" . $prefix . $sm : null;
61
        $columns[] = 1 <= $xs && $xs <= 12 ? "col-xs-" . $prefix . $xs : null;
62
63
        // Return the columns.
64
        return trim(implode(" ", $columns));
65
    }
66
67
}
68