Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

Autoloader_Handler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 184
Duplicated Lines 15.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 28
loc 184
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A is_latest_autoloader() 0 19 4
A should_autoloader_reset() 0 22 5
A build_autoloader() 0 23 1
B update_autoloader_chain() 28 28 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file was automatically generated by automattic/jetpack-autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
namespace Automattic\Jetpack\Autoloader\jp95016e8b7af5cfd6a3cbf90d4433a769;
9
10
 // phpcs:ignore
11
12
/**
13
 * This class selects the package version for the autoloader.
14
 */
15
class Autoloader_Handler {
16
17
	// The name of the autoloader function registered by v1.* autoloaders.
18
	const V1_AUTOLOADER_NAME = 'Automattic\Jetpack\Autoloader\autoloader';
19
20
	/*
21
	 * The autoloader function for v2.* autoloaders is named __NAMESPACE__ . \autoloader.
22
	 * The namespace is defined in AutoloadGenerator as
23
	 * 'Automattic\Jetpack\Autoloader\jp' plus a unique suffix.
24
	 */
25
	const V2_AUTOLOADER_BASE = 'Automattic\Jetpack\Autoloader\jp';
26
27
	/**
28
	 * The current plugin path.
29
	 *
30
	 * @var string
31
	 */
32
	private $current_plugin_path;
33
34
	/**
35
	 * The paths for all of the active plugins.
36
	 *
37
	 * @var array
38
	 */
39
	private $active_plugin_paths;
40
41
	/**
42
	 * The Autoloader_Locator object.
43
	 *
44
	 * @var Autoloader_Locator
45
	 */
46
	private $autoloader_locator;
47
48
	/**
49
	 * The Version_Selector object.
50
	 *
51
	 * @var Version_Selector
52
	 */
53
	private $version_selector;
54
55
	/**
56
	 * The constructor.
57
	 *
58
	 * @param string             $current_plugin_path The current plugin path.
59
	 * @param array              $active_plugin_paths The active plugin paths.
60
	 * @param Autoloader_Locator $autoloader_locator The Autoloader_Locator object.
61
	 * @param Version_Selector   $version_selector The Version_Selector object.
62
	 */
63
	public function __construct( $current_plugin_path, $active_plugin_paths, $autoloader_locator, $version_selector ) {
64
		$this->current_plugin_path = $current_plugin_path;
65
		$this->active_plugin_paths = $active_plugin_paths;
66
		$this->autoloader_locator  = $autoloader_locator;
67
		$this->version_selector    = $version_selector;
68
	}
69
70
	/**
71
	 * Finds the latest installed autoloader.
72
	 *
73
	 * @return bool True if this autoloader is the latest, false otherwise.
74
	 */
75
	public function is_latest_autoloader() {
76
		global $jetpack_autoloader_latest_version;
77
78
		if ( isset( $jetpack_autoloader_latest_version ) ) {
79
			return $jetpack_autoloader_latest_version === $this->autoloader_locator->get_autoloader_version( $this->current_plugin_path );
80
		}
81
82
		$latest_plugin = $this->autoloader_locator->find_latest_autoloader( $this->active_plugin_paths, $jetpack_autoloader_latest_version );
83
		if ( ! isset( $latest_plugin ) ) {
84
			return true;
85
		}
86
87
		if ( $latest_plugin !== $this->current_plugin_path ) {
88
			require $this->autoloader_locator->get_autoloader_path( $latest_plugin );
89
			return false;
90
		}
91
92
		return true;
93
	}
94
95
	/**
96
	 * Checks whether the autoloader should be reset. The autoloader should be reset:
97
	 *
98
	 *  - When a plugin is activated via a method other than a request, for example using WP-CLI.
99
	 *  - When the active plugins list changes between autoloader checks, for example when filtered by a plugin.
100
	 *
101
	 * We perform this reset because the manifest files for the plugin will have been initially unknown when
102
	 * selecting versions for classes and files.
103
	 *
104
	 * If the current plugin is not already known, this method will add it to the
105
	 * $jetpack_autoloader_activating_plugins_paths global.
106
	 * The $jetpack_autoloader_cached_plugin_paths global will store a cache of the
107
	 * active plugin paths when last changed.
108
	 *
109
	 * @return boolean True if the autoloader must be reset, else false.
110
	 */
111
	public function should_autoloader_reset() {
112
		global $jetpack_autoloader_activating_plugins_paths;
113
		global $jetpack_autoloader_cached_plugin_paths;
114
115
		$plugin_unknown = ! in_array( $this->current_plugin_path, $this->active_plugin_paths, true );
116
		if ( $plugin_unknown ) {
117
			if ( ! isset( $jetpack_autoloader_activating_plugins_paths ) ) {
118
				$jetpack_autoloader_activating_plugins_paths = array();
119
			}
120
121
			// If the current plugin isn't known, add it to the activating plugins list.
122
			$jetpack_autoloader_activating_plugins_paths[] = $this->current_plugin_path;
123
			$this->active_plugin_paths[]                   = $this->current_plugin_path;
124
		}
125
126
		$cache_invalidated = $jetpack_autoloader_cached_plugin_paths !== $this->active_plugin_paths;
127
		if ( $cache_invalidated ) {
128
			$jetpack_autoloader_cached_plugin_paths = $this->active_plugin_paths;
129
		}
130
131
		return $plugin_unknown || $cache_invalidated;
132
	}
133
134
	/**
135
	 * Builds the Version_Autoloader class that is used for autoloading.
136
	 *
137
	 * @return Version_Loader
138
	 */
139
	public function build_autoloader() {
140
		$manifest_handler = new Manifest_Handler( $this->active_plugin_paths, $this->version_selector );
141
142
		global $jetpack_packages_psr4;
143
		$jetpack_packages_psr4 = array();
144
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_psr4.php', $jetpack_packages_psr4 );
145
146
		global $jetpack_packages_classmap;
147
		$jetpack_packages_classmap = array();
148
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $jetpack_packages_classmap );
149
150
		global $jetpack_packages_filemap;
151
		$jetpack_packages_filemap = array();
152
		$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_filemap.php', $jetpack_packages_filemap );
153
154
		// Store the generated autoloader data in the loader so we can use it.
155
		return new Version_Loader(
156
			$this->version_selector,
157
			$jetpack_packages_classmap,
158
			$jetpack_packages_psr4,
159
			$jetpack_packages_filemap
160
		);
161
	}
