Completed
Push — master ( ddac6b...cf6d46 )
by Kenji
03:11
created

autoloader.php ➔ autoload()   D

Complexity

Conditions 15
Paths 44

Size

Total Lines 108
Code Lines 75

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 15
eloc 75
nc 44
nop 1
dl 0
loc 108
rs 4.9121

How to fix   Long Method    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
3
// This is from: https://github.com/bcit-ci/CodeIgniter/blob/develop/tests/mocks/autoloader.php
4
5
// This autoloader provide convinient way to working with mock object
6
// make the test looks natural. This autoloader support cascade file loading as well
7
// within mocks directory.
8
//
9
// Prototype :
10
//
11
// $mock_table = new Mock_Libraries_Table(); 			// Will load ./mocks/libraries/table.php
12
// $mock_database_driver = new Mock_Database_Driver();	// Will load ./mocks/database/driver.php
13
// and so on...
14
function autoload($class)
15
{
16
	$dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
17
18
	$ci_core = array(
19
		'Benchmark',
20
		'Config',
21
		'Controller',
22
		'Exceptions',
23
		'Hooks',
24
		'Input',
25
		'Lang',
26
		'Loader',
27
		'Log',
28
		'Model',
29
		'Output',
30
		'Router',
31
		'Security',
32
		'URI',
33
		'Utf8'
34
	);
35
36
	$ci_libraries = array(
37
		'Calendar',
38
		'Cart',
39
		'Driver_Library',
40
		'Email',
41
		'Encrypt',
42
		'Encryption',
43
		'Form_validation',
44
		'Ftp',
45
		'Image_lib',
46
		'Javascript',
47
		'Migration',
48
		'Pagination',
49
		'Parser',
50
		'Profiler',
51
		'Table',
52
		'Trackback',
53
	   	'Typography',
54
		'Unit_test',
55
		'Upload',
56
	   	'User_agent',
57
		'Xmlrpc',
58
		'Zip'
59
	);
60
61
	$ci_drivers = array('Session', 'Cache');
62
63
	if (strpos($class, 'Mock_') === 0)
64
	{
65
		$class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class));
66
	}
67
	elseif (strpos($class, 'CI_') === 0)
68
	{
69
		$subclass = substr($class, 3);
70
71
		if (in_array($subclass, $ci_core))
72
		{
73
			$dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR;
74
			$class = $subclass;
75
		}
76
		elseif (in_array($subclass, $ci_libraries))
77
		{
78
			$dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR;
79
			$class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
80
		}
81
		elseif (in_array($subclass, $ci_drivers))
82
		{
83
			$dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
84
			$class = $subclass;
85
		}
86
		elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
87
			$dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
88
			$class = $subclass;
89
		}
90
		elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4)
91
		{
92
			$driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
93
			$dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR;
94
			$file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php';
95
		}
96
		elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
97
		{
98
			$driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
99
			$dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
100
			$file = $dir.$m[1].'_'.$m[2].'.php';
101
		}
102
		elseif (strpos($class, 'CI_DB') === 0)
103
		{
104
			$dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR;
105
			$file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
106
		}
107
		else
108
		{
109
			$class = strtolower($class);
110
		}
111
	}
112
113
	$file = isset($file) ? $file : $dir.$class.'.php';
114
115
	if ( ! file_exists($file))
116
	{
117
		return FALSE;
118
	}
119
120
	include_once($file);
121
}
122