Passed
Push — master ( d1bf2b...e3ab8d )
by Andrii
01:41
created

SetterTrait::warning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * RBAC implementation for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-rbac
6
 * @package   hipanel-rbac
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
use yii\base\InvalidConfigException;
14
use yii\rbac\Assignment;
15
use yii\rbac\Item;
16
17
/**
18
 * Smart setters for AuthManager.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
trait SetterTrait
23
{
24
    /**
25
     * Set permission.
26
     * @param string $name
27
     * @param string $description
28
     * @return Item
29
     */
30
    public function setPermission($name, $description = null)
31 13
    {
32
        $permission = $this->getPermission($name) ?: $this->createPermission($name);
0 ignored issues
show
Bug introduced by
It seems like createPermission() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $permission = $this->getPermission($name) ?: $this->/** @scrutinizer ignore-call */ createPermission($name);
Loading history...
Bug introduced by
It seems like getPermission() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $permission = $this->/** @scrutinizer ignore-call */ getPermission($name) ?: $this->createPermission($name);
Loading history...
33 13
        if ($description) {
34 13
            $permission->description = $description;
35
        }
36
        $this->add($permission);
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->/** @scrutinizer ignore-call */ 
37
               add($permission);
Loading history...
37 13
38
        return $permission;
39 13
    }
40
41
    /**
42
     * Set role.
43
     * @param string $name
44
     * @param string $description
45
     * @return Item
46
     */
47
    public function setRole($name, $description = null)
48 13
    {
49
        $role = $this->getRole($name) ?: $this->createRole($name);
0 ignored issues
show
Bug introduced by
It seems like getRole() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $role = $this->/** @scrutinizer ignore-call */ getRole($name) ?: $this->createRole($name);
Loading history...
Bug introduced by
It seems like createRole() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $role = $this->getRole($name) ?: $this->/** @scrutinizer ignore-call */ createRole($name);
Loading history...
50 13
        if ($description) {
51 13
            $role->description = $description;
52
        }
53
        $this->add($role);
54 13
55
        return $role;
56 13
    }
57
58
    /**
59
     * Set child.
60
     * @param string|Item $parent
61
     * @param string|Item $child
62
     * @return bool
63
     */
64
    public function setChild($parent, $child)
65 13
    {
66
        if (is_string($parent)) {
67 13
            $name   = $parent;
68 13
            $parent = $this->getItem($parent);
0 ignored issues
show
Bug introduced by
The method getItem() does not exist on hipanel\rbac\SetterTrait. Did you maybe mean getAllItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            /** @scrutinizer ignore-call */ 
69
            $parent = $this->getItem($parent);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69 13
            if (is_null($parent)) {
70 13
                throw new InvalidConfigException("Unknown parent:$name at setChild");
71
            }
72
        }
73
        if (is_string($child)) {
74 13
            $name  = $child;
75
            $child = $this->getItem($child);
76
            if (is_null($child)) {
77
                throw new InvalidConfigException("Unknown child:$name at setChild");
78
            }
79
        }
80
        if (isset($this->children[$parent->name][$child->name])) {
81 13
            return false;
82
        }
83
84
        return $this->addChild($parent, $child);
0 ignored issues
show
Bug introduced by
It seems like addChild() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        return $this->/** @scrutinizer ignore-call */ addChild($parent, $child);
Loading history...
85 13
    }
86
87
    /**
88
     * Assigns an item (role or permission) to a user.
89
     * @param string|Item $item
90
     * @param string|integer $userId the user ID (see [[\yii\web\User::id]])
91
     * @throws \Exception when given wrong item name
92
     * @return Assignment|null the assignment object or `null` when assignment was not found by name
93
     */
94
    public function setAssignment($item, $userId)
95 26
    {
96
        try {
97
            if (is_string($item)) {
98 26
                $item = $this->findItem($item);
99 26
            }
100
        } catch (InvalidConfigException $e) {
101
            $this->warning('Role or permission "' . $item . '" does not exist');
102
103
            return null;
104
        }
105
106
        if (isset($this->assignments[$userId][$item->name])) {
107 26
            return $this->assignments[$userId][$item->name];
108
        }
109
110
        return $this->assign($item, $userId);
0 ignored issues
show
Bug introduced by
It seems like assign() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        return $this->/** @scrutinizer ignore-call */ assign($item, $userId);
Loading history...
111 26
    }
112
113
    protected function findItem($name, $description = null)
114 26
    {
115
        $item = $this->getItem($name);
116 26
        if ($item) {
117 26
            return $item;
118 26
        }
119
        if (strncmp($name, 'deny:', 5) === 0) {
120
            return $this->setPermission($name, $description);
121
        }
122
123
        throw new InvalidConfigException("Unknown item:$name at findItem");
124
    }
125
126
    /**
127
     * Assigns items to a user.
128
     * @param string|array $items
129
     * @param string|integer $userId
130
     */
131
    public function setAssignments($items, $userId)
132 10
    {
133
        if (is_string($items)) {
134 10
            $items = explode(',', $items);
135 10
        }
136
        foreach ($items as $item) {
137 10
            $this->setAssignment($item, $userId);
138 10
        }
139
    }
140 10
141
    /**
142
     * Returns all assignments in the system.
143
     * @return array
144
     */
145
    public function getAllAssignments()
146
    {
147
        return $this->assignments;
148
    }
149
150
    /**
151
     * Returns all items in the system.
152
     * @return array
153
     */
154
    public function getAllItems()
155 26
    {
156
        return $this->items;
157 26
    }
158
159
    protected function warning(string $message)
160
    {
161
        return class_exists('Yii') ? \Yii::warning($message) : \yii\helpers\Yii::warning($message);
0 ignored issues
show
Bug introduced by
The type yii\helpers\Yii was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
Are you sure the usage of Yii::warning($message) targeting yii\BaseYii::warning() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
162
    }
163
}
164