Completed
Push — master ( 478f67...9afd4a )
by Andrii
04:25
created

ChargeGridView::defaultColumns()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 67
Code Lines 47

Duplication

Lines 22
Ratio 32.84 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 67
rs 6.6523
cc 8
eloc 47
nc 1
nop 0

How to fix   Long Method   

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
namespace hipanel\modules\finance\grid;
4
5
6
use hipanel\grid\CurrencyColumn;
7
use hipanel\widgets\ArraySpoiler;
8
use Yii;
9
use yii\helpers\Html;
10
11
class ChargeGridView extends \hipanel\grid\BoxedGridView
12
{
13
    public static function defaultColumns()
14
    {
15
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('type_label...ime($model->time); })); (array<string,array>) is incompatible with the return type of the parent method hipanel\grid\GridView::defaultColumns of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
16
            'type_label' => [
17
                'label' => Yii::t('hipanel', 'Type'),
18
                'format' => 'html',
19 View Code Duplication
                'value' => function ($model) {
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...
20
                    static $colors = [
21
                        'correction' => 'normal',
22
                        'exchange' => 'warning',
23
                        'deposit' => 'success',
24
                    ];
25
                    $color = $colors[$model->type] ?: 'muted';
26
27
                    return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]);
28
                },
29
            ],
30
            'sum' => [
31
                'class' => CurrencyColumn::class,
32
                'attribute' => 'sum',
33
                'colors' => ['danger' => 'warning'],
34
                'headerOptions' => ['class' => 'text-right'],
35
                'contentOptions' => function ($model) {
36
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
37
                },
38
            ],
39
            'label' => [
40
                'attribute' => 'label',
41
                'format' => 'raw',
42
                'value' => function ($model) {
43
                    if ($model->name) {
44
                        $descr = Yii::t('hipanel', ucfirst($model->class)) . ' ' . Html::tag('b', $model->name);
45
                    } elseif ($model->label) {
46
                        $descr = $model->label;
47
                    }
48
                    $text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr;
0 ignored issues
show
Bug introduced by
The variable $descr 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
                    return $text;
51
                },
52
            ],
53
            'quantity' => [
54
                'attribute' => 'quantity',
55
                'format' => 'html',
56
                'value' => function ($model) {
57
                    return BillGridView::billQuantity($model);
58
                }
59
            ],
60
            'time' => [
61
                'format' => 'html',
62
                'filter' => false,
63
                'enableSorting' => false,
64
                'contentOptions' => ['class' => 'text-nowrap'],
65 View Code Duplication
                'value' => function ($model) {
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...
66
                    list($date, $time) = explode(' ', $model->time, 2);
67
68
                    if (in_array($model->type, [
69
                        'discount', 'domain', 'monthly', 'overuse', 'premium_package',
70
                        'feature', 'intercept', 'periodic',
71
                    ], true)) {
72
                        return Yii::$app->formatter->asDate($date, 'LLLL y');
73
                    }
74
75
                    return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time);
76
                },
77
            ],
78
        ];
79
    }
80
}
81