Completed
Push — master ( d5222d...8300db )
by Fumio
02:37
created

ClassLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 10.13 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 93.1%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 10
c 3
b 1
f 1
lcom 2
cbo 1
dl 8
loc 79
ccs 27
cts 29
cp 0.931
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 8 8 1
A unregister() 0 6 2
A __construct() 0 5 1
B load() 0 31 6

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 Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment;
6
7
class ClassLoader
8
{
9
    /**
10
     * @var static
11
     */
12
    protected static $instance;
13
14
    /**
15
     * @param array $addons
16
     */
17 2 View Code Duplication
    public static function register(AddonEnvironment $env, $addons)
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...
18
    {
19 2
        static::$instance = new static($env, $addons);
20
21
        // TODO check addon configuration
22
23 2
        spl_autoload_register([static::$instance, 'load'], true, false);
24 2
    }
25
26
    /**
27
     */
28 1
    public static function unregister()
29
    {
30 1
        if (static::$instance) {
31 1
            spl_autoload_unregister([static::$instance, 'load']);
32 1
        }
33 1
    }
34
35
    protected $env;
36
37
    protected $addons;
38
39
    /**
40
     * @param Jumilla\Addomnipot\Laravel\Environment $env
41
     * @param array $addons
42
     */
43 3
    public function __construct(AddonEnvironment $env, array $addons)
44
    {
45 3
        $this->env = $env;
46 3
        $this->addons = $addons;
47 3
    }
48
49
    /**
50
     * @param string $className
51
     *
52
     * @return bool
53
     */
54 1
    public function load($className)
55
    {
56 1
        foreach ($this->addons as $addon) {
57 1
            $namespace = $addon->phpNamespace();
58
59 1
            $namespacePrefix = $namespace ? $namespace.'\\' : '';
60
61
            // アドオンの名前空間下のクラスでないなら
62 1
            if (!starts_with($className, $namespacePrefix)) {
63 1
                continue;
64
            }
65
66
            // 名前空間を削る
67 1
            $relativeClassName = substr($className, strlen($namespacePrefix));
68
69
            // クラスの相対パスを作成する(PSR-4)
70 1
            $relativePath = $this->env->classToPath($relativeClassName);
71
72
            // 全ディレクトリ下を探索する (PSR-4)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73 1
            foreach ($addon->config('addon.directories') as $directory) {
74 1
                $path = $addon->path($directory.'/'.$relativePath);
75 1
                if (file_exists($path)) {
76
                    require_once $path;
77
78
                    return true;
79
                }
80 1
            }
81 1
        }
82
83 1
        return false;
84
    }
85
}
86