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

AutoLoader::autoload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 60
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 60
ccs 3
cts 7
cp 0.4286
crap 2.7462
rs 9.5555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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