Conditions | 9 |
Paths | 64 |
Total Lines | 56 |
Code Lines | 29 |
Lines | 0 |
Ratio | 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 |
||
67 | public function widget( $args, $instance ) { |
||
68 | if ( ! isset( $args['widget_id'] ) ) |
||
69 | $args['widget_id'] = $this->id; |
||
70 | |||
71 | $output = ''; |
||
72 | |||
73 | $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' ); |
||
74 | |||
75 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
||
76 | $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
||
77 | |||
78 | $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; |
||
79 | if ( ! $number ) |
||
80 | $number = 5; |
||
81 | |||
82 | /** |
||
83 | * Filter the arguments for the Recent Comments widget. |
||
84 | * |
||
85 | * @since 3.4.0 |
||
86 | * |
||
87 | * @see WP_Comment_Query::query() for information on accepted arguments. |
||
88 | * |
||
89 | * @param array $comment_args An array of arguments used to retrieve the recent comments. |
||
90 | */ |
||
91 | $comments = get_comments( apply_filters( 'widget_comments_args', array( |
||
92 | 'number' => $number, |
||
93 | 'status' => 'approve', |
||
94 | 'post_status' => 'publish' |
||
95 | ) ) ); |
||
96 | |||
97 | $output .= $args['before_widget']; |
||
98 | if ( $title ) { |
||
99 | $output .= $args['before_title'] . $title . $args['after_title']; |
||
100 | } |
||
101 | |||
102 | $output .= '<ul id="recentcomments">'; |
||
103 | if ( is_array( $comments ) && $comments ) { |
||
104 | // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) |
||
105 | $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) ); |
||
106 | _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false ); |
||
107 | |||
108 | foreach ( (array) $comments as $comment ) { |
||
109 | $output .= '<li class="recentcomments">'; |
||
110 | /* translators: comments widget: 1: comment author, 2: post link */ |
||
111 | $output .= sprintf( _x( '%1$s on %2$s', 'widgets' ), |
||
112 | '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>', |
||
113 | '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>' |
||
114 | ); |
||
115 | $output .= '</li>'; |
||
116 | } |
||
117 | } |
||
118 | $output .= '</ul>'; |
||
119 | $output .= $args['after_widget']; |
||
120 | |||
121 | echo $output; |
||
122 | } |
||
123 | |||
174 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: