Completed
Push — master ( 3f24f8...42984c )
by Andrey
01:17
created

Permission   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 3
dl 143
loc 143
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 8 8 1
A rules() 14 14 1
A getCurrentChildren() 6 6 1
A getNewChildren() 4 4 2
A createItem() 4 4 1
A createChild() 4 4 1
A getOldName() 4 4 2
A getItem() 4 4 1
A setItemForInstance() 5 5 1
A setChildrenForInstance() 5 5 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
namespace Itstructure\RbacModule\models;
4
5
use yii\helpers\ArrayHelper;
6
use yii\rbac\{Item, Permission as BasePermission, ManagerInterface};
7
use Itstructure\RbacModule\interfaces\ModelInterface;
8
use Itstructure\RbacModule\Module;
9
10
/**
11
 * Class Permission
12
 *
13
 * @property BasePermission $permission
14
 * @property array $permissions
15
 * @property ManagerInterface $authManager
16
 *
17
 * @package Itstructure\RbacModule\models
18
 */
19 View Code Duplication
class Permission extends Rbac implements ModelInterface
0 ignored issues
show
Duplication introduced by
This class 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...
20
{
21
    /**
22
     * Permission object.
23
     *
24
     * @var BasePermission
25
     */
26
    public $permission;
27
28
    /**
29
     * Child permissions.
30
     *
31
     * @var array
32
     */
33
    public $permissions = [];
34
35
    /**
36
     * Validate rules.
37
     *
38
     * @return array
39
     */
40
    public function rules()
41
    {
42
        return ArrayHelper::merge(
43
            parent::rules(),
44
            [
45
                [
46
                    [
47
                        'permissions',
48
                    ],
49
                    'safe',
50
                ],
51
            ]
52
        );
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function attributeLabels()
59
    {
60
        return [
61
            'name' => Module::t('permissions', 'Name'),
62
            'description' => Module::t('permissions', 'Description'),
63
            'permissions' => Module::t('permissions', 'Permissions'),
64
        ];
65
    }
66
67
    /**
68
     * Get current child permissions assigned to current permission.
69
     *
70
     * @return \yii\rbac\Permission[]
71
     */
72
    protected function getCurrentChildren(): array
73
    {
74
        $permissions = $this->authManager->getChildren($this->getOldName());
75
76
        return array_keys($permissions);
77
    }
78
79
    /**
80
     * Get new child items.
81
     *
82
     * @return array
83
     */
84
    protected function getNewChildren(): array
85
    {
86
        return empty($this->permissions) ? [] : $this->permissions;
87
    }
88
89
    /**
90
     * Create new Permission.
91
     *
92
     * @param string $name
93
     *
94
     * @return Item
95
     */
96
    protected function createItem(string $name): Item
97
    {
98
        return $this->authManager->createPermission($name);
99
    }
100
101
    /**
102
     * Create self child permission.
103
     *
104
     * @param string $name
105
     *
106
     * @return Item
107
     */
108
    protected function createChild(string $name): Item
109
    {
110
        return $this->authManager->createPermission($name);
111
    }
112
113
    /**
114
     * Returns old name in current object, which is set before new value.
115
     *
116
     * @return mixed
117
     */
118
    protected function getOldName()
119
    {
120
        return null === $this->permission ? null : $this->permission->name;
121
    }
122
123
    /**
124
     * Returns Permission object.
125
     *
126
     * @param string $key
127
     *
128
     * @return Item|null
129
     */
130
    protected function getItem(string $key)
131
    {
132
        return $this->authManager->getPermission($key);
133
    }
134
135
    /**
136
     * Set permission field for child instance of Rbac - Permission.
137
     *
138
     * @param Rbac $instance
139
     * @param Item      $item
140
     *
141
     * @return Rbac
142
     */
143
    protected function setItemForInstance(Rbac $instance, Item $item): Rbac
144
    {
145
        $instance->permission = $item;
0 ignored issues
show
Documentation introduced by
The property permission does not exist on object<Itstructure\RbacModule\models\Rbac>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
        return $instance;
147
    }
148
149
    /**
150
     * Set children permissions for instance of Role.
151
     *
152
     * @param Rbac $instance
153
     *
154
     * @return Rbac
155
     */
156
    protected function setChildrenForInstance(Rbac $instance): Rbac
157
    {
158
        $instance->permissions = $instance->getCurrentChildren();
0 ignored issues
show
Documentation introduced by
The property permissions does not exist on object<Itstructure\RbacModule\models\Rbac>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
        return $instance;
160
    }
161
}
162