Completed
Push — master ( ebc159...dcba67 )
by Freek
01:22
created

PermissionServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 19.3 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 22
loc 114
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B boot() 0 25 3
A register() 0 9 1
A registerModelBindings() 0 7 1
B registerBladeExtensions() 22 42 1
B convertPipeToArray() 0 24 4

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 Spatie\Permission;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Spatie\Permission\Contracts\Role as RoleContract;
8
use Spatie\Permission\Contracts\Permission as PermissionContract;
9
10
class PermissionServiceProvider extends ServiceProvider
11
{
12
    public function boot(PermissionRegistrar $permissionLoader)
13
    {
14
        $this->publishes([
15
            __DIR__.'/../config/permission.php' => $this->app->configPath().'/permission.php',
16
        ], 'config');
17
18
        if (! class_exists('CreatePermissionTables')) {
19
            $timestamp = date('Y_m_d_His', time());
20
21
            $this->publishes([
22
                __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php",
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

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...
23
            ], 'migrations');
24
        }
25
26
        if ($this->app->runningInConsole()) {
27
            $this->commands([
28
                Commands\CreateRole::class,
29
                Commands\CreatePermission::class,
30
            ]);
31
        }
32
33
        $this->registerModelBindings();
34
35
        $permissionLoader->registerPermissions();
36
    }
37
38
    public function register()
39
    {
40
        $this->mergeConfigFrom(
41
            __DIR__.'/../config/permission.php',
42
            'permission'
43
        );
44
45
        $this->registerBladeExtensions();
46
    }
47
48
    protected function registerModelBindings()
49
    {
50
        $config = $this->app->config['permission.models'];
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
52
        $this->app->bind(PermissionContract::class, $config['permission']);
53
        $this->app->bind(RoleContract::class, $config['role']);
54
    }
55
56
    protected function registerBladeExtensions()
57
    {
58
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
59 View Code Duplication
            $bladeCompiler->directive('role', function ($arguments) {
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...
60
                list($role, $guard) = explode(',', $arguments.',');
61
62
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
63
            });
64
            $bladeCompiler->directive('endrole', function () {
65
                return '<?php endif; ?>';
66
            });
67
68 View Code Duplication
            $bladeCompiler->directive('hasrole', function ($arguments) {
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...
69
                list($role, $guard) = explode(',', $arguments.',');
70
71
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
72
            });
73
            $bladeCompiler->directive('endhasrole', function () {
74
                return '<?php endif; ?>';
75
            });
76
77 View Code Duplication
            $bladeCompiler->directive('hasanyrole', function ($arguments) {
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...
78
                list($roles, $guard) = explode(',', $arguments.',');
79
                $roles = $this->convertPipeToArray($roles);
80
81
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
82
            });
83
            $bladeCompiler->directive('endhasanyrole', function () {
84
                return '<?php endif; ?>';
85
            });
86
87 View Code Duplication
            $bladeCompiler->directive('hasallroles', function ($arguments) {
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...
88
                list($roles, $guard) = explode(',', $arguments.',');
89
                $roles = $this->convertPipeToArray($roles);
90
91
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
92
            });
93
            $bladeCompiler->directive('endhasallroles', function () {
94
                return '<?php endif; ?>';
95
            });
96
        });
97
    }
98
99
    public function convertPipeToArray(string $pipeString)
100
    {
101
        $pipeString = trim($pipeString);
102
103
        if (strlen($pipeString) <= 2) {
104
            return $pipeString;
105
        }
106
107
        $quoteCharacter = substr($pipeString, 0, 1);
108
109
        if (! in_array($quoteCharacter, ["'", '"'])) {
110
            return $pipeString;
111
        }
112
113
        $endCharacter = substr($quoteCharacter, -1, 1);
114
115
        if ($quoteCharacter !== $endCharacter) {
116
            return $pipeString;
117
        }
118
119
        $roleString = '['.implode($quoteCharacter.','.$quoteCharacter, explode('|', $pipeString)).']';
120
121
        return $roleString;
122
    }
123
}
124