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 |
||
15 | class Jetpack_Recommendations_Banner { |
||
16 | /** |
||
17 | * Jetpack_Recommendations_Banner |
||
18 | * |
||
19 | * @var Jetpack_Recommendations_Banner |
||
20 | **/ |
||
21 | private static $instance = null; |
||
22 | |||
23 | /** |
||
24 | * Factory method |
||
25 | */ |
||
26 | public static function init() { |
||
33 | |||
34 | /** |
||
35 | * Jetpack_Recommendations_Banner constructor. |
||
36 | */ |
||
37 | private function __construct() { |
||
40 | |||
41 | /** |
||
42 | * Initialize hooks to display the banner |
||
43 | */ |
||
44 | public function maybe_initialize_hooks() { |
||
53 | |||
54 | /** |
||
55 | * Determines if the banner can be displayed |
||
56 | */ |
||
57 | public static function can_be_displayed() { |
||
58 | if ( ! Jetpack_Recommendations::is_banner_enabled() ) { |
||
59 | return false; |
||
60 | } |
||
61 | |||
62 | // Only the dashboard and plugins pages should see the banner. |
||
63 | if ( ! in_array( get_current_screen()->id, array( 'dashboard', 'plugins' ), true ) ) { |
||
64 | return false; |
||
65 | } |
||
66 | |||
67 | if ( ! current_user_can( 'jetpack_manage_modules' ) ) { |
||
68 | return false; |
||
69 | } |
||
70 | |||
71 | if ( Jetpack_Options::get_option( 'recommendations_banner_dismissed' ) ) { |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | if ( ! in_array( |
||
76 | Jetpack_Options::get_option( 'recommendations_step', 'not-started' ), |
||
77 | array( |
||
78 | 'not-started', |
||
79 | 'site-type-question', |
||
80 | ), |
||
81 | true |
||
82 | ) ) { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Handles storing the user responses in the banner. |
||
91 | */ |
||
92 | public static function ajax_callback() { |
||
93 | check_ajax_referer( 'jp-recommendations-banner-nonce', 'nonce' ); |
||
94 | |||
95 | if ( ! current_user_can( 'jetpack_manage_modules' ) ) { |
||
96 | wp_die(); |
||
97 | } |
||
98 | |||
99 | $tracking = new Tracking(); |
||
100 | |||
101 | if ( isset( $_REQUEST['dismissBanner'] ) && 'true' === $_REQUEST['dismissBanner'] ) { |
||
102 | Jetpack_Options::update_option( 'recommendations_banner_dismissed', 1 ); |
||
103 | $tracking->record_user_event( 'recommendations_banner_dismissed' ); |
||
104 | wp_send_json_success(); |
||
105 | wp_die(); |
||
106 | } |
||
107 | |||
108 | $data = Jetpack_Recommendations::get_recommendations_data(); |
||
109 | |||
110 | $tracking_answers = array(); |
||
111 | |||
112 | View Code Duplication | if ( isset( $_REQUEST['personal'] ) && is_string( $_REQUEST['personal'] ) ) { |
|
113 | $value = 'true' === $_REQUEST['personal'] ? true : false; |
||
114 | $data['site-type-personal'] = $value; |
||
115 | $tracking_answers['personal'] = $value; |
||
116 | } |
||
117 | |||
118 | View Code Duplication | if ( isset( $_REQUEST['business'] ) && is_string( $_REQUEST['business'] ) ) { |
|
119 | $value = 'true' === $_REQUEST['business'] ? true : false; |
||
120 | $data['site-type-business'] = $value; |
||
121 | $tracking_answers['business'] = $value; |
||
122 | } |
||
123 | |||
124 | View Code Duplication | if ( isset( $_REQUEST['store'] ) && is_string( $_REQUEST['store'] ) ) { |
|
125 | $value = 'true' === $_REQUEST['store'] ? true : false; |
||
126 | $data['site-type-store'] = $value; |
||
127 | $tracking_answers['store'] = $value; |
||
128 | } |
||
129 | |||
130 | View Code Duplication | if ( isset( $_REQUEST['other'] ) && is_string( $_REQUEST['other'] ) ) { |
|
131 | $value = 'true' === $_REQUEST['other'] ? true : false; |
||
132 | $data['site-type-other'] = $value; |
||
133 | $tracking_answers['other'] = $value; |
||
134 | } |
||
135 | |||
136 | Jetpack_Recommendations::update_recommendations_data( $data ); |
||
|
|||
137 | Jetpack_Options::update_option( 'recommendations_step', 'banner-completed' ); |
||
138 | |||
139 | $tracking->record_user_event( 'recommendations_banner_answered', $tracking_answers ); |
||
140 | |||
141 | wp_send_json_success(); |
||
142 | wp_die(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Enqueue JavaScript files. |
||
147 | */ |
||
148 | View Code Duplication | public function enqueue_banner_scripts() { |
|
170 | |||
171 | /** |
||
172 | * Include the needed styles |
||
173 | */ |
||
174 | public function admin_banner_styles() { |
||
185 | |||
186 | /** |
||
187 | * Renders the Recommendations Banner |
||
188 | */ |
||
189 | public function render_banner() { |
||
246 | |||
247 | /** |
||
248 | * Renders a checkbox. |
||
249 | * |
||
250 | * @param string $name The name to give the form input. |
||
251 | * @param string $title The title to put on the checkbox. |
||
252 | */ |
||
253 | private function render_checkbox( $name, $title ) { |
||
263 | } |
||
264 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: