Completed
Push — master ( 612575...486a28 )
by Paweł
03:08
created

CodeReviewAutoloader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 9.4285
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
/**
3
 * Very simple class autoloader for code_review plugin
4
 */
5
class CodeReviewAutoloader {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
7
	private $classMap = array();
8
9 1
	public function __construct($basePath = null) {
10 1
		if ($basePath === null) {
11 1
			$basePath = dirname(__FILE__);
12 1
		}
13 1
		$this->registerDirectory($basePath);
14 1
	}
15
16
	/**
17
	 * Not fully PSR-0 compatible, but good enough for this particular plugin
18
	 *
19
	 * @param string $basePath
20
	 * @param string $prefix
21
	 */
22 1
	private function registerDirectory($basePath, $prefix = '') {
23 1
		$basePath = str_replace('\\', '/', $basePath);
24 1
		$basePath = rtrim($basePath, '/') . '/';
25 1
		$prefix = ($prefix ? $prefix . '_' : '' );
26 1
		$files = scandir($basePath);
27 1
		foreach ($files as $file) {
28 1
			if ($file[0] == '.') {
29 1
				continue;
30
			}
31 1
			$path = $basePath . $file;
32 1
			if (is_dir($path)) {
33 1
				$this->registerDirectory($path, $prefix . pathinfo($path, PATHINFO_FILENAME));
34 1
			} elseif (strtolower(pathinfo($path, PATHINFO_EXTENSION)) == 'php') {
35 1
				$name = $prefix . pathinfo($path, PATHINFO_FILENAME);
36 1
				$this->classMap[$name] = $path;
37 1
			}
38 1
		}
39 1
	}
40
41
	/**
42
	 * @param string $className
43
	 * @return bool
44
	 */
45 7
	public function load($className) {
46 7
		if (isset($this->classMap[$className]) && file_exists($this->classMap[$className])) {
47 6
			return include($this->classMap[$className]);
48
		}
49 3
		return false;
50
	}
51
52
	/**
53
	 * @return bool
54
	 */
55 1
	public function register() {
56 1
		return spl_autoload_register(array($this, 'load'));
57
	}
58
59
	/**
60
	 * @return bool
61
	 */
62 1
	public function unregister() {
63 1
		return spl_autoload_unregister(array($this, 'load'));
64
	}
65
66
}