Test Failed
Push — master ( b8acae...88c84e )
by Vitaliy
19:04 queued 11s
created

Config::getComponents()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 3
nop 0
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * @author  Vitaliy IIIFX Khomenko (c) 2021
4
 * @license MIT
5
 *
6
 * @link    https://github.com/iiifx-production/yii2-autocomplete-helper
7
 */
8
9
namespace iiifx\Yii2\Autocomplete;
10
11
use Closure;
12
use Throwable;
13
use Yii;
14
use yii\base\BaseObject;
15
use yii\helpers\FileHelper;
16
17
class Config extends BaseObject
18
{
19
    public array $files = [];
20
    protected array $_config;
21
    protected array $_components;
22
23
    /**
24
     * @return mixed[]
25
     */
26
    public function getComponents(): array
27
    {
28
        if ($this->_components === null) {
29
            $this->_components = [];
30
31
            if ($config = $this->readConfig()) {
32
                foreach ($this->files as $current) {
33
                    if (isset($config[$current]['components'])) {
34
                        /** @var mixed[] $components */
35
                        $components = $config[$current]['components'];
36
37
                        if (is_array($components)) {
38
                            foreach ($components as $name => $component) {
39
                                if (($class = $this->findClass($component)) !== false) {
40
                                    $this->_components[$name][$class] = $class;
41
                                }
42
                            }
43
                        }
44
                    }
45
                }
46
            }
47
        }
48
49
        return $this->_components;
50
    }
51
52
    /**
53
     * @return mixed[]
54
     */
55
    protected function readConfig(): array
56
    {
57
        if ($this->_config === null) {
58
            $this->_config = [];
59
60
            foreach ($this->files as $file) {
61
                $path = Yii::getAlias($file);
62
                $path = FileHelper::normalizePath($path);
63
64
                if (is_file($path)) {
65
                    try {
66
                        /** @noinspection PhpIncludeInspection */
67
                        $this->_config[$file] = require $path;
68
                    } catch (Throwable) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')', expecting '|' or T_VARIABLE on line 68 at column 38
Loading history...
69
                        # Ignore
70
                    }
71
                }
72
            }
73
        }
74
75
        return $this->_config;
76
    }
77
78
    /**
79
     * @param mixed $section
80
     *
81
     * @return string|false
82
     */
83
    protected function findClass(mixed $section): bool|string
84
    {
85
        try {
86
            if ($section instanceof Closure) {
87
                return get_class($section());
88
            }
89
90
            if (is_object($section)) {
91
                return get_class($section);
92
            }
93
94
            if (is_string($section)) {
95
                return $section;
96
            }
97
98
            if (is_array($section) && isset($section['class'])) {
99
                return $section['class'];
100
            }
101
        } catch (Throwable) {
102
            # Ignore
103
        }
104
105
        return false;
106
    }
107
}
108