UtilizationColumn   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
B getDataCellValue() 0 22 7
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\grid;
12
13
use hiqdev\higrid\DataColumn;
14
use Yii;
15
use yii\bootstrap\Progress;
16
17
class UtilizationColumn extends DataColumn
18
{
19
    public $format = 'html';
20
21
    public function init()
22
    {
23
        parent::init();
24
        $this->label = Yii::t('hipanel.hosting.ipam', 'Utilization');
25
    }
26
27
    public function getDataCellValue($model, $key, $index)
28
    {
29
        $prc = $model->utilization;
30
        switch ($prc) {
31
            case $prc >= 0 && $prc <= 40:
32
                $level = 'progress-bar-success';
33
                break;
34
            case $prc >= 41 && $prc <= 70:
35
                $level = 'progress-bar-warning';
36
                break;
37
            case $prc >= 71 && $prc <= 100:
38
                $level = 'progress-bar-danger';
39
                break;
40
        }
41
42
        return Progress::widget([
43
            'percent' => $prc,
44
            'label' => $prc . '%',
45
            'barOptions' => ['class' => $level],
0 ignored issues
show
Bug introduced by
The variable $level 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...
46
            'options' => ['style' => 'background-color: grey;'],
47
        ]);
48
    }
49
}
50