Completed
Push — master ( f02869...8251a6 )
by Nicolas
03:31
created

ModuleMigrator::fire()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php namespace Modules\Core\Console\Installers\Scripts;
2
3
use Illuminate\Console\Command;
4
use Modules\Core\Console\Installers\SetupScript;
5
6
class ModuleMigrator implements SetupScript
7
{
8
    /**
9
     * @var array
10
     */
11
    protected $modules = [
12
        'Setting',
13
        'Menu',
14
        'Media',
15
        'Page',
16
        'Dashboard',
17
        'Translation',
18
    ];
19
20
    /**
21
     * Fire the install script
22
     * @param  Command $command
23
     * @return mixed
24
     */
25
    public function fire(Command $command)
26
    {
27
        if ($command->option('verbose')) {
28
            $command->blockMessage('Migrations', 'Starting the module migrations ...', '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...
29
        }
30
31
        foreach ($this->modules as $module) {
32
            if ($command->option('verbose')) {
33
                $command->call('module:migrate', ['module' => $module]);
34
                continue;
35
            }
36
            $command->callSilent('module:migrate', ['module' => $module]);
37
        }
38
    }
39
}
40