Detector   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 28
c 3
b 2
f 0
dl 0
loc 51
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 9 2
A getApplication() 0 7 2
A getConfig() 0 7 2
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 Yii;
12
use yii\base\Application;
13
use yii\base\BaseObject;
14
15
class Detector extends BaseObject
16
{
17
    public ?Application $app = null;
18
    public array $ids = [
19
        'basic-console' => 'basic',
20
        'app-console' => 'advanced',
21
    ];
22
    public array $configs = [
23
        'basic' => [
24
            '@app/config/console.php',
25
            '@app/config/web.php',
26
        ],
27
        'advanced' => [
28
            '@common/config/main.php',
29
            '@common/config/main-local.php',
30
            '@console/config/main.php',
31
            '@console/config/main-local.php',
32
            '@backend/config/main.php',
33
            '@backend/config/main-local.php',
34
            '@frontend/config/main.php',
35
            '@frontend/config/main-local.php',
36
        ],
37
    ];
38
39
    public function detect(): bool|string
40
    {
41
        $application = $this->getApplication();
42
43
        if (isset($this->ids[$application->id])) {
44
            return $this->ids[$application->id];
45
        }
46
47
        return false;
48
    }
49
50
    public function getConfig(): array
51
    {
52
        if ($type = $this->detect()) {
53
            return $this->configs[$type] ?? [];
54
        }
55
56
        return [];
57
    }
58
59
    protected function getApplication(): Application
60
    {
61
        if (!$this->app instanceof Application) {
62
            $this->app = Yii::$app;
63
        }
64
65
        return $this->app;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->app could return the type null which is incompatible with the type-hinted return yii\base\Application. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
}
68