Completed
Push — master ( d412ce...8871ca )
by Fumio
03:00
created

AliasResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 6.4 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 97.78%

Importance

Changes 0
Metric Value
dl 8
loc 125
ccs 44
cts 45
cp 0.9778
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 8 8 1
A __construct() 0 5 1
A unregister() 0 6 2
A makeAppAddon() 0 10 1
C load() 0 46 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Console\AppNamespaceDetectorTrait;
7
8
class AliasResolver
9
{
10
    use AppNamespaceDetectorTrait;
11
12
    /**
13
     * @var static
14
     */
15
    protected static $instance;
16
17
    /**
18
     * @param string $app_path
19
     * @param array $addons
20
     * @param array $aliases
21
     */
22 2 View Code Duplication
    public static function register($app_path, array $addons, array $aliases)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24 2
        static::$instance = new static($app_path, $addons, $aliases);
25
26
        // TODO check addon configuration
27
28 2
        spl_autoload_register([static::$instance, 'load'], true, false);
29 2
    }
30
31
    /**
32
     */
33 2
    public static function unregister()
34
    {
35 2
        if (static::$instance) {
36 2
            spl_autoload_unregister([static::$instance, 'load']);
37 2
        }
38 2
    }
39
40
    /**
41
     * @var array
42
     */
43
    protected $addons;
44
45
    /**
46
     * @var array
47
     */
48
    protected $globalClassAliases;
49
50
    /**
51
     * The constructor.
52
     *
53
     * @param string $app_path
54
     * @param array $addons
55
     * @param array $aliases
56
     */
57 3
    public function __construct($app_path, array $addons, array $aliases)
58
    {
59 3
        $this->addons = array_merge([$this->makeAppAddon($app_path)], $addons);
60 3
        $this->globalClassAliases = $aliases;
61 3
    }
62
63
    /**
64
     * Make addon instance for application namespace.
65
     *
66
     * @param string $path
67
     *
68
     * @return static
69
     */
70 3
    protected function makeAppAddon($path)
71
    {
72 3
        $config = new Repository([
73
            'addon' => [
74 3
                'namespace' => trim($this->getAppNamespace(), '\\'),
75 3
            ],
76 3
        ]);
77
78 3
        return new Addon('app', $path, $config);
79
    }
80
81
    /**
82
     * @param string $className
83
     *
84
     * @return bool
85
     */
86 1
    public function load($className)
87
    {
88 1
        foreach ($this->addons as $addon) {
89 1
            $namespace = $addon->phpNamespace();
90
91
            // 名前空間のないパッケージはエイリアス解決をする必要がない
92 1
            if (empty($namespace)) {
93 1
                continue;
94
            }
95
96 1
            $namespacePrefix = $namespace.'\\';
97 1
            $includesGlobalAliases = $addon->config('addon.includes_global_aliases', true);
98 1
            $addonAliases = $addon->config('addon.aliases', []);
99
100
            // アドオンの名前空間下のクラスでないなら
101 1
            if (!starts_with($className, $namespacePrefix)) {
102 1
                continue;
103
            }
104
105
            // クラス名を取り出す
106 1
            $parts = explode('\\', $className);
107 1
            $relativeClassName = $parts[count($parts) - 1];
108
109
            // グローバルなエイリアスかチェックする
110 1
            if ($includesGlobalAliases) {
111 1
                if (isset($this->globalClassAliases[$relativeClassName])) {
112 1
                    $originalClassName = $this->globalClassAliases[$relativeClassName];
113 1
                    class_alias($originalClassName, $className);
114
115 1
                    return true;
116
                }
117 1
            }
118
119
            // パッケージ固有のエイリアスかチェックする
120 1
            if ($addonAliases) {
121 1
                if (isset($addonAliases[$relativeClassName])) {
122 1
                    $originalClassName = $addonAliases[$relativeClassName];
123 1
                    class_alias($originalClassName, $className);
124
125 1
                    return true;
126
                }
127
            }
128 1
        }
129
130 1
        return false;
131
    }
132
}
133