ConfigureUserProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fire() 0 6 1
A configure() 0 6 1
A factory() 0 6 1
1
<?php namespace Modules\Core\Console\Installers\Scripts;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Foundation\Application;
5
use Modules\Core\Console\Installers\SetupScript;
6
7
class ConfigureUserProvider implements SetupScript
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $drivers = [
13
        'Sentinel',
14
    ];
15
16
    /**
17
     * @var
18
     */
19
    private $application;
20
21
    /**
22
     * @param Application $application
23
     */
24
    public function __construct(Application $application)
25
    {
26
        $this->application = $application;
27
    }
28
29
    /**
30
     * Fire the install script
31
     * @param  Command $command
32
     * @return mixed
33
     */
34
    public function fire(Command $command)
35
    {
36
        $command->blockMessage('User Module', 'Starting the User Module setup...', 'comment');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Console\Command as the method blockMessage() does only exist in the following sub-classes of Illuminate\Console\Command: Modules\Core\Console\InstallCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
37
38
        $this->configure('Sentinel', $command);
39
    }
40
41
    /**
42
     * @param $driver
43
     * @param $command
44
     * @return mixed
45
     */
46
    protected function configure($driver, $command)
47
    {
48
        $driver = $this->factory($driver);
49
50
        return $driver->fire($command);
51
    }
52
53
    /**
54
     * @param $driver
55
     * @return mixed
56
     */
57
    protected function factory($driver)
58
    {
59
        $class = __NAMESPACE__ . "\\UserProviders\\{$driver}Installer";
60
61
        return $this->application->make($class);
62
    }
63
}
64