Completed
Branch newinternal (ffe884)
by Simon
04:07
created

AutoLoader::load()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0018
Metric Value
dl 0
loc 34
ccs 23
cts 24
cp 0.9583
rs 8.439
cc 5
eloc 19
nc 10
nop 1
crap 5.0018
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca;
10
11
/**
12
 * AutoLoader for the new classes
13
 */
14
class AutoLoader
15
{
16 9
	public static function load($class)
17
	{
18 9
		global $filepath;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
19
20
		// handle namespaces sensibly
21 9
		if (strpos($class, "Waca") !== false) {
22
			// strip off the initial namespace
23 9
			$class = str_replace("Waca\\", "", $class);
24
25
			// swap backslashes for forward slashes to map to directory names
26 9
			$class = str_replace("\\", "/", $class);
27 9
		}
28
29 1
		$paths = array(
30 9
			$filepath . 'includes/' . $class . ".php",
31 9
			$filepath . 'includes/DataObjects/' . $class . ".php",
32 9
			$filepath . 'includes/Providers/' . $class . ".php",
33 9
			$filepath . 'includes/Providers/Interfaces/' . $class . ".php",
34 9
			$filepath . 'includes/Validation/' . $class . ".php",
35 9
			$filepath . 'includes/Helpers/' . $class . ".php",
36 9
			$filepath . 'includes/Helpers/Interfaces/' . $class . ".php",
37 9
			$filepath . $class . ".php",
38 9
		);
39
40 9
		foreach ($paths as $file) {
41 9
			if (file_exists($file)) {
42 9
				require_once($file);
43 9
			}
44
45 9
			if (class_exists($class)) {
46
				return;
47
			}
48 9
		}
49 9
	}
50
}
51