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 // phpcs:ignore WordPress.Files.FileName |
||
| 18 | class Test_Autoloader_Scenarios extends TestCase { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Indicates whether or not the autoloader has been reset by a load operation. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private $autoloader_reset; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Setup runs before each test. |
||
| 29 | * |
||
| 30 | * @before |
||
| 31 | */ |
||
| 32 | public function set_up() { |
||
| 33 | // We need to make sure there's an autoloader containing the current files for testing. |
||
| 34 | $this->generate_autoloader( 'plugin_current' ); |
||
| 35 | $this->generate_autoloader( 'plugin_newer' ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Teardown runs after each test. |
||
| 40 | * |
||
| 41 | * @after |
||
| 42 | */ |
||
| 43 | public function tear_down() { |
||
| 44 | cleanup_test_wordpress_data(); |
||
| 45 | |||
| 46 | // Make sure all of the tests have no cache file. |
||
| 47 | // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged |
||
| 48 | @unlink( TEST_DATA_PATH . '/cache/jetpack-autoloader-' . Plugins_Handler::TRANSIENT_KEY . '.json' ); |
||
| 49 | @rmdir( TEST_DATA_PATH . '/cache' ); |
||
| 50 | // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Tests that the autoloader works as expected. |
||
| 55 | */ |
||
| 56 | public function test_autoloader_init() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Tests that the autoloader does not initialize twice. |
||
| 69 | */ |
||
| 70 | public function test_autoloader_init_once() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Tests that the autoloader loads the latest when loading an older one first. |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function test_autoloader_loads_latest() { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Tests that the autoloader does not conflict with a v1 autoloader. |
||
| 102 | */ |
||
| 103 | public function test_autoloader_overrides_v1() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Tests that the autoloader is not reset when an older V2 initializes after the latest. |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function test_autoloader_not_reset_by_older_v2() { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Tests that the autoloader resets when an unknown plugin is encountered, and that it does not |
||
| 136 | * reset a second time once the unknown plugin has been recorded. |
||
| 137 | */ |
||
| 138 | public function test_autoloader_resets_when_unknown_plugin_is_encountered() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Tests that the autoloader uses the cache to avoid resetting when an known plugin is encountered. |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function test_autoloader_uses_cache_to_avoid_resets() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Tests that the autoloader updates the cache. |
||
| 172 | */ |
||
| 173 | public function test_autoloader_updates_cache() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Tests that the autoloader does not update the cache if it has not changed. |
||
| 188 | */ |
||
| 189 | public function test_autoloader_does_not_update_unchanged_cache() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Tests that the autoloader empties the cache if shutdown happens before plugins_loaded. |
||
| 209 | */ |
||
| 210 | public function test_autoloader_empties_cache_on_early_shutdown() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Generates a new autoloader from the current source files for the "plugin_current" plugin. |
||
| 227 | * |
||
| 228 | * @param string $plugin The plugin to generate the autoloader for. |
||
| 229 | */ |
||
| 230 | private function generate_autoloader( $plugin ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * "Activate" a plugin so that the autoloader can detect it. |
||
| 252 | * |
||
| 253 | * @param string $plugin The plugin we want to activate. |
||
| 254 | */ |
||
| 255 | private function activate_plugin( $plugin ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Loads the given autoloader and initializes it. |
||
| 268 | * |
||
| 269 | * @param string $plugin The plugin to load the autoloader from. |
||
| 270 | */ |
||
| 271 | private function load_autoloader( $plugin ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Writes the plugins to the cache so that they can be read by the autoloader. |
||
| 288 | * |
||
| 289 | * @param string[] $plugins The plugins to cache. |
||
| 290 | */ |
||
| 291 | private function cache_plugins( $plugins ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Runs the autoloader's shutdown action. |
||
| 305 | * |
||
| 306 | * @param bool $plugins_loaded Indicates whether or not the plugins_loaded action should have fired. |
||
| 307 | */ |
||
| 308 | private function shutdown_autoloader( $plugins_loaded = true ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Asserts that the latest autoloader version is the one given. |
||
| 318 | * |
||
| 319 | * @param string $version The version to check. |
||
| 320 | */ |
||
| 321 | private function assertAutoloaderVersion( $version ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Asserts that the autoloader cache contains the plugins given. |
||
| 331 | * |
||
| 332 | * @param array $plugins The plugins to check the cache for. |
||
| 333 | */ |
||
| 334 | private function assertAutoloaderCache( $plugins ) { |
||
| 348 | } |
||
| 349 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: