Completed
Push — add/cli-install ( 37af96...631b7a )
by
unknown
75:35 queued 66:03
created

PHP_Autoloader::load_class()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class handles management of the actual PHP autoloader.
6
 */
7
class PHP_Autoloader {
8
9
	/**
10
	 * Registers the autoloader with PHP so that it can begin autoloading classes.
11
	 *
12
	 * @param Version_Loader $version_loader The class loader to use in the autoloader.
13
	 */
14
	public function register_autoloader( $version_loader ) {
15
		// Make sure no other autoloaders are registered.
16
		$this->unregister_autoloader();
17
18
		// Set the global so that it can be used to load classes.
19
		global $jetpack_autoloader_loader;
20
		$jetpack_autoloader_loader = $version_loader;
21
22
		// Ensure that the autoloader is first to avoid contention with others.
23
		spl_autoload_register( array( self::class, 'load_class' ), true, true );
24
	}
25
26
	/**
27
	 * Unregisters the active autoloader so that it will no longer autoload classes.
28
	 */
29
	public function unregister_autoloader() {
30
		// Remove any v2 autoloader that we've already registered.
31
		$autoload_chain = spl_autoload_functions();
32
		foreach ( $autoload_chain as $autoloader ) {
33
			// We can identify a v2 autoloader using the namespace.
34
			$namespace_check = null;
0 ignored issues
show
Unused Code introduced by
$namespace_check is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
36
			// Functions are recorded as strings.
37
			if ( is_string( $autoloader ) ) {
38
				$namespace_check = $autoloader;
39
			} elseif ( is_array( $autoloader ) && is_string( $autoloader[0] ) ) {
40
				// Static method calls have the class as the first array element.
41
				$namespace_check = $autoloader[0];
42
			} else {
43
				// Since the autoloader has only ever been a function or a static method we don't currently need to check anything else.
44
				continue;
45
			}
46
47
			// Check for the namespace without the generated suffix.
48
			if ( 'Automattic\\Jetpack\\Autoloader\\jp' === substr( $namespace_check, 0, 32 ) ) {
49
				spl_autoload_unregister( $autoloader );
50
			}
51
		}
52
53
		// Clear the global now that the autoloader has been unregistered.
54
		global $jetpack_autoloader_loader;
55
		$jetpack_autoloader_loader = null;
56
	}
57
58
	/**
59
	 * Loads a class file if one could be found.
60
	 *
61
	 * Note: This function is static so that the autoloader can be easily unregistered. If
62
	 * it was a class method we would have to unwrap the object to check the namespace.
63
	 *
64
	 * @param string $class_name The name of the class to autoload.
65
	 *
66
	 * @return bool Indicates whether or not a class file was loaded.
67
	 */
68 View Code Duplication
	public static function load_class( $class_name ) {
69
		global $jetpack_autoloader_loader;
70
		if ( ! isset( $jetpack_autoloader_loader ) ) {
71
			return;
72
		}
73
74
		$file = $jetpack_autoloader_loader->find_class_file( $class_name );
75
		if ( ! isset( $file ) ) {
76
			return false;
77
		}
78
79
		require $file;
80
		return true;
81
	}
82
}
83