| Conditions | 17 |
| Paths | 30 |
| Total Lines | 183 |
| Code Lines | 141 |
| Lines | 136 |
| Ratio | 74.32 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 205 | } else { |
||
| 206 | $text = __( 'to go.', 'jetpack' ); |
||
| 207 | } |
||
| 208 | |||
| 209 | /* Countdown to the milestone. */ |
||
| 210 | echo '<div class="milestone-countdown">' . sprintf( __( '%1$s %2$s %3$s', 'jetpack' ), |
||
| 211 | '<span class="difference"></span>', |
||
| 212 | '<span class="label"></span>', |
||
| 213 | $text |
||
| 214 | ) . '</div>'; |
||
| 215 | |||
| 216 | self::$config_js['instances'][] = array( |
||
| 217 | 'id' => $args['widget_id'], |
||
| 218 | 'diff' => $diff, |
||
| 219 | 'message' => $instance['message'], |
||
| 220 | 'unit' => $instance['unit'], |
||
| 221 | 'type' => $instance['type'], |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | echo '</div><!--milestone-content-->'; |
||
| 226 | |||
| 227 | echo $args['after_widget']; |
||
| 228 | |||
| 229 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 230 | do_action( 'jetpack_stats_extra', 'widget_view', 'milestone' ); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Update |
||
| 235 | */ |
||
| 236 | function update( $new_instance, $old_instance ) { |
||
| 237 | return $this->sanitize_instance( $new_instance ); |
||
| 238 | } |
||
| 239 | |||
| 240 | /* |
||
| 241 | * Make sure that a number is within a certain range. |
||
| 242 | * If the number is too small it will become the possible lowest value. |
||
| 243 | * If the number is too large it will become the possible highest value. |
||
| 244 | * |
||
| 245 | * @param int $n The number to check. |
||
| 246 | * @param int $floor The lowest possible value. |
||
| 247 | * @param int $ceil The highest possible value. |
||
| 248 | */ |
||
| 249 | function sanitize_range( $n, $floor, $ceil ) { |
||
| 250 | $n = (int) $n; |
||
| 251 | if ( $n < $floor ) { |
||
| 252 | $n = $floor; |
||
| 253 | } elseif ( $n > $ceil ) { |
||
| 254 | $n = $ceil; |
||
| 255 | } |
||
| 256 | return $n; |
||
| 257 | } |
||
| 258 | |||
| 259 | /* |
||
| 260 | * Sanitize an instance of this widget. |
||
| 261 | * |
||
| 262 | * Date ranges match the documentation for mktime in the php manual. |
||
| 263 | * @see http://php.net/manual/en/function.mktime.php#refsect1-function.mktime-parameters |
||
| 264 | * |
||
| 265 | * @uses Milestone_Widget::sanitize_range(). |
||
| 266 | */ |
||
| 267 | function sanitize_instance( $dirty ) { |
||
| 268 | $now = (int) current_time( 'timestamp' ); |
||
| 269 | |||
| 270 | $dirty = wp_parse_args( $dirty, array( |
||
| 271 | 'title' => '', |
||
| 272 | 'event' => __( 'The Big Day', 'jetpack' ), |
||
| 273 | 'unit' => 'automatic', |
||
| 274 | 'type' => 'until', |
||
| 275 | 'message' => __( 'The big day is here.', 'jetpack' ), |
||
| 276 | 'day' => date( 'd', $now ), |
||
| 277 | 'month' => date( 'm', $now ), |
||
| 278 | 'year' => date( 'Y', $now ), |
||
| 279 | 'hour' => 0, |
||
| 280 | 'min' => 0, |
||
| 281 | ) ); |
||
| 282 | |||
| 283 | $allowed_tags = array( |
||
| 284 | 'a' => array( 'title' => array(), 'href' => array(), 'target' => array() ), |
||
| 285 | 'em' => array( 'title' => array() ), |
||
| 286 | 'strong' => array( 'title' => array() ), |
||
| 287 | ); |
||
| 288 | |||
| 289 | $clean = array( |
||
| 290 | 'title' => trim( strip_tags( stripslashes( $dirty['title'] ) ) ), |
||
| 291 | 'event' => trim( strip_tags( stripslashes( $dirty['event'] ) ) ), |
||
| 292 | 'unit' => $dirty['unit'], |
||
| 293 | 'type' => $dirty['type'], |
||
| 294 | 'message' => wp_kses( $dirty['message'], $allowed_tags ), |
||
| 295 | 'year' => $this->sanitize_range( $dirty['year'], 1901, 2037 ), |
||
| 296 | 'month' => $this->sanitize_range( $dirty['month'], 1, 12 ), |
||
| 297 | 'hour' => $this->sanitize_range( $dirty['hour'], 0, 23 ), |
||
| 298 | 'min' => zeroise( $this->sanitize_range( $dirty['min'], 0, 59 ), 2 ), |
||
| 299 | ); |
||
| 300 | |||
| 301 | $clean['day'] = $this->sanitize_range( $dirty['day'], 1, date( 't', mktime( 0, 0, 0, $clean['month'], 1, $clean['year'] ) ) ); |
||
| 302 | |||
| 303 | return $clean; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Form |
||
| 308 | */ |
||
| 309 | function form( $instance ) { |
||
| 310 | $instance = $this->sanitize_instance( $instance ); |
||
| 311 | |||
| 312 | $units = array( |
||
| 313 | 'automatic' => __( 'Automatic', 'jetpack' ), |
||
| 314 | 'months' => __( 'Months', 'jetpack' ), |
||
| 315 | 'days' => __( 'Days', 'jetpack' ), |
||
| 316 | 'hours' => __( 'Hours', 'jetpack' ), |
||
| 317 | ); |
||
| 318 | ?> |
||
| 319 | |||
| 320 | <div class="milestone-widget"> |
||
| 321 | <p> |
||
| 322 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'jetpack' ); ?></label> |
||
| 323 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
||
| 324 | </p> |
||
| 325 | |||
| 326 | <p> |
||
| 327 | <label for="<?php echo $this->get_field_id( 'event' ); ?>"><?php _e( 'Description', 'jetpack' ); ?></label> |
||
| 328 | <input class="widefat" id="<?php echo $this->get_field_id( 'event' ); ?>" name="<?php echo $this->get_field_name( 'event' ); ?>" type="text" value="<?php echo esc_attr( $instance['event'] ); ?>" /> |
||
| 329 | </p> |
||
| 330 | |||
| 331 | <fieldset class="jp-ms-data-time"> |
||
| 332 | <legend><?php esc_html_e( 'Date', 'jetpack' ); ?></legend> |
||
| 333 | |||
| 334 | <label for="<?php echo $this->get_field_id( 'month' ); ?>" class="assistive-text"><?php _e( 'Month', 'jetpack' ); ?></label> |
||
| 335 | <select id="<?php echo $this->get_field_id( 'month' ); ?>" class="month" name="<?php echo $this->get_field_name( 'month' ); ?>"><?php |
||
| 336 | global $wp_locale; |
||
| 337 | for ( $i = 1; $i < 13; $i++ ) { |
||
| 338 | $monthnum = zeroise( $i, 2 ); |
||
| 339 | echo '<option value="' . esc_attr( $monthnum ) . '"' . selected( $i, $instance['month'], false ) . '>' . $monthnum . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . '</option>'; |
||
| 340 | } |
||
| 341 | ?></select> |
||
| 342 | |||
| 343 | <label for="<?php echo $this->get_field_id( 'day' ); ?>" class="assistive-text"><?php _e( 'Day', 'jetpack' ); ?></label> |
||
| 344 | <input id="<?php echo $this->get_field_id( 'day' ); ?>" class="day" name="<?php echo $this->get_field_name( 'day' ); ?>" type="text" value="<?php echo esc_attr( $instance['day'] ); ?>">, |
||
| 345 | |||
| 346 | <label for="<?php echo $this->get_field_id( 'year' ); ?>" class="assistive-text"><?php _e( 'Year', 'jetpack' ); ?></label> |
||
| 347 | <input id="<?php echo $this->get_field_id( 'year' ); ?>" class="year" name="<?php echo $this->get_field_name( 'year' ); ?>" type="text" value="<?php echo esc_attr( $instance['year'] ); ?>"> |
||
| 348 | </fieldset> |
||
| 349 | |||
| 350 | <fieldset class="jp-ms-data-time"> |
||
| 351 | <legend><?php esc_html_e( 'Time', 'jetpack' ); ?></legend> |
||
| 352 | |||
| 353 | <label for="<?php echo $this->get_field_id( 'hour' ); ?>" class="assistive-text"><?php _e( 'Hour', 'jetpack' ); ?></label> |
||
| 354 | <input id="<?php echo $this->get_field_id( 'hour' ); ?>" class="hour" name="<?php echo $this->get_field_name( 'hour' ); ?>" type="text" value="<?php echo esc_attr( $instance['hour'] ); ?>"> |
||
| 355 | |||
| 356 | <label for="<?php echo $this->get_field_id( 'min' ); ?>" class="assistive-text"><?php _e( 'Minutes', 'jetpack' ); ?></label> |
||
| 357 | |||
| 358 | <span class="time-separator">:</span> |
||
| 359 | |||
| 360 | <input id="<?php echo $this->get_field_id( 'min' ); ?>" class="minutes" name="<?php echo $this->get_field_name( 'min' ); ?>" type="text" value="<?php echo esc_attr( $instance['min'] ); ?>"> |
||
| 361 | </fieldset> |
||
| 362 | |||
| 363 | <fieldset class="jp-ms-data-unit"> |
||
| 364 | <legend><?php esc_html_e( 'Time Unit', 'jetpack' ); ?></legend> |
||
| 365 | |||
| 366 | <label for="<?php echo $this->get_field_id( 'unit' ); ?>" class="assistive-text"> |
||
| 367 | <?php _e( 'Time Unit', 'jetpack' ); ?> |
||
| 368 | </label> |
||
| 369 | <select id="<?php echo $this->get_field_id( 'unit' ); ?>" class="unit" name="<?php echo $this->get_field_name( 'unit' ); ?>"> |
||
| 370 | <?php |
||
| 371 | foreach ( $units as $key => $unit ) { |
||
| 372 | echo '<option value="' . esc_attr( $key ) . '"' . selected( $key, $instance['unit'], false ) . '>' . $unit . '</option>'; |
||
| 373 | } |
||
| 374 | ?></select> |
||
| 375 | </fieldset> |
||
| 376 | |||
| 377 | <ul class="milestone-type"> |
||
| 378 | <li> |
||
| 379 | <label> |
||
| 380 | <input |
||
| 381 | <?php checked( $instance['type'], 'until' ); ?> |
||
| 382 | name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>" |
||
| 383 | type="radio" |
||
| 384 | value="until" |
||
| 385 | /> |
||
| 386 | <?php esc_html_e( 'Until your milestone', 'jetpack' ); ?> |
||
| 387 | </label> |
||
| 388 | </li> |
||
| 412 |
This check marks private properties in classes that are never used. Those properties can be removed.