Completed
Pull Request — 1.x (#9)
by Akihito
04:16 queued 03:14
created

Boot::getContxtualModule()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 9.7
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4.0961
1
<?php
2
/**
3
 * This file is part of the BEAR.Middleware package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Middleware;
8
9
use BEAR\AppMeta\AbstractAppMeta;
10
use BEAR\Middleware\Exception\InvalidContextException;
11
use BEAR\Middleware\Module\AppMetaModule;
12
use BEAR\Middleware\Module\MiddlewareModule;
13
use BEAR\Middleware\Module\StreamModule;
14
use Ray\Compiler\DiCompiler;
15
use Ray\Compiler\Exception\NotCompiled;
16
use Ray\Compiler\ScriptInjector;
17
use Ray\Di\AbstractModule;
18
use Ray\Di\InjectorInterface;
19
20
class Boot
21
{
22 1
    public function getInjector(AbstractAppMeta $appMeta, $contexts)
23
    {
24
        try {
25 1
            $injector = (new ScriptInjector($appMeta->tmpDir))->getInstance(InjectorInterface::class);
26 1
        } catch (NotCompiled $e) {
27 1
            $module = $this->getContxtualModule($appMeta, $contexts);
28
            $module->override(new MiddlewareModule(new AppMetaModule($appMeta)));
0 ignored issues
show
Documentation introduced by
new \BEAR\Middleware\Mod...AppMetaModule($appMeta) is of type object<BEAR\Middleware\Module\AppMetaModule>, but the function expects a null|object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
new \BEAR\Middleware\Mod...ppMetaModule($appMeta)) is of type object<BEAR\Middleware\Module\MiddlewareModule>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
            $compiler = new DiCompiler(new StreamModule($module), $appMeta->tmpDir);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $this->getContxtualModule($appMeta, $contexts) on line 27 can also be of type object<Ray\Di\AbstractModule>; however, Ray\Di\AbstractModule::__construct() does only seem to accept null|object<self>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
            $compiler->compile();
31
            $injector = (new ScriptInjector($appMeta->tmpDir))->getInstance(InjectorInterface::class);
32
        }
33
34
        return $injector;
35
    }
36
37
    /**
38
     * Return configured module
39
     *
40
     * @param AbstractAppMeta $appMeta
41
     * @param string          $contexts
42
     *
43
     * @return AbstractModule
44
     */
45 1
    private function getContxtualModule(AbstractAppMeta $appMeta, $contexts)
46
    {
47 1
        $contextsArray = array_reverse(explode('-', $contexts));
48 1
        $module = null;
49 1
        foreach ($contextsArray as $context) {
50 1
            $class = $appMeta->name . '\Module\\' . ucwords($context) . 'Module';
51 1
            if (! class_exists($class)) {
52 1
                $class = 'BEAR\Package\Context\\' . ucwords($context) . 'Module';
53
            }
54 1
            if (! is_a($class, AbstractModule::class, true)) {
55 1
                throw new InvalidContextException($context);
56
            }
57
            $module = new $class($module);
58
        }
59
60
        return $module;
61
    }
62
}
63