ScriptHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 9.84 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 13
c 3
b 1
f 1
lcom 0
cbo 10
dl 6
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRootDir() 0 8 2
C runEvents() 6 41 11

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 Taisiya\CoreBundle\Composer;
4
5
use Composer\Composer;
6
use Composer\EventDispatcher\Event;
7
use Doctrine\Common\Inflector\Inflector;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Taisiya\CoreBundle\App;
10
use Taisiya\CoreBundle\Event\Composer\CommandEvent;
11
use Taisiya\CoreBundle\Event\Composer\InstallerEvent;
12
use Taisiya\CoreBundle\Event\Composer\PackageEvent;
13
use Taisiya\CoreBundle\Event\Composer\PluginEvent;
14
15
class ScriptHandler
16
{
17
    /**
18
     * @param Composer $composer
19
     *
20
     * @return string
21
     */
22
    final protected static function getRootDir(Composer $composer): string
23
    {
24
        if (!defined('TAISIYA_ROOT')) {
25
            define('TAISIYA_ROOT', dirname($composer->getConfig()->get('vendor-dir')));
26
        }
27
28
        return TAISIYA_ROOT;
29
    }
30
31
    /**
32
     * @param Event $event
33
     */
34
    final public static function runEvents(Event $event): void
35
    {
36
        $rootDir = self::getRootDir($event->getComposer());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Composer\EventDispatcher\Event as the method getComposer() does only exist in the following sub-classes of Composer\EventDispatcher\Event: Composer\Installer\InstallerEvent, Composer\Installer\PackageEvent, Composer\Script\CommandEvent, Composer\Script\Event, Composer\Script\PackageEvent. 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
        $app = file_exists($rootDir.'/bootstrap.php')
39
            ? require_once $rootDir.'/bootstrap.php' :
40
            new App(['settings' => require_once $rootDir.'/app/config/settings.php']);
41
42
        /** @var EventDispatcher $dispatcher */
43
        $dispatcher = $app->getContainer()['event_dispatcher'];
44
45
        foreach (require_once $rootDir.'/var/cache/internal/events_subscribers.cache.php' as $subscriberClass) {
46
            $dispatcher->addSubscriber(new $subscriberClass());
47
        }
48
49
        if (preg_match('/-cmd$/', $event->getName())) {
50
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\CommandEvent\\'.Inflector::classify($event->getName()).'Event';
51 View Code Duplication
        } elseif (preg_match('/-dependencies-solving$/', $event->getName())) {
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...
52
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\InstallerEvent\\'.Inflector::classify($event->getName()).'Event';
53
        } elseif (preg_match('/-package-/', $event->getName())) {
54
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PackageEvent\\'.Inflector::classify($event->getName()).'Event';
55 View Code Duplication
        } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) {
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...
56
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PluginEvent\\'.Inflector::classify($event->getName()).'Event';
57
        }
58
        $detailedEvent = new $detailedEventClass($app);
0 ignored issues
show
Bug introduced by
The variable $detailedEventClass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
59
        $dispatcher->dispatch($detailedEvent::NAME, $detailedEvent);
60
61
        if (preg_match('/-cmd$/', $event->getName())) {
62
            $commandEvent = new CommandEvent($app);
63
            $dispatcher->dispatch($commandEvent::NAME, $commandEvent);
64
        } elseif (preg_match('/-dependencies-solving$/', $event->getName())) {
65
            $installerEvent = new InstallerEvent($app);
66
            $dispatcher->dispatch($installerEvent::NAME, $installerEvent);
67
        } elseif (preg_match('/-package-/', $event->getName())) {
68
            $packageEvent = new PackageEvent($app);
69
            $dispatcher->dispatch($packageEvent::NAME, $packageEvent);
70
        } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) {
71
            $pluginEvent = new PluginEvent($app);
72
            $dispatcher->dispatch($pluginEvent::NAME, $pluginEvent);
73
        }
74
    }
75
}
76