DummyUserServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 59.46 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B boot() 22 33 5

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 Longman\LaravelDummyUser;
4
5
use Illuminate\Auth\AuthManager;
6
use Illuminate\Auth\Events\Login;
7
use Illuminate\Auth\Events\Logout;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use Illuminate\Contracts\Cache\Repository as CacheContract;
10
use Illuminate\Contracts\Foundation\Application;
11
use Illuminate\Support\ServiceProvider;
12
13
class DummyUserServiceProvider extends ServiceProvider
14
{
15
    public function boot(AuthManager $auth)
16
    {
17
        $auth->provider('dummy', function (Application $app, array $config) {
18
            /** @var \Illuminate\Contracts\Cache\Repository $cache */
19
            $cache = $app->make(CacheContract::class);
20
21 View Code Duplication
            $this->app['events']->listen(Login::class, function ($event) use ($config, $cache) {
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...
22
                if (! $event->user instanceof Authenticatable) {
23
                    return false;
24
                }
25
26
                $id = $event->user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable 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...
27
                if (! $id) {
28
                    return false;
29
                }
30
                $cache->put('users.' . $id, $event->user, $config['lifetime']);
31
            });
32
33 View Code Duplication
            $this->app['events']->listen(Logout::class, function ($event) use ($config, $cache) {
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...
34
                if (! $event->user instanceof Authenticatable) {
35
                    return false;
36
                }
37
38
                $id = $event->user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable 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...
39
                if (! $id) {
40
                    return false;
41
                }
42
                $cache->forget('users.' . $id);
43
            });
44
45
            return new DummyUserProvider($cache, $config);
46
        });
47
    }
48
49
}
50