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 |
||
8 | class WC_Stripe_Payment_Gateway_Test extends WP_UnitTestCase { |
||
9 | /** |
||
10 | * Gateway under test. |
||
11 | * |
||
12 | * @var WC_Gateway_Stripe |
||
13 | */ |
||
14 | private $gateway; |
||
15 | |||
16 | /** |
||
17 | * Sets up things all tests need. |
||
18 | */ |
||
19 | public function setUp() { |
||
24 | |||
25 | /** |
||
26 | * Helper function to update test order meta data |
||
27 | */ |
||
28 | private function updateOrderMeta( $order, $key, $value ) { |
||
29 | $order->update_meta_data( $key, $value ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Tests false is returned if payment intent is not set in the order. |
||
34 | */ |
||
35 | public function test_default_get_payment_intent_from_order() { |
||
40 | |||
41 | /** |
||
42 | * Tests if payment intent is fetched from Stripe API. |
||
43 | */ |
||
44 | public function test_success_get_payment_intent_from_order() { |
||
71 | |||
72 | /** |
||
73 | * Tests if false is returned when error is returned from Stripe API. |
||
74 | */ |
||
75 | public function test_error_get_payment_intent_from_order() { |
||
107 | } |
||
108 |
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.