AliasResolver   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 2
dl 0
loc 129
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A __construct() 0 5 1
A getAppNamespace() 0 4 1
A unregister() 0 6 2
A makeAppAddon() 0 8 1
C load() 0 46 8
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Container\Container;
7
8
class AliasResolver
9
{
10
    /**
11
     * @var static
12
     */
13
    protected static $instance;
14
15
    /**
16
     * @param string $apppath
17
     * @param array $addons
18
     * @param array $aliases
19
     */
20 2
    public static function register($apppath, array $addons, array $aliases)
21
    {
22 2
        static::$instance = new static($apppath, $addons, $aliases);
23
24 2
        spl_autoload_register([static::$instance, 'load'], true, false);
25 2
    }
26
27
    /**
28
     */
29 1
    public static function unregister()
30
    {
31 1
        if (static::$instance) {
32 1
            spl_autoload_unregister([static::$instance, 'load']);
33
        }
34 1
    }
35
36
    /**
37
     * @var array
38
     */
39
    protected $addons;
40
41
    /**
42
     * @var array
43
     */
44
    protected $globalClassAliases;
45
46
    /**
47
     * The constructor.
48
     *
49
     * @param string $apppath
50
     * @param array $addons
51
     * @param array $aliases
52
     */
53 3
    public function __construct($apppath, array $addons, array $aliases)
54
    {
55 3
        $this->addons = array_merge([$this->makeAppAddon($apppath)], $addons);
56 3
        $this->globalClassAliases = $aliases;
57 3
    }
58
59
    /**
60
     * Make addon instance for application namespace.
61
     *
62
     * @param string $path
63
     *
64
     * @return static
65
     */
66 3
    protected function makeAppAddon($path)
67
    {
68
        $config = [
69 3
            'namespace' => trim($this->getAppNamespace(), '\\'),
70
        ];
71
72 3
        return new Addon(null, 'app', $path, $config);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Illuminate\Contra...Foundation\Application>.

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...
73
    }
74
75
    /**
76
     * @param string $className
77
     *
78
     * @return bool
79
     */
80 1
    public function load($className)
81
    {
82 1
        foreach ($this->addons as $addon) {
83 1
            $namespace = $addon->phpNamespace();
84
85
            // 名前空間のないパッケージはエイリアス解決をする必要がない
86 1
            if (empty($namespace)) {
87 1
                continue;
88
            }
89
90 1
            $namespacePrefix = $namespace.'\\';
91 1
            $includesGlobalAliases = $addon->config('addon.includes_global_aliases', true);
92 1
            $addonAliases = $addon->config('addon.aliases', []);
93
94
            // アドオンの名前空間下のクラスでないなら
95 1
            if (!starts_with($className, $namespacePrefix)) {
96 1
                continue;
97
            }
98
99
            // クラス名を取り出す
100 1
            $parts = explode('\\', $className);
101 1
            $relativeClassName = $parts[count($parts) - 1];
102
103
            // グローバルなエイリアスかチェックする
104 1
            if ($includesGlobalAliases) {
105 1
                if (isset($this->globalClassAliases[$relativeClassName])) {
106 1
                    $originalClassName = $this->globalClassAliases[$relativeClassName];
107 1
                    class_alias($originalClassName, $className);
108
109 1
                    return true;
110
                }
111
            }
112
113
            // パッケージ固有のエイリアスかチェックする
114 1
            if ($addonAliases) {
115 1
                if (isset($addonAliases[$relativeClassName])) {
116 1
                    $originalClassName = $addonAliases[$relativeClassName];
117 1
                    class_alias($originalClassName, $className);
118
119 1
                    return true;
120
                }
121
            }
122
        }
123
124 1
        return false;
125
    }
126
127
    /**
128
     * Get the application namespace.
129
     *
130
     * @return string
131
     */
132 3
    protected function getAppNamespace()
133
    {
134 3
        return Container::getInstance()->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Container\Container>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
    }
136
}
137