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

ModuleAssets::fire()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 1
dl 14
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 View Code Duplication
class ModuleAssets implements SetupScript
0 ignored issues
show
Duplication introduced by
This class 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...
7
{
8
    /**
9
     * @var array
10
     */
11
    protected $modules = [
12
        'Core',
13
        'Media',
14
        'Menu',
15
    ];
16
17
    /**
18
     * Fire the install script
19
     * @param  Command $command
20
     * @return mixed
21
     */
22
    public function fire(Command $command)
23
    {
24
        if ($command->option('verbose')) {
25
            $command->blockMessage('Module assets', 'Publishing module assets ...', '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...
26
        }
27
28
        foreach ($this->modules as $module) {
29
            if ($command->option('verbose')) {
30
                $command->call('module:publish', ['module' => $module]);
31
                continue;
32
            }
33
            $command->callSilent('module:publish', ['module' => $module]);
34
        }
35
    }
36
}
37