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 if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
15 | class EE_Recommended_Versions extends EE_Middleware { |
||
16 | |||
17 | |||
18 | /** |
||
19 | * converts a Request to a Response |
||
20 | * |
||
21 | * @param EE_Request $request |
||
22 | * @param EE_Response $response |
||
23 | * @return EE_Response |
||
24 | */ |
||
25 | public function handle_request( EE_Request $request, EE_Response $response ) { |
||
26 | $this->_request = $request; |
||
27 | $this->_response = $response; |
||
28 | //$this->_response->add_output( "\n\t IN >> " . __CLASS__ ); |
||
29 | //$this->_response->set_notice( 1, 'hey look at this' ); |
||
30 | // check required WP version |
||
31 | View Code Duplication | if ( ! $this->_minimum_wp_version_required() ) { |
|
|
|||
32 | $this->_request->un_set( 'activate', true ); |
||
33 | add_action( 'admin_notices', array( $this, 'minimum_wp_version_error' ), 1 ); |
||
34 | //$this->_response->add_output( "\n<br />" . 'minimum_wp_version_error' ); |
||
35 | $this->_response->terminate_request(); |
||
36 | } |
||
37 | // check required PHP version |
||
38 | View Code Duplication | if ( ! $this->_minimum_php_version_required() ) { |
|
39 | $this->_request->un_set( 'activate', true ); |
||
40 | add_action( 'admin_notices', array( $this, 'minimum_php_version_error' ), 1 ); |
||
41 | //$this->_response->add_output( "\n<br />" . 'minimum_php_version_error' ); |
||
42 | $this->_response->terminate_request(); |
||
43 | } |
||
44 | // check recommended WP version |
||
45 | if ( ! $this->_minimum_wp_version_recommended() ) { |
||
46 | $this->_display_minimum_recommended_wp_version_notice(); |
||
47 | } |
||
48 | // check recommended PHP version |
||
49 | if ( ! $this->_minimum_php_version_recommended() ) { |
||
50 | $this->_display_minimum_recommended_php_version_notice(); |
||
51 | } |
||
52 | $this->_response = $this->process_request_stack( $this->_request, $this->_response ); |
||
53 | //$this->_response->add_output( "\n\t OUT << " . __CLASS__ ); |
||
54 | return $this->_response; |
||
55 | } |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * _check_wp_version |
||
61 | * |
||
62 | * @access private |
||
63 | * @param string $min_version |
||
64 | * @return boolean |
||
65 | */ |
||
66 | private function _check_wp_version( $min_version = EE_MIN_WP_VER_REQUIRED ) { |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * _minimum_wp_version_required |
||
75 | * |
||
76 | * @access private |
||
77 | * @return boolean |
||
78 | */ |
||
79 | private function _minimum_wp_version_required() { |
||
82 | |||
83 | |||
84 | |||
85 | /** |
||
86 | * _minimum_wp_version_recommended |
||
87 | * |
||
88 | * @access private |
||
89 | * @return boolean |
||
90 | */ |
||
91 | private function _minimum_wp_version_recommended() { |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * _check_php_version |
||
99 | * |
||
100 | * @access private |
||
101 | * @param string $min_version |
||
102 | * @return boolean |
||
103 | */ |
||
104 | private function _check_php_version( $min_version = EE_MIN_PHP_VER_RECOMMENDED ) { |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * _minimum_php_version_required |
||
112 | * |
||
113 | * @access private |
||
114 | * @return boolean |
||
115 | */ |
||
116 | private function _minimum_php_version_required() { |
||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * _minimum_php_version_recommended |
||
124 | * |
||
125 | * @access private |
||
126 | * @return boolean |
||
127 | */ |
||
128 | private function _minimum_php_version_recommended() { |
||
131 | |||
132 | |||
133 | |||
134 | /** |
||
135 | * minimum_wp_version_error |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function minimum_wp_version_error() { |
||
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * minimum_php_version_error |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function minimum_php_version_error() { |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * _display_minimum_recommended_wp_version_notice |
||
187 | * |
||
188 | * @access private |
||
189 | * @return void |
||
190 | */ |
||
191 | View Code Duplication | private function _display_minimum_recommended_wp_version_notice() { |
|
204 | |||
205 | |||
206 | |||
207 | /** |
||
208 | * _display_minimum_recommended_php_version_notice |
||
209 | * |
||
210 | * @access private |
||
211 | * @return void |
||
212 | */ |
||
213 | View Code Duplication | private function _display_minimum_recommended_php_version_notice() { |
|
225 | |||
226 | } |
||
227 | |||
231 | // Location: /EE_Recommended_Versions.core.php |
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.