Module::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Package\Exception\InvalidContextException;
9
use BEAR\Package\Module\AppMetaModule;
10
use BEAR\Package\Module\CacheNamespaceModule;
11
use Ray\Di\AbstractModule;
12
use Ray\Di\AssistedModule;
13
14
use function array_reverse;
15
use function class_exists;
16
use function explode;
17
use function is_a;
18
use function is_subclass_of;
19
use function ucwords;
20
21
class Module
22
{
23
    /**
24
     * Return module from $appMeta and $context
25
     */
26
    public function __invoke(AbstractAppMeta $appMeta, string $context, string $cacheNamespace = ''): AbstractModule
27
    {
28
        $contextsArray = array_reverse(explode('-', $context));
29
        $module = new AssistedModule();
30
        foreach ($contextsArray as $contextItem) {
31
            $module = $this->installContextModule($appMeta, $contextItem, $module);
32
        }
33
34
        $module->override(new AppMetaModule($appMeta));
0 ignored issues
show
Documentation introduced by
new \BEAR\Package\Module\AppMetaModule($appMeta) is of type object<BEAR\Package\Module\AppMetaModule>, 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...
35
        $module->override(new CacheNamespaceModule($cacheNamespace));
0 ignored issues
show
Documentation introduced by
new \BEAR\Package\Module...Module($cacheNamespace) is of type object<BEAR\Package\Module\CacheNamespaceModule>, 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...
36
37
        return $module;
38
    }
39
40
    private function installContextModule(AbstractAppMeta $appMeta, string $contextItem, AbstractModule $module): AbstractModule
41
    {
42
        $class = $appMeta->name . '\Module\\' . ucwords($contextItem) . 'Module';
43
        if (! class_exists($class)) {
44
            $class = 'BEAR\Package\Context\\' . ucwords($contextItem) . 'Module';
45
        }
46
47
        if (! is_a($class, AbstractModule::class, true)) {
48
            throw new InvalidContextException($contextItem);
49
        }
50
51
        /** @psalm-suppress UnsafeInstantiation */
52
        $module = is_subclass_of($class, AbstractAppModule::class) ? new $class($appMeta, $module) : new $class($module);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \BEAR\Package\AbstractAppModule::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
53
54
        return $module;
55
    }
56
}
57