Completed
Push — master ( b3308c...45a414 )
by Nikita
06:13
created

ScriptHandler::runEvents()   C

Complexity

Conditions 11
Paths 100

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
rs 5.2653
cc 11
eloc 29
nc 100
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Taisiya\CoreBundle\Composer;
4
5
use Composer\EventDispatcher\Event;
6
use Doctrine\Common\Inflector\Inflector;
7
use Symfony\Component\EventDispatcher\EventDispatcher;
8
use Taisiya\CoreBundle\App;
9
use Taisiya\CoreBundle\Event\Composer\CommandEvent;
10
use Taisiya\CoreBundle\Event\Composer\InstallerEvent;
11
use Taisiya\CoreBundle\Event\Composer\PackageEvent;
12
use Taisiya\CoreBundle\Event\Composer\PluginEvent;
13
14
defined('TAISIYA_ROOT') || define('TAISIYA_ROOT', dirname(dirname(__DIR__)));
15
16
class ScriptHandler
17
{
18
    const EVENT_EXECUTE = 'composer.handler.execute';
19
20
    /**
21
     * @param Event $event
22
     */
23
    final public static function runEvents(Event $event): void
24
    {
25
        $app = file_exists(TAISIYA_ROOT.'/bootstrap.php')
26
            ? require_once TAISIYA_ROOT.'/bootstrap.php' :
27
            new App(['settings' => require_once TAISIYA_ROOT.'/app/config/settings.php']);
28
29
        /** @var EventDispatcher $dispatcher */
30
        $dispatcher = $app->getContainer()['event_dispatcher'];
31
32
        foreach (require_once TAISIYA_ROOT.'/var/cache/internal/events_subscribers.cache.php' as $subscriberClass) {
33
            $dispatcher->addSubscriber(new $subscriberClass());
34
        }
35
36
        if (preg_match('/-cmd$/', $event->getName())) {
37
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\CommandEvent\\'.Inflector::classify($event->getName()).'Event';
38
        } elseif (preg_match('/-dependencies-solving$/', $event->getName())) {
39
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\InstallerEvent\\'.Inflector::classify($event->getName()).'Event';
40
        } elseif (preg_match('/-package-/', $event->getName())) {
41
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PackageEvent\\'.Inflector::classify($event->getName()).'Event';
42
        } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) {
43
            $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PluginEvent\\'.Inflector::classify($event->getName()).'Event';
44
        }
45
        $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...
46
        $dispatcher->dispatch($detailedEvent::NAME, $detailedEvent);
47
48
        if (preg_match('/-cmd$/', $event->getName())) {
49
            $commandEvent = new CommandEvent($app);
50
            $dispatcher->dispatch($commandEvent::NAME, $commandEvent);
51
        } elseif (preg_match('/-dependencies-solving$/', $event->getName())) {
52
            $installerEvent = new InstallerEvent($app);
53
            $dispatcher->dispatch($installerEvent::NAME, $installerEvent);
54
        } elseif (preg_match('/-package-/', $event->getName())) {
55
            $packageEvent = new PackageEvent($app);
56
            $dispatcher->dispatch($packageEvent::NAME, $packageEvent);
57
        } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) {
58
            $pluginEvent = new PluginEvent($app);
59
            $dispatcher->dispatch($pluginEvent::NAME, $pluginEvent);
60
        }
61
    }
62
}
63