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 | public function setUp() { |
||
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:
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: