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 |
||
| 15 | class WP_Test_Integration_Loader extends TestCase { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * A manifest handler configured for a single plugin. |
||
| 19 | * |
||
| 20 | * @var Manifest_Handler |
||
| 21 | */ |
||
| 22 | private $single_manifest_handler; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * A manifest handler configured for multiple plugins. |
||
| 26 | * |
||
| 27 | * @var Manifest_Handler |
||
| 28 | */ |
||
| 29 | private $multiple_manifest_handler; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Setup runs before each test. |
||
| 33 | * |
||
| 34 | * @before |
||
| 35 | */ |
||
| 36 | public function set_up() { |
||
| 37 | $this->single_manifest_handler = new Manifest_Handler( |
||
| 38 | array( |
||
| 39 | TEST_DATA_PATH . '/plugins/plugin_current', |
||
| 40 | ), |
||
| 41 | new Version_Selector() |
||
| 42 | ); |
||
| 43 | $this->multiple_manifest_handler = new Manifest_Handler( |
||
| 44 | array( |
||
| 45 | TEST_DATA_PATH . '/plugins/plugin_current', |
||
| 46 | TEST_DATA_PATH . '/plugins/plugin_newer', |
||
| 47 | ), |
||
| 48 | new Version_Selector() |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Tests that the classmap manifest from a single plugin can be handled correctly. |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function test_single_plugin_classmap() { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Tests that the classmap manifest from multiple plugins can be handled correctly. |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function test_multiple_plugin_classmap() { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Tests that the PSR-4 manifest from a single plugin can be handled correctly. |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function test_single_plugin_psr4() { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Tests that the PSR-4 manifest from multiple plugins can be handled correctly. |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function test_multiple_plugin_psr4() { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Tests that the filemap manifest from a single plugin can be handled correctly. |
||
| 142 | * |
||
| 143 | * @preserveGlobalState disabled |
||
| 144 | * @runInSeparateProcess |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function test_single_plugin_filemap() { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Tests that the filemap manifest from multiple plugins can be handled correctly. |
||
| 168 | * |
||
| 169 | * @preserveGlobalState disabled |
||
| 170 | * @runInSeparateProcess |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function test_multiple_plugin_filemap() { |
|
| 191 | } |
||
| 192 |
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: