Passed
Pull Request — master (#39)
by Mostafa Abd El-Salam
04:01
created

PermissionDirectives   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 79.03 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 49
loc 62
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A role_directive() 13 13 1
A hasrole_directive() 12 12 1
A hasanyrole_directive() 12 12 1
A hasallroles_directive() 12 12 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 View Code Duplication
    public function role_directive()
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...
21
    {
22 18
        $this->bladeCompiler->directive('role', function ($arguments) {
23 5
            $arguments = preg_replace('(\(|\)| )', '', $arguments);
24 5
            list($role, $guard) = \explode(',', $arguments . ',');
25
26 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
27 18
        });
28
29 18
        $this->bladeCompiler->directive('endrole', function () {
30 5
            return '<?php endif; ?>';
31 18
        });
32 18
    }
33
34 View Code Duplication
    public function hasrole_directive()
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...
35
    {
36 18
        $this->bladeCompiler->directive('hasrole', function ($arguments) {
37 5
            $arguments = preg_replace('(\(|\)| )', '', $arguments);
38 5
            list($role, $guard) = \explode(',', $arguments . ',');
39
40 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
41 18
        });
42 18
        $this->bladeCompiler->directive('endhasrole', function () {
43 5
            return '<?php endif; ?>';
44 18
        });
45 18
    }
46
47 View Code Duplication
    public function hasanyrole_directive()
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...
48
    {
49 18
        $this->bladeCompiler->directive('hasanyrole', function ($arguments) {
50 8
            $arguments = preg_replace('(\(|\)| )', '', $arguments);
51 8
            list($roles, $guard) = \explode(',', $arguments . ',');
52
53 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
54 18
        });
55 18
        $this->bladeCompiler->directive('endhasanyrole', function () {
56 8
            return '<?php endif; ?>';
57 18
        });
58 18
    }
59
60 View Code Duplication
    public function hasallroles_directive()
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...
61
    {
62 18
        $this->bladeCompiler->directive('hasallroles', function ($arguments) {
63 8
            $arguments = preg_replace('(\(|\)| )', '', $arguments);
64 8
            list($roles, $guard) = \explode(',', $arguments . ',');
65
66 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
67 18
        });
68 18
        $this->bladeCompiler->directive('endhasallroles', function () {
69 8
            return '<?php endif; ?>';
70 18
        });
71
    }
72
}