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:
Complex classes like Jetpack_EU_Cookie_Law_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_EU_Cookie_Law_Widget, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { |
||
19 | /** |
||
20 | * EU Cookie Law cookie name. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | public static $cookie_name = 'eucookielaw'; |
||
25 | |||
26 | /** |
||
27 | * Default hide options. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $hide_options = array( |
||
32 | 'button', |
||
33 | 'scroll', |
||
34 | 'time', |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Default text options. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $text_options = array( |
||
43 | 'default', |
||
44 | 'custom', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Default color scheme options. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $color_scheme_options = array( |
||
53 | 'default', |
||
54 | 'negative', |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Default policy URL options. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | private $policy_url_options = array( |
||
63 | 'default', |
||
64 | 'custom', |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * Widget position options. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $position_options = array( |
||
73 | 'bottom', |
||
74 | 'top', |
||
75 | ); |
||
76 | |||
77 | /** |
||
78 | * Constructor. |
||
79 | */ |
||
80 | View Code Duplication | function __construct() { |
|
96 | |||
97 | /** |
||
98 | * Enqueue scripts and styles. |
||
99 | */ |
||
100 | function enqueue_frontend_scripts() { |
||
101 | wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), JETPACK__VERSION ); |
||
102 | |||
103 | if ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) { |
||
104 | wp_enqueue_script( |
||
105 | 'eu-cookie-law-script', |
||
106 | Assets::get_file_url_for_environment( |
||
107 | '_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js', |
||
108 | 'modules/widgets/eu-cookie-law/eu-cookie-law.js' |
||
109 | ), |
||
110 | array(), |
||
111 | '20180522', |
||
112 | true |
||
113 | ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return an associative array of default values. |
||
119 | * |
||
120 | * These values are used in new widgets. |
||
121 | * |
||
122 | * @return array Default values for the widget options. |
||
123 | */ |
||
124 | public function defaults() { |
||
141 | |||
142 | /** |
||
143 | * Front-end display of the widget. |
||
144 | * |
||
145 | * @param array $args Widget arguments. |
||
146 | * @param array $instance Saved values from database. |
||
147 | */ |
||
148 | public function widget( $args, $instance ) { |
||
149 | /** |
||
150 | * Filters the display of the EU Cookie Law widget. |
||
151 | * |
||
152 | * @since 6.1.1 |
||
153 | * |
||
154 | * @param bool true Should the EU Cookie Law widget be disabled. Default to false. |
||
155 | */ |
||
156 | if ( apply_filters( 'jetpack_disable_eu_cookie_law_widget', false ) ) { |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
|
|||
161 | |||
162 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
||
163 | require dirname( __FILE__ ) . '/eu-cookie-law/widget-amp.php'; |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | $classes = array(); |
||
168 | $classes['hide'] = 'hide-on-' . esc_attr( $instance['hide'] ); |
||
169 | if ( 'negative' === $instance['color-scheme'] ) { |
||
170 | $classes['negative'] = 'negative'; |
||
171 | } |
||
172 | |||
173 | if ( 'top' === $instance['position'] ) { |
||
174 | $classes['top'] = 'top'; |
||
175 | } |
||
176 | |||
177 | if ( Jetpack::is_module_active( 'wordads' ) ) { |
||
178 | $classes['ads'] = 'ads-active'; |
||
179 | $classes['hide'] = 'hide-on-button'; |
||
180 | } |
||
181 | |||
182 | echo $args['before_widget']; |
||
183 | require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' ); |
||
184 | echo $args['after_widget']; |
||
185 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
186 | do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Back-end widget form. |
||
191 | * |
||
192 | * @param array $instance Previously saved values from database. |
||
193 | */ |
||
194 | public function form( $instance ) { |
||
212 | |||
213 | /** |
||
214 | * Sanitize widget form values as they are saved. |
||
215 | * |
||
216 | * @param array $new_instance Values just sent to be saved. |
||
217 | * @param array $old_instance Previously saved values from database. |
||
218 | * @return array Updated safe values to be saved. |
||
219 | */ |
||
220 | public function update( $new_instance, $old_instance ) { |
||
287 | |||
288 | /** |
||
289 | * Check if the value is allowed and not empty. |
||
290 | * |
||
291 | * @param string $value Value to check. |
||
292 | * @param array $allowed Array of allowed values. |
||
293 | * |
||
294 | * @return string $value if pass the check or first value from allowed values. |
||
295 | */ |
||
296 | View Code Duplication | function filter_value( $value, $allowed = array() ) { |
|
303 | } |
||
304 | |||
305 | // Register Jetpack_EU_Cookie_Law_Widget widget. |
||
312 |
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: