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() { |
||
41 | |||
42 | /** |
||
43 | * Teardown runs after each test. |
||
44 | * |
||
45 | * @after |
||
46 | */ |
||
47 | public function tear_down() { |
||
57 | |||
58 | /** |
||
59 | * Tests that the autoloader works as expected. |
||
60 | */ |
||
61 | public function test_autoloader_init() { |
||
62 | $this->activate_plugin( 'plugin_current' ); |
||
63 | |||
64 | $this->load_autoloader( 'plugin_current' ); |
||
65 | |||
66 | $this->assertAutoloaderVersion( '2.6.0.0' ); |
||
67 | |||
68 | $this->shutdown_autoloader( true ); |
||
69 | $this->assertAutoloaderCache( array( 'plugin_current' ) ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Tests that the autoloader does not initialize twice. |
||
74 | */ |
||
75 | public function test_autoloader_init_once() { |
||
87 | |||
88 | /** |
||
89 | * Tests that the autoloader loads the latest when loading an older one first. |
||
90 | */ |
||
91 | View Code Duplication | public function test_autoloader_loads_latest() { |
|
104 | |||
105 | /** |
||
106 | * Tests that the autoloader does not conflict with a v1 autoloader. |
||
107 | */ |
||
108 | public function test_autoloader_overrides_v1() { |
||
121 | |||
122 | /** |
||
123 | * Tests that the autoloader is not reset when an older V2 initializes after the latest. |
||
124 | */ |
||
125 | View Code Duplication | public function test_autoloader_not_reset_by_older_v2() { |
|
138 | |||
139 | /** |
||
140 | * Tests that the autoloader resets when an unknown plugin is encountered, and that it does not |
||
141 | * reset a second time once the unknown plugin has been recorded. |
||
142 | */ |
||
143 | public function test_autoloader_resets_when_unknown_plugin_is_encountered() { |
||
155 | |||
156 | /** |
||
157 | * Tests that the autoloader uses the cache to avoid resetting when an known plugin is encountered. |
||
158 | */ |
||
159 | View Code Duplication | public function test_autoloader_uses_cache_to_avoid_resets() { |
|
174 | |||
175 | /** |
||
176 | * Tests that the autoloader updates the cache. |
||
177 | */ |
||
178 | public function test_autoloader_updates_cache() { |
||
179 | $this->activate_plugin( 'plugin_current' ); |
||
180 | |||
181 | // Write an empty cache so we can make sure it was updated. |
||
182 | $this->cache_plugins( array() ); |
||
183 | |||
184 | $this->load_autoloader( 'plugin_current' ); |
||
185 | $this->shutdown_autoloader( true ); |
||
186 | |||
187 | $this->assertAutoloaderVersion( '2.6.0.0' ); |
||
188 | $this->assertAutoloaderCache( array( 'plugin_current' ) ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Tests that the autoloader does not update the cache if it has not changed. |
||
193 | */ |
||
194 | public function test_autoloader_does_not_update_unchanged_cache() { |
||
211 | |||
212 | /** |
||
213 | * Tests that the autoloader empties the cache if shutdown happens before plugins_loaded. |
||
214 | */ |
||
215 | public function test_autoloader_empties_cache_on_early_shutdown() { |
||
216 | $this->activate_plugin( 'plugin_current' ); |
||
217 | |||
218 | // Write a cache that we can use when loading the autoloader. |
||
219 | $this->cache_plugins( array( 'plugin_current' ) ); |
||
220 | |||
221 | $this->load_autoloader( 'plugin_current' ); |
||
222 | |||
223 | // Make sure to shutdown prematurely so that the cache will be erased instead of saved. |
||
224 | $this->shutdown_autoloader( false ); |
||
225 | |||
226 | $this->assertAutoloaderVersion( '2.6.0.0' ); |
||
227 | $this->assertAutoloaderCache( array() ); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Tests that the autoloader is able to resolve symbolic links to avoid duplicate plugin entries. |
||
232 | */ |
||
233 | public function test_autoloader_resolves_symlinks() { |
||
234 | $this->activate_plugin( 'plugin_current', 'plugin_symlink' ); |
||
235 | |||
236 | $this->load_autoloader( 'plugin_symlink' ); |
||
237 | |||
238 | $this->assertAutoloaderVersion( '2.6.0.0' ); |
||
239 | |||
240 | $this->shutdown_autoloader( true ); |
||
241 | // Since there's no cache we should expect the resolved path. |
||
242 | $this->assertAutoloaderCache( array( 'plugin_current' ) ); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Tests that the autoloader can handle cases where the cached path is a symlink. |
||
247 | */ |
||
248 | public function test_autoloader_resolves_cached_symlinks() { |
||
249 | $this->cache_plugins( array( 'plugin_symlink' ) ); |
||
250 | |||
251 | $this->activate_plugin( 'plugin_current', 'plugin_symlink' ); |
||
252 | |||
253 | $this->load_autoloader( 'plugin_symlink' ); |
||
254 | |||
255 | $this->assertAutoloaderVersion( '2.6.0.0' ); |
||
256 | |||
257 | $this->shutdown_autoloader( true ); |
||
258 | // The cache shouldn't be updated since internally real paths are always used. |
||
259 | $this->assertAutoloaderCache( array( 'plugin_symlink' ) ); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Generates a new autoloader from the current source files for the "plugin_current" plugin. |
||
264 | * |
||
265 | * @param string $plugin The plugin to generate the autoloader for. |
||
266 | */ |
||
267 | private function generate_autoloader( $plugin ) { |
||
286 | |||
287 | /** |
||
288 | * "Activate" a plugin so that the autoloader can detect it. |
||
289 | * |
||
290 | * @param string $plugin The plugin we want to activate. |
||
291 | * @param string $folder The folder that the plugin is in. If empty this will default to $plugin. |
||
292 | */ |
||
293 | private function activate_plugin( $plugin, $folder = '' ) { |
||
307 | |||
308 | /** |
||
309 | * Loads the given autoloader and initializes it. |
||
310 | * |
||
311 | * @param string $plugin The plugin to load the autoloader from. |
||
312 | */ |
||
313 | private function load_autoloader( $plugin ) { |
||
327 | |||
328 | /** |
||
329 | * Writes the plugins to the cache so that they can be read by the autoloader. |
||
330 | * |
||
331 | * @param string[] $plugins The plugins to cache. |
||
332 | */ |
||
333 | private function cache_plugins( $plugins ) { |
||
344 | |||
345 | /** |
||
346 | * Runs the autoloader's shutdown action. |
||
347 | * |
||
348 | * @param bool $plugins_loaded Indicates whether or not the plugins_loaded action should have fired. |
||
349 | */ |
||
350 | private function shutdown_autoloader( $plugins_loaded = true ) { |
||
357 | |||
358 | /** |
||
359 | * Asserts that the latest autoloader version is the one given. |
||
360 | * |
||
361 | * @param string $version The version to check. |
||
362 | */ |
||
363 | private function assertAutoloaderVersion( $version ) { |
||
370 | |||
371 | /** |
||
372 | * Asserts that the autoloader cache contains the plugins given. |
||
373 | * |
||
374 | * @param array $plugins The plugins to check the cache for. |
||
375 | */ |
||
376 | private function assertAutoloaderCache( $plugins ) { |
||
390 | } |
||
391 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: