Completed
Push — master ( ba702a...fd24a7 )
by Mostafa Abd El-Salam
10:50 queued 06:35
created

PermissionDirectives   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 40.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 33
loc 82
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A roleDirective() 0 12 1
A hasroleDirective() 11 11 1
A hasanyroleDirective() 11 11 1
A hasallrolesDirective() 11 11 1
A extractRoleGuard() 0 6 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 Maklad\Permission\Directives;
4
5
use Illuminate\View\Compilers\BladeCompiler;
6
7
/**
8
 * Class PermissionDirectives
9
 * @package Maklad\Permission\Directives
10
 */
11
class PermissionDirectives
12
{
13
    private $bladeCompiler;
14
15 18
    public function __construct(BladeCompiler $bladeCompiler)
16
    {
17 18
        $this->bladeCompiler = $bladeCompiler;
18 18
    }
19
20
    /**
21
     * Declare role directive
22
     */
23
    public function roleDirective()
24
    {
25 18
        $this->bladeCompiler->directive('role', function ($arguments) {
26 5
            list($role, $guard) = $this->extractRoleGuard($arguments);
27
28 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
29 18
        });
30
31 18
        $this->bladeCompiler->directive('endrole', function () {
32 5
            return '<?php endif; ?>';
33 18
        });
34 18
    }
35
36
    /**
37
     * Declare hasrole directive
38
     */
39 View Code Duplication
    public function hasroleDirective()
0 ignored issues
show
Duplication introduced by
This method 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...
40
    {
41 18
        $this->bladeCompiler->directive('hasrole', function ($arguments) {
42 5
            list($role, $guard) = $this->extractRoleGuard($arguments);
43
44 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
45 18
        });
46 18
        $this->bladeCompiler->directive('endhasrole', function () {
47 5
            return '<?php endif; ?>';
48 18
        });
49 18
    }
50
51
    /**
52
     * Declare hasanyrole directive
53
     */
54 View Code Duplication
    public function hasanyroleDirective()
0 ignored issues
show
Duplication introduced by
This method 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...
55
    {
56 18
        $this->bladeCompiler->directive('hasanyrole', function ($arguments) {
57 8
            list($roles, $guard) = $this->extractRoleGuard($arguments);
58
59 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
60 18
        });
61 18
        $this->bladeCompiler->directive('endhasanyrole', function () {
62 8
            return '<?php endif; ?>';
63 18
        });
64 18
    }
65
66
    /**
67
     * Declare hasallroles directive
68
     */
69 View Code Duplication
    public function hasallrolesDirective()
0 ignored issues
show
Duplication introduced by
This method 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...
70
    {
71 18
        $this->bladeCompiler->directive('hasallroles', function ($arguments) {
72 8
            list($roles, $guard) = $this->extractRoleGuard($arguments);
73
74 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
75 18
        });
76 18
        $this->bladeCompiler->directive('endhasallroles', function () {
77 8
            return '<?php endif; ?>';
78 18
        });
79 18
    }
80
81
    /**
82
     * @param $arguments
83
     *
84
     * @return array
85
     */
86 17
    private function extractRoleGuard($arguments): array
87
    {
88 17
        $arguments = preg_replace('(\(|\)| )', '', $arguments);
89
90 17
        return \explode(',', $arguments . ',');
91
    }
92
}
93