Passed
Push — master ( 53c725...a8eb1b )
by Petr
07:45
created

ALoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 36
ccs 4
cts 16
cp 0.25
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A load() 0 13 3
1
<?php
2
3
namespace kalanis\kw_modules\Loaders\Kw;
4
5
6
use kalanis\kw_autoload\AutoloadException;
0 ignored issues
show
Bug introduced by
The type kalanis\kw_autoload\AutoloadException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use kalanis\kw_modules\Interfaces\ILoader;
8
use kalanis\kw_modules\Interfaces\IMdTranslations;
9
use kalanis\kw_modules\Interfaces\IModule;
10
use kalanis\kw_modules\ModuleException;
11
use kalanis\kw_modules\Traits\TMdLang;
12
13
14
/**
15
 * Class AKwLoader
16
 * @package kalanis\kw_modules\Loaders\Kw
17
 * Load modules data from defined targets
18
 * @codeCoverageIgnore contains external autoloader
19
 *
20
 * Name is passed as first big and the rest little ( ucfirst(strtolower($x)) )
21
 * - lookup by curly braces
22
 *
23
 * KWCMS is for kw_autoloader on "vendor" position and modules dir on "project" position
24
 */
25
abstract class ALoader implements ILoader
26
{
27
    use TMdLang;
28
29 1
    public function __construct(?IMdTranslations $lang = null)
30
    {
31 1
        if (class_exists('\kalanis\kw_autoload\Autoload')) {
32
            \kalanis\kw_autoload\Autoload::addPath('%2$s%1$smodules%1$s%5$s%1$sphp-src%1$s%6$s');
0 ignored issues
show
Bug introduced by
The type kalanis\kw_autoload\Autoload was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
            \kalanis\kw_autoload\Autoload::addPath('%2$s%1$smodules%1$s%5$s%1$ssrc%1$s%6$s');
34
            \kalanis\kw_autoload\Autoload::addPath('%2$s%1$smodules%1$s%5$s%1$s%6$s');
35
        }
36 1
        $this->setMdLang($lang);
37 1
    }
38
39
    public function load(array $module, array $constructParams = []): ?IModule
40
    {
41
        /** @var class-string<IModule> $classPath */
42
        $classPath = $this->getClassName($module);
43
        try {
44
            $reflection = new \ReflectionClass($classPath);
45
            $module = $reflection->newInstanceArgs($constructParams);
46
            if (!$module instanceof IModule) {
47
                throw new ModuleException($this->getMdLang()->mdNotInstanceOfIModule($classPath));
48
            }
49
            return $module;
50
        } catch (AutoloadException | \ReflectionException $ex) {
51
            return null;
52
        }
53
    }
54
55
    /**
56
     * @param string[] $module
57
     * @throws ModuleException
58
     * @return string
59
     */
60
    abstract protected function getClassName(array $module): string;
61
}
62