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 |
||
7 | class Autoloader_Handler { |
||
8 | |||
9 | // The name of the autoloader function registered by v1.* autoloaders. |
||
10 | const V1_AUTOLOADER_NAME = 'Automattic\Jetpack\Autoloader\autoloader'; |
||
11 | |||
12 | /* |
||
13 | * The autoloader function for v2.* autoloaders is named __NAMESPACE__ . \autoloader. |
||
14 | * The namespace is defined in AutoloadGenerator as |
||
15 | * 'Automattic\Jetpack\Autoloader\jp' plus a unique suffix. |
||
16 | */ |
||
17 | const V2_AUTOLOADER_BASE = 'Automattic\Jetpack\Autoloader\jp'; |
||
18 | |||
19 | const AUTOLOAD_GENERATOR_CLASS_NAME = 'Automattic\Jetpack\Autoloader\AutoloadGenerator'; |
||
20 | |||
21 | /** |
||
22 | * The Plugins_Handler object. |
||
23 | * |
||
24 | * @var Plugins_Handler |
||
25 | */ |
||
26 | private $plugins_handler = null; |
||
27 | |||
28 | /** |
||
29 | * The Version_Selector object. |
||
30 | * |
||
31 | * @var Version_Selector |
||
32 | */ |
||
33 | private $version_selector = null; |
||
34 | |||
35 | /** |
||
36 | * The constructor. |
||
37 | * |
||
38 | * @param Plugins_Handler $plugins_handler The Plugins_Handler object. |
||
39 | * @param Version_Selector $version_selector The Version_Selector object. |
||
40 | */ |
||
41 | public function __construct( $plugins_handler, $version_selector ) { |
||
45 | |||
46 | /** |
||
47 | * Finds the latest installed autoloader. |
||
48 | */ |
||
49 | public function find_latest_autoloader() { |
||
85 | |||
86 | /** |
||
87 | * Check the plugins directory for autoloader v1.x files. If the plugin's directory contains |
||
88 | * v1.x autoloader files, we will ignore this plugins autoloader. |
||
89 | * |
||
90 | * This situation can occur if a plugin has a v2.x autoloader and a backup containing a version |
||
91 | * of the plugin with a v1.x autoloader is restored. The v2.x autolaoder files remain in the |
||
92 | * plugin directory. |
||
93 | * |
||
94 | * @param string $autoload_packages_path The path to the plugin's vendor/autoload_packages.php file. |
||
95 | * |
||
96 | * @return boolean True if the plugin directory contains a v1.x autoloader, false otherwise. |
||
97 | */ |
||
98 | private function check_for_v1_files( $autoload_packages_path ) { |
||
146 | |||
147 | /** |
||
148 | * Get this autoloader's package version. |
||
149 | * |
||
150 | * @return String The autoloader's package version. |
||
151 | */ |
||
152 | public function get_current_autoloader_version() { |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Updates the spl autoloader chain: |
||
162 | * - Registers this namespace's autoloader function. |
||
163 | * - If a v1 autoloader function is registered, moves it to the end of the chain. |
||
164 | * - Removes any other v2 autoloader functions that have already been registered. This |
||
165 | * can occur when the autoloader is being reset by an activating plugin. |
||
166 | */ |
||
167 | public function update_autoloader_chain() { |
||
195 | } |
||
196 |