Completed
Push — master ( b21a89...27844f )
by Andrii
02:23
created

SetterTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 137
Duplicated Lines 10.22 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 71.15%

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 2
cbo 2
dl 14
loc 137
ccs 37
cts 52
cp 0.7115
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setPermission() 0 10 3
A setRole() 0 10 3
B setChild() 14 22 6
A setAssignment() 0 18 4
A findItem() 0 12 3
A setAssignments() 0 9 3
A getAllAssignments() 0 4 1
A getAllItems() 0 4 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
 * 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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
use Yii;
14
use yii\base\InvalidParamException;
15
use yii\rbac\Assignment;
16
use yii\rbac\Item;
17
18
/**
19
 * Smart setters for AuthManager.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
trait SetterTrait
24
{
25
    /**
26
     * Set permission.
27
     * @param string $name
28
     * @param string $description
29
     * @return Item
30
     */
31 11
    public function setPermission($name, $description = null)
32
    {
33 11
        $permission = $this->getPermission($name) ?: $this->createPermission($name);
0 ignored issues
show
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34 11
        if ($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35
            $permission->description = $description;
36
        }
37 11
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
39 11
        return $permission;
40
    }
41
42
    /**
43
     * Set role.
44
     * @param string $name
45
     * @param string $description
46
     * @return Item
47
     */
48 10
    public function setRole($name, $description = null)
49
    {
50 10
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51 10
        if ($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $role->description = $description;
53
        }
54 10
        $this->add($role);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
56 10
        return $role;
57
    }
58
59
    /**
60
     * Set child.
61
     * @param string|Item $parent
62
     * @param string|Item $child
63
     * @return bool
64
     */
65 10
    public function setChild($parent, $child)
66
    {
67 10 View Code Duplication
        if (is_string($parent)) {
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...
68 10
            $name   = $parent;
69 10
            $parent = $this->getItem($parent);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70 10
            if (is_null($parent)) {
71
                throw new InvalidParamException("Unknown parent:$name at setChild");
72
            }
73
        }
74 10 View Code Duplication
        if (is_string($child)) {
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...
75
            $name  = $child;
76
            $child = $this->getItem($child);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
            if (is_null($child)) {
78
                throw new InvalidParamException("Unknown child:$name at setChild");
79
            }
80
        }
81 10
        if (isset($this->children[$parent->name][$child->name])) {
0 ignored issues
show
Bug introduced by
The property children does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
            return false;
83
        }
84
85 10
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
86
    }
87
88
    /**
89
     * Assigns an item (role or permission) to a user.
90
     * @param string|Item $item
91
     * @param string|integer $userId the user ID (see [[\yii\web\User::id]])
92
     * @throws \Exception when given wrong item name
93
     * @return Assignment|null the assignment object or `null` when assignment was not found by name
94
     */
95 20
    public function setAssignment($item, $userId)
96
    {
97
        try {
98 20
            if (is_string($item)) {
99 20
                $item = $this->findItem($item);
100
            }
101
        } catch (InvalidParamException $e) {
102
            Yii::warning('Role or permission "' . $item . '" does not exist');
103
104
            return null;
105
        }
106
107 20
        if (isset($this->assignments[$userId][$item->name])) {
108
            return $this->assignments[$userId][$item->name];
0 ignored issues
show
Bug introduced by
The property assignments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
        }
110
111 20
        return $this->assign($item, $userId);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on hipanel\rbac\SetterTrait. Did you maybe mean setAssignment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
    }
113
114 20
    protected function findItem($name, $description = null)
115
    {
116 20
        $item = $this->getItem($name);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
117 20
        if ($item) {
118 20
            return $item;
119
        }
120 2
        if (strncmp($name, 'deny:', 5) === 0) {
121 2
            return $this->setPermission($name, $description);
122
        }
123
124
        throw new InvalidParamException("Unknown item:$name at findItem");
125
    }
126
127
    /**
128
     * Assigns items to a user.
129
     * @param string|array $items
130
     * @param string|integer $userId
131
     */
132 10
    public function setAssignments($items, $userId)
133
    {
134 10
        if (is_string($items)) {
135 10
            $items = explode(',', $items);
136
        }
137 10
        foreach ($items as $item) {
138 10
            $this->setAssignment($item, $userId);
139
        }
140 10
    }
141
142
    /**
143
     * Returns all assignments in the system.
144
     * @return array
145
     */
146
    public function getAllAssignments()
147
    {
148
        return $this->assignments;
149
    }
150
151
    /**
152
     * Returns all items in the system.
153
     * @return array
154
     */
155 20
    public function getAllItems()
156
    {
157 20
        return $this->items;
0 ignored issues
show
Bug introduced by
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
158
    }
159
}
160