Completed
Pull Request — master (#1)
by Andrii
05:21
created

SetterTrait::setRole()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3.2098
1
<?php
2
3
/*
4
 * RBAC implementation for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-rbac
7
 * @package   hipanel-rbac
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\rbac;
13
14
use yii\base\InvalidParamException;
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 5
    public function setPermission($name, $description = null)
31
    {
32 5
        $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...
33 5
        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...
34
            $permission->description = $description;
35
        }
36 5
        $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...
37
38 5
        return $permission;
39
    }
40
41
    /**
42
     * Set role.
43
     * @param string $name
44
     * @param string $description
45
     * @return Item
46
     */
47 5
    public function setRole($name, $description = null)
48
    {
49 5
        $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...
50 5
        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...
51
            $role->description = $description;
52
        }
53 5
        $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...
54
55 5
        return $role;
56
    }
57
58
    /**
59
     * Set child.
60
     * @param string|Item $parent
61
     * @param string|Item $child
62
     * @return bool
63
     */
64 5
    public function setChild($parent, $child)
65
    {
66 5 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...
67 5
            $name   = $parent;
68 5
            $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...
69 5
            if (is_null($parent)) {
70
                throw new InvalidParamException("Unknown parent:$name at setChild");
71
            }
72 5
        }
73 5 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...
74 5
            $name  = $child;
75 5
            $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...
76 5
            if (is_null($child)) {
77
                throw new InvalidParamException("Unknown child:$name at setChild");
78
            }
79 5
        }
80 5
        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...
81
            return false;
82
        }
83
84 5
        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...
85
    }
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 the assignment object
93
     */
94 10
    public function setAssignment($item, $userId)
95
    {
96 10 View Code Duplication
        if (is_string($item)) {
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...
97 10
            $name = $item;
98 10
            $item = $this->getItem($item);
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...
99 10
            if (is_null($item)) {
100
                throw new InvalidParamException("Unknown item:$name at setAssignment");
101
            }
102 10
        }
103 10
        if (isset($this->assignments[$userId][$item->name])) {
104
            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...
105
        }
106
107 10
        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...
108
    }
109
110
    /**
111
     * Assigns items to a user.
112
     * @param array $items
113
     * @param string|integer $userId
114
     */
115 2
    public function setAssignments(array $items, $userId)
116
    {
117 2
        foreach ($items as $item) {
118 2
            $this->setAssignment($item, $userId);
119 2
        }
120 2
    }
121
122
    /**
123
     * Returns all assignments in the system.
124
     * @return array
125
     */
126
    public function getAllAssignments()
127
    {
128
        return $this->assignments;
129
    }
130
131
    /**
132
     * Returns all items in the system.
133
     * @return array
134
     */
135 10
    public function getAllItems()
136
    {
137 10
        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...
138
    }
139
}
140