Completed
Pull Request — master (#39)
by Mostafa Abd El-Salam
02:19
created

PermissionDirectives   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 52.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 33
loc 63
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 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
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
    public function __construct(BladeCompiler $bladeCompiler)
16
    {
17
        $this->bladeCompiler = $bladeCompiler;
18
    }
19
20
    public function roleDirective()
21
    {
22
        $this->bladeCompiler->directive('role', function ($arguments) {
23
            list($role, $guard) = $this->extractRoleGuard($arguments);
24
25
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
26
        });
27
28
        $this->bladeCompiler->directive('endrole', function () {
29
            return '<?php endif; ?>';
30
        });
31
    }
32
33 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...
34
    {
35
        $this->bladeCompiler->directive('hasrole', function ($arguments) {
36
            list($role, $guard) = $this->extractRoleGuard($arguments);
37
38
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
39
        });
40
        $this->bladeCompiler->directive('endhasrole', function () {
41
            return '<?php endif; ?>';
42
        });
43
    }
44
45 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...
46
    {
47
        $this->bladeCompiler->directive('hasanyrole', function ($arguments) {
48
            list($roles, $guard) = $this->extractRoleGuard($arguments);
49
50
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
51
        });
52
        $this->bladeCompiler->directive('endhasanyrole', function () {
53
            return '<?php endif; ?>';
54
        });
55
    }
56
57 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...
58
    {
59
        $this->bladeCompiler->directive('hasallroles', function ($arguments) {
60
            list($roles, $guard) = $this->extractRoleGuard($arguments);
61
62
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
63
        });
64
        $this->bladeCompiler->directive('endhasallroles', function () {
65
            return '<?php endif; ?>';
66
        });
67
    }
68
69
    private function extractRoleGuard($arguments){
70
        $arguments = preg_replace('(\(|\)| )', '', $arguments);
71
        return \explode(',', $arguments . ',');
72
    }
73
}