162
163
	/**
164
	 * Updates the spl autoloader chain:
165
	 *  - Registers this namespace's autoloader function.
166
	 *  - If a v1 autoloader function is registered, moves it to the end of the chain.
167
	 *  - Removes any other v2 autoloader functions that have already been registered. This
168
	 *    can occur when the autoloader is being reset by an activating plugin.
169
	 */
170 View Code Duplication
	public function update_autoloader_chain() {
171
		spl_autoload_register( __NAMESPACE__ . '\autoloader' );
172
173
		$autoload_chain = spl_autoload_functions();
174
175
		foreach ( $autoload_chain as $autoloader ) {
176
			if ( ! is_string( $autoloader ) ) {
177
				/*
178
				 * The Jetpack Autoloader functions are registered as strings, so
179
				 * just continue if $autoloader isn't a string.
180
				 */
181
				continue;
182
			}
183
184
			if ( self::V1_AUTOLOADER_NAME === $autoloader ) {
185
				// Move the v1.* autoloader function to the end of the spl autoloader chain.
186
				spl_autoload_unregister( $autoloader );
187
				spl_autoload_register( $autoloader );
188
189
			} elseif (
190
				self::V2_AUTOLOADER_BASE === substr( $autoloader, 0, strlen( self::V2_AUTOLOADER_BASE ) )
191
				&& __NAMESPACE__ !== substr( $autoloader, 0, strlen( __NAMESPACE__ ) )
192
			) {
193
				// Unregister any other v2.* autoloader functions if they're in the chain.
194
				spl_autoload_unregister( $autoloader );
195
			}
196
		}
197
	}
198
}
199