Completed
Push — master ( e6d5b1...3d0fa3 )
by Dmitry
03:47
created

Bootstrap::run()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 8.439
c 4
b 0
f 0
cc 5
eloc 15
nc 9
nop 4
crap 30
1
<?php
2
3
namespace Basis\Jobs\Module;
4
5
use Basis\Converter;
6
use Basis\Filesystem;
7
use Basis\Service;
8
use Basis\Runner;
9
use Exception;
10
use ReflectionClass;
11
use ReflectionProperty;
12
13
class Bootstrap
14
{
15
    public function run(Runner $runner, Service $service, Filesystem $fs, Converter $converter)
16
    {
17
        $runner->dispatch('tarantool.migrate');
18
19
        $meta = $runner->dispatch('module.meta');
20
        foreach ($meta['jobs'] as $job) {
21
            $class = new ReflectionClass($runner->getJobClass($job));
22
            $params = [];
23
            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
24
                $params[] = $property->getName();
25
            }
26
            $service->registerJob($job, $params);
27
        }
28
29
        foreach ($fs->listClasses('Listeners') as $class) {
30
            $chain = str_replace('\\', '.', substr($class, 10));
31
32
            foreach ($chain as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $chain of type string is not traversable.
Loading history...
33
                $chain[$k] = $converter->toCamelCase($v);
34
            }
35
            $event = implode('.', $event);
0 ignored issues
show
Bug introduced by
The variable $event 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...
36
37
            $service->subscribe($event);
38
        }
39
    }
40
}
41