Autoloader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 42
ccs 0
cts 17
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B doAutoload() 0 25 3
1
<?php
2
namespace Wonnova\SDK;
3
4
/**
5
 * Class Autoloader
6
 * @author
7
 * @link
8
 */
9
class Autoloader
10
{
11
    /**
12
     * Registers a new autoloader which is compatible with this component
13
     */
14
    public static function register()
15
    {
16
        spl_autoload_register([__CLASS__, 'doAutoload']);
17
    }
18
19
    /**
20
     * Performs the autoloading of classes in this component by using psr-4 standard
21
     *
22
     * @param $class
23
     * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
24
     */
25
    public static function doAutoload($class)
26
    {
27
        // base directory for the namespace prefix
28
        $base_dir = __DIR__ . '/../';
29
30
        // does the class use the namespace prefix?
31
        $len = strlen(__NAMESPACE__);
32
        if (strncmp(__NAMESPACE__, $class, $len) !== 0) {
33
            // no, move to the next registered autoloader
34
            return;
35
        }
36
37
        // get the relative class name
38
        $relative_class = substr($class, $len);
39
40
        // replace the namespace prefix with the base directory, replace namespace
41
        // separators with directory separators in the relative class name, append
42
        // with .php
43
        $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
44
45
        // if the file exists, require it
46
        if (file_exists($file)) {
47
            require $file;
48
        }
49
    }
50
}
51