Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

autoloader.php ➔ autoLoad()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 37
rs 5.1612

How to fix   Complexity   

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
spl_autoload_extensions('.php');
3
spl_autoload_register("autoloader");
4
5
$rootPath = str_replace('\\', '/', realpath(str_replace('\\', '/', dirname(__FILE__)) . '/../../') . '/');
6
7
/**
8
 * The function to be registered as the default autoload functino
9
 * for loading classes
10
 *
11
 * @param $class
12
 *
13
 * @throws \Exception
14
 */
15
function autoloader($class) {
16
	autoLoad($class);	
17
}
18
19
/**
20
 * The decoupled autoload function for actually loading the classes
21
 *
22
 * @param      $class
23
 * @param bool $throwException
24
 *
25
 * @return bool
26
 * @throws \Exception
27
 */
28
function autoLoad($class, $throwException = true)
29
{
30
	if (class_exists($class, false)) {
31
		return true;
32
	}
33
	
34
	global $rootPath;
35
	$file = $rootPath . str_replace('\\', '/', $class) . ".php";
36
	$debug_backtrace = debug_backtrace();
37
	
38
	if (file_exists($file)) {
39
		require_once($file);
40
		if ($throwException) {
41
			if (class_exists($class, false) === false && interface_exists($class, false) === false) {
42
				throw new \Exception('Could not load class "' . $class . '" in file ' . $file);
43
			} else {
44
				return true;
45
			}
46
		}
47
		return class_exists($class, false) || interface_exists($class, false);
48
	} else {
49
		if (isset($debug_backtrace[2]) && isset($debug_backtrace[2]['file']) && isset($debug_backtrace[2]['line'])) {			
50
			if ($throwException) {
51
				errorHandler(0, 'Could not load class \'' . $class . '\' in file ' . $rootPath . $file, $debug_backtrace[2]['file'], $debug_backtrace[2]['line']);
52
			} else {
53
				return false;
54
			}			
55
		} else {
56
			if ($throwException) {
57
				throw new \Exception('Could not load class "' . $class . '" in file ' . $file . "\n" . 'Called from unknown origin.');
58
			} else {
59
				return false;
60
			}
61
		}
62
	}
63
	return false;
64
}