Completed
Push — auto-extend-with-legacy-class-... ( cd3fad )
by
unknown
08:29
created

autoload.php ➔ autoloader()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 13
nop 1
dl 0
loc 54
rs 6.8569
c 0
b 0
f 0

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 file `autoload_packages.php`was generated by automattic/jetpack-autoloader.
4
 *
5
 * From your plugin include this file with:
6
 * require_once . plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';
7
 *
8
 * @package Automattic\Jetpack\Autoloader
9
 */
10
11
// phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
12
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
13
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_ns_cFound
14
15
namespace Automattic\Jetpack\Autoloader;
16
17
if ( ! function_exists( __NAMESPACE__ . '\enqueue_package_class' ) ) {
18
	global $jetpack_packages_classes;
19
20
	if ( ! is_array( $jetpack_packages_classes ) ) {
21
		$jetpack_packages_classes = array();
22
	}
23
	/**
24
	 * Adds the version of a package to the $jetpack_packages global array so that
25
	 * the autoloader is able to find it.
26
	 *
27
	 * @param string $class_name Name of the class that you want to autoload.
28
	 * @param string $version Version of the class.
29
	 * @param string $path Absolute path to the class so that we can load it.
30
	 */
31
	function enqueue_package_class( $class_name, $version, $path ) {
32
		global $jetpack_packages_classes;
33
34
		if ( ! isset( $jetpack_packages_classes[ $class_name ] ) ) {
35
			$jetpack_packages_classes[ $class_name ] = array(
36
				'version' => $version,
37
				'path'    => $path,
38
			);
39
		}
40
		// If we have a @dev version set always use that one!
41
		if ( 'dev-' === substr( $jetpack_packages_classes[ $class_name ]['version'], 0, 4 ) ) {
42
			return;
43
		}
44
45
		// Always favour the @dev version. Since that version is the same as bleeding edge.
46
		// We need to make sure that we don't do this in production!
47
		if ( 'dev-' === substr( $version, 0, 4 ) ) {
48
			$jetpack_packages_classes[ $class_name ] = array(
49
				'version' => $version,
50
				'path'    => $path,
51
			);
52
53
			return;
54
		}
55
		// Set the latest version!
56
		if ( version_compare( $jetpack_packages_classes[ $class_name ]['version'], $version, '<' ) ) {
57
			$jetpack_packages_classes[ $class_name ] = array(
58
				'version' => $version,
59
				'path'    => $path,
60
			);
61
		}
62
	}
63
}
64
65
if ( ! function_exists( __NAMESPACE__ . '\autoloader' ) ) {
66
	/**
67
	 * Used for autoloading jetpack packages.
68
	 *
69
	 * @param string $class_name Class Name to load.
70
	 */
71
	function autoloader( $class_name ) {
72
		global $jetpack_packages_classes;
73
74
		if ( isset( $jetpack_packages_classes[ $class_name ] ) ) {
75
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
76
				// TODO ideally we shouldn't skip any of these, see: https://github.com/Automattic/jetpack/pull/12646.
77
				$ignore = in_array(
78
					$class_name,
79
					array(
80
						'Automattic\Jetpack\JITM',
81
						'Automattic\Jetpack\Connection\Manager',
82
						'Automattic\Jetpack\Connection\Manager_Interface',
83
						'Automattic\Jetpack\Connection\XMLRPC_Connector',
84
						'Jetpack_Options',
85
						'Jetpack_Signature',
86
						'Automattic\Jetpack\Sync\Main',
87
						'Automattic\Jetpack\Constants',
88
						'Automattic\Jetpack\Tracking',
89
						'Automattic\Jetpack\Plugin\Tracking',
90
					),
91
					true
92
				);
93
				if ( ! $ignore && function_exists( 'did_action' ) && ! did_action( 'plugins_loaded' ) ) {
94
					_doing_it_wrong(
95
						esc_html( $class_name ),
96
						sprintf(
97
							/* translators: %s Name of a PHP Class */
98
							esc_html__( 'Not all plugins have loaded yet but we requested the class %s', 'jetpack' ),
99
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
100
							$class_name
101
						),
102
						esc_html( $jetpack_packages_classes[ $class_name ]['version'] )
103
					);
104
				}
105
			}
106
107
			if ( file_exists( $jetpack_packages_classes[ $class_name ]['path'] ) ) {
108
				require_once $jetpack_packages_classes[ $class_name ]['path'];
109
110
				// Extend autoloaded class with legacy class, if it exists.
111
				if ( class_exists( $class_name ) && method_exists( $class_name, 'getLegacyName' ) ) {
112
					eval( 'class ' . $class_name::getLegacyName() . ' extends ' . $class_name . '{}' );
113
114
					if ( class_exists( $class_name::getLegacyName() ) ) {
115
						echo $class_name::getLegacyName() . "exists!\n<br>";
116
					}
117
				}
118
119
				return true;
120
			}
121
		}
122
123
		return false;
124
	}
125
126
	// Add the jetpack autoloader.
127
	spl_autoload_register( __NAMESPACE__ . '\autoloader' );
128
}
129