Passed
Push — master ( da6314...127f02 )
by Lee
04:23 queued 11s
created

Roles::hasRole()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 4
nop 2
crap 4
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Casbin\Rbac;
6
7
class Roles
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Roles
Loading history...
8
{
9
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
10
     * @var array
11
     */
12
    public $roles = [];
13
14
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
15
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
16
     * @param mixed  $matchingFunc
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
17
     *
18
     * @return bool
19
     */
20 90
    public function hasRole(string $name, $matchingFunc): bool
21
    {
22 90
        if ($matchingFunc instanceof \Closure) {
23 12
            foreach ($this->roles as $key => $value) {
24 12
                if ($matchingFunc($name, (string) $key)) {
25 12
                    return true;
26
                }
27
            }
28
        } else {
29 81
            return isset($this->roles[$name]);
30
        }
31
32 3
        return false;
33
    }
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     * @param mixed  $matchingFunc
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
38
     *
39
     * @return Role
40
     */
41 72
    public function createRole(string $name, $matchingFunc): Role
42
    {
43 72
        if (!isset($this->roles[$name])) {
44 18
            $this->roles[$name] = new Role($name);
45
        }
46
47 72
        if ($matchingFunc instanceof \Closure) {
48 12
            foreach ($this->roles as $key => $value) {
49 12
                if ($matchingFunc($name, (string) $key) && $name !== (string) $key) {
50 12
                    if (!isset($this->roles[$key])) {
51
                        $this->roles[$key] = new Role($key);
52
                    }
53 12
                    $this->roles[$name]->addRole($this->roles[$key]);
54
55 12
                    break;
56
                }
57
            }
58
        }
59
60 72
        if (!isset($this->roles[$name])) {
61
            $this->roles[$name] = new Role($name);
62
        }
63
64 72
        return $this->roles[$name];
65
    }
66
}
67