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.NotHyphenatedLowercase |
||
| 16 | View Code Duplication | class TokensTest extends TestCase { |
|
|
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * Temporary stack for `wp_redirect`. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $arguments_stack = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * User ID added for the test. |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $user_id; |
||
| 31 | |||
| 32 | const DEFAULT_TEST_CAPS = array( 'default_test_caps' ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Initialize the object before running the test method. |
||
| 36 | * |
||
| 37 | * @before |
||
| 38 | */ |
||
| 39 | public function set_up() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Clean up the testing environment. |
||
| 51 | * |
||
| 52 | * @after |
||
| 53 | */ |
||
| 54 | public function tear_down() { |
||
| 58 | } |
||
| 59 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.