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 |
||
10 | abstract class WC_Stripe_Connect_REST_Controller extends WP_REST_Controller { |
||
11 | |||
12 | /** |
||
13 | * Endpoint namespace. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $namespace = 'wc/v1'; |
||
18 | |||
19 | /** |
||
20 | * Stripe connect api. |
||
21 | * |
||
22 | * @var object $api |
||
23 | */ |
||
24 | private $api; |
||
25 | |||
26 | /** |
||
27 | * Constructor. |
||
28 | * |
||
29 | * @param WC_Stripe_Connect_API $api stripe connect api. |
||
30 | */ |
||
31 | public function __construct( WC_Stripe_Connect_API $api ) { |
||
35 | |||
36 | /** |
||
37 | * Registers rest routes for stripe connect functionality |
||
38 | */ |
||
39 | public function register_routes() { |
||
83 | |||
84 | /** |
||
85 | * Send get request. |
||
86 | * |
||
87 | * @param array $request request. |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function get_internal( $request ) { |
||
97 | |||
98 | /** |
||
99 | * Send post request. |
||
100 | * |
||
101 | * @param array $request request. |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | public function post_internal( $request ) { |
||
111 | |||
112 | /** |
||
113 | * Sends delete request. |
||
114 | * |
||
115 | * @param array $request request. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function delete_internal( $request ) { |
||
125 | |||
126 | /** |
||
127 | * Validate the requester's permissions |
||
128 | * |
||
129 | * @param array $request request. |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function check_permission( $request ) { |
||
137 | |||
138 | /** |
||
139 | * Consolidate cache prevention mechanisms. |
||
140 | */ |
||
141 | public function prevent_route_caching() { |
||
150 | |||
151 | /** |
||
152 | * Send a no-cache header for WCS REST API responses. Prompted by cache issues |
||
153 | * on the Pantheon hosting platform. |
||
154 | * |
||
155 | * See: https://pantheon.io/docs/cache-control/ |
||
156 | * |
||
157 | * @param WP_REST_Response $response REST API response. |
||
158 | * @param WP_REST_Server $server server. |
||
159 | * |
||
160 | * @return WP_REST_Response passthrough $response parameter |
||
161 | */ |
||
162 | public function send_nocache_header( $response, $server ) { |
||
168 | } |
||
169 |
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.