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 |
||
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 ) { |
||
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() { |
||
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() { |
||
133 | |||
134 | /** |
||
135 | * Builds the Version_Autoloader class that is used for autoloading. |
||
136 | * |
||
137 | * @return Version_Loader |
||
138 | */ |
||
139 | public function build_autoloader() { |
||
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() { |
|
198 | } |
||
199 |