Completed
Push — master ( 17eff7...565461 )
by Fumio
02:34
created

AliasResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 6.5 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
dl 8
loc 123
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 2

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