Completed
Branch master (740422)
by Mathieu
02:04
created

AutoLoader::loadClass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 11
ccs 2
cts 4
cp 0.5
crap 4.125
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Suricate;
3
4
class AutoLoader
5
{
6 2
    public static function autoload($class)
7
    {
8
        $class      = str_replace('\\', DIRECTORY_SEPARATOR, $class);
9
10
        $filename   = dirname(__DIR__) . DIRECTORY_SEPARATOR . $class . '.php';
11
12
        if (is_file($filename)) {
13
            include $filename;
14
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
15
            //throw new \Exception("Object '$class' not found", 2);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
16 2
        }
17
18
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
19
        $class              = str_replace('\\', DIRECTORY_SEPARATOR, $class);
20
        $convertedClassName = preg_replace('|([a-z])([A-Z])|', '$1_$2', $class);
21
22
        $trimmedClass = ltrim($class, DIRECTORY_SEPARATOR);
23
24
        if (($sepPos = strpos($trimmedClass, DIRECTORY_SEPARATOR)) !== false) {
25
            $classRootNameSpace = substr($trimmedClass, 0, $sepPos);
26
            $convertedClassName = substr(
27
                $trimmedClass,
28
                strrpos($trimmedClass, DIRECTORY_SEPARATOR) + 1
29
            );
30
        } else {
31
            $classRootNameSpace = '';
32
        }
33
        $splitted           = explode('_', $convertedClassName);
34
        $classType          = strtolower(array_pop($splitted));
35
        $classNameSpace     = strtolower(array_shift($splitted));
36
        $dir                = '';
37
        $thisClass = str_replace(__NAMESPACE__.'\\', '', __CLASS__) . "<br/>";
38
        echo $thisClass;
39
40
        if (isset(self::$dirMap[$classType])) {
41
            $dir        = self::$dirMap[$classType];
42
            $filename   = self::$root . '/' . $dir . '/' . str_replace(ucFirst($classType), '', $class) . '.php';
43
        } elseif (isset(self::$dirMap[$classNameSpace])) {
44
            $dir        = self::$dirMap[$classNameSpace];
45
            $filename   = self::$root . '/' . $dir . '/' . $class . '.php';
46
        } elseif (isset(self::$dirMap[$classRootNameSpace])) {
47
            $dir        = self::$dirMap[$classRootNameSpace];
48
            $filename   = self::$root . '/' . $dir . '/' . str_replace(ucFirst($classRootNameSpace), '', $class) . '.php';
49
        } elseif (isset(self::$dirMap['default'])) {
50
            $dir        = self::$dirMap['default'];
51
            $filename   = self::$root . '/' . $dir . '/' . $class . '.php';
52
        }
53
54
        if ($dir != '') {
55
            if (is_file($filename)) {
56
                include $filename;
57
            } else {
58
                throw new \Exception("Object '$class' not found", 2);
59
            }
60
        } else {
61
            throw new \Exception("Unknown object type / No directory found", 1);
62
        }
63
        */
64
       
65 2
    }
66
67 2
    public static function loadClass($class)
68
    {
69
        if (!class_exists($class, false) && !interface_exists($class, false)) {
70
            self::autoload($class);
71
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
72
            if (!class_exists($class, false) && !interface_exists($class, false)) {
73
               throw new \Exception("File loaded, but '$class' does not exist");
74
            }
75
            */
76
        }
77 2
    }
78
79
    public static function register()
80
    {
81
        ini_set('unserialize_callback_func', 'spl_autoload_call');
82
        spl_autoload_register(__NAMESPACE__ .'\Autoloader::loadClass');
83
    }
84
85
    public static function unRegister()
86
    {
87
        spl_autoload_unregister(__NAMESPACE__ .'\Autoloader::loadClass');
88
    }
89
}
90