Completed
Push — master ( b97427...e235cc )
by Raffael
30:35 queued 26:08
created

ContainerBuilder::detectApps()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 29
cp 0
rs 6.9666
c 0
b 0
f 0
cc 12
nc 24
nop 2
crap 156

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
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Bootstrap;
13
14
use Composer\Autoload\ClassLoader as Composer;
15
use Micro\Container\Container;
16
use Noodlehaus\Config;
17
use Psr\Container\ContainerInterface;
18
19
class ContainerBuilder
20
{
21
    /**
22
     * Init bootstrap.
23
     */
24
    public static function get(Composer $composer): ContainerInterface
25
    {
26
        $config = self::loadConfig();
27
        $config = self::detectApps($config, $composer);
28
        $container = new Container($config);
29
30
        return $container;
31
    }
32
33
    /**
34
     * Load config.
35
     */
36
    protected static function loadConfig(): Config
37
    {
38
        $configs[] = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'.container.config.php';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$configs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
39
40
        foreach (glob(constant('BALLOON_CONFIG_DIR').DIRECTORY_SEPARATOR.'*.yaml') as $path) {
41
            clearstatcache(true, $path);
42
            $configs[] = $path;
43
        }
44
45
        return new Config($configs);
46
    }
47
48
    /**
49
     * Find apps.
50
     */
51
    protected static function detectApps(Config $master, Composer $composer): Config
52
    {
53
        $apps = [];
54
        $configs = [];
0 ignored issues
show
Unused Code introduced by
$configs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
56
        foreach (glob(constant('BALLOON_PATH').DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'*') as $app) {
57
            $name = basename($app);
58
            $ns = str_replace('.', '\\', $name).'\\';
59
            $composer->addPsr4($ns, $app);
60
61
            if (!file_exists($app.DIRECTORY_SEPARATOR.'.container.config.php')) {
62
                continue;
63
            }
64
65
            $file = $app.DIRECTORY_SEPARATOR.'.container.config.php';
66
            $load_master = isset($master['Apps'][$name]['enabled']) ? $master['Apps'][$name]['enabled'] : null;
67
68
            if ($load_master === false) {
69
                continue;
70
            }
71
72
            $app = new Config($file);
73
            $load = isset($app['Apps'][$name]['enabled']) ? $app['Apps'][$name]['enabled'] : null;
74
75
            if ($load_master === null && $load === null || $load_master === true || $load_master === null && $load === true) {
76
                $apps[] = $app;
77
            }
78
        }
79
80
        $new = new Config([]);
81
        foreach ($apps as $app) {
82
            $new->merge($app);
83
        }
84
85
        $new->merge($master);
86
87
        return $new;
88
    }
89
}
90