AuthItemSearch   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 10.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 8 1
A attributeLabels() 10 10 1
B search() 0 32 6

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
namespace yii2mod\rbac\models\search;
4
5
use dosamigos\arrayquery\ArrayQuery;
6
use Yii;
7
use yii\base\Model;
8
use yii\data\ArrayDataProvider;
9
use yii\rbac\Item;
10
11
/**
12
 * Class AuthItemSearch
13
 *
14
 * @package yii2mod\rbac\models\search
15
 */
16
class AuthItemSearch extends Model
17
{
18
    /**
19
     * @var string auth item name
20
     */
21
    public $name;
22
23
    /**
24
     * @var int auth item type
25
     */
26
    public $type;
27
28
    /**
29
     * @var string auth item description
30
     */
31
    public $description;
32
33
    /**
34
     * @var string auth item rule name
35
     */
36
    public $ruleName;
37
38
    /**
39
     * @var int the default page size
40
     */
41
    public $pageSize = 25;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function rules(): array
47
    {
48
        return [
49
            [['name', 'ruleName', 'description'], 'trim'],
50
            [['type'], 'integer'],
51
            [['name', 'ruleName', 'description'], 'safe'],
52
        ];
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 View Code Duplication
    public function attributeLabels(): array
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...
59
    {
60
        return [
61
            'name' => Yii::t('yii2mod.rbac', 'Name'),
62
            'type' => Yii::t('yii2mod.rbac', 'Type'),
63
            'description' => Yii::t('yii2mod.rbac', 'Description'),
64
            'rule' => Yii::t('yii2mod.rbac', 'Rule'),
65
            'data' => Yii::t('yii2mod.rbac', 'Data'),
66
        ];
67
    }
68
69
    /**
70
     * Creates data provider instance with search query applied
71
     *
72
     * @param array $params
73
     *
74
     * @return \yii\data\ArrayDataProvider
75
     */
76
    public function search(array $params): ArrayDataProvider
77
    {
78
        $authManager = Yii::$app->getAuthManager();
79
80
        if ($this->type == Item::TYPE_ROLE) {
81
            $items = $authManager->getRoles();
82
        } else {
83
            $items = array_filter($authManager->getPermissions(), function ($item) {
84
                return strpos($item->name, '/') !== 0;
85
            });
86
        }
87
88
        $query = new ArrayQuery($items);
89
90
        $this->load($params);
91
92
        if ($this->validate()) {
93
            $query->addCondition('name', $this->name ? "~{$this->name}" : null)
94
                ->addCondition('ruleName', $this->ruleName ? "~{$this->ruleName}" : null)
95
                ->addCondition('description', $this->description ? "~{$this->description}" : null);
96
        }
97
98
        return new ArrayDataProvider([
99
            'allModels' => $query->find(),
100
            'sort' => [
101
                'attributes' => ['name'],
102
            ],
103
            'pagination' => [
104
                'pageSize' => $this->pageSize,
105
            ],
106
        ]);
107
    }
108
}
109