EventSubscriber   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A registerComposer() 0 16 2
1
<?php
2
3
namespace N98\Magento\Command\ComposerWrapper;
4
5
use Composer\Factory;
6
use Composer\IO\ConsoleIO;
7
use Symfony\Component\Console\Event\ConsoleEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class EventSubscriber implements EventSubscriberInterface
11
{
12
    /**
13
     * Returns an array of event names this subscriber wants to listen to.
14
     *
15
     * @return array The event names to listen to
16
     *
17
     * @api
18
     */
19
    public static function getSubscribedEvents()
20
    {
21
        return array('console.command' => 'registerComposer');
22
    }
23
24
    /**
25
     * @param ConsoleEvent $event
26
     */
27
    public function registerComposer(ConsoleEvent $event)
28
    {
29
        /*
30
         * Inject composer object in composer commands
31
         */
32
        $command = $event->getCommand();
33
        if (strstr(get_class($command), 'Composer\\Command\\')) {
34
            $io = new ConsoleIO($event->getInput(), $event->getOutput(), $command->getHelperSet());
0 ignored issues
show
Bug introduced by
It seems like $command->getHelperSet() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
35
            $magentoRootFolder = $command->getApplication()->getMagentoRootFolder();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getMagentoRootFolder() does only exist in the following sub-classes of Symfony\Component\Console\Application: N98\Magento\Application. 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...
36
            $configFile = $magentoRootFolder . '/composer.json';
37
            $composer = Factory::create($io, $configFile);
38
            \chdir($magentoRootFolder);
39
            $command->setComposer($composer);
40
            $command->setIO($io);
41
        }
42
    }
43
}
44