Completed
Push — master ( d0e0e5...0154ba )
by Andrii
05:43 queued 03:12
created

ChargeController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 32.35 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 11
loc 34
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 11 11 1
A actions() 0 13 1

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
3
/**
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\controllers;
13
14
use hipanel\actions\IndexAction;
15
use hipanel\base\CrudController;
16
use hipanel\filters\EasyAccessControl;
17
use hipanel\modules\finance\models\query\ChargeQuery;
18
use yii\base\Event;
19
20
/**
21
 * Class ChargeController
22
 * @package hipanel\modules\finance\controllers
23
 */
24
class ChargeController extends CrudController
25
{
26
    /**
27
     * @inheritDoc
28
     */
29 View Code Duplication
    public function behaviors()
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...
30
    {
31
        return array_merge(parent::behaviors(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...bill.charges.read')))); (array<*,array>) is incompatible with the return type of the parent method hipanel\base\Controller::behaviors of type 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...
32
            'access-bill' => [
33
                'class' => EasyAccessControl::class,
34
                'actions' => [
35
                    '*' => 'bill.charges.read',
36
                ],
37
            ],
38
        ]);
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function actions()
45
    {
46
        return array_merge(parent::actions(), [
47
            'index' => [
48
                'class' => IndexAction::class,
49
                'on beforePerform' => function (Event $event) {
50
                    /** @var ChargeQuery $query */
51
                    $query = $event->sender->getDataProvider()->query;
52
                    $query->withCommonObject()->withLatestCommonObject();
53
                },
54
            ],
55
        ]);
56
    }
57
}
58