| Conditions | 23 |
| Paths | 330 |
| Total Lines | 111 |
| Code Lines | 66 |
| Lines | 21 |
| Ratio | 18.92 % |
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 |
||
| 175 | protected function load_component ( $instance ) {
|
||
| 176 | global $current_user; |
||
| 177 | |||
| 178 | $course_ids = array(); |
||
| 179 | if ( 'activecourses' == esc_attr( $instance['component'] ) ) {
|
||
| 180 | $courses = Sensei_Utils::sensei_check_for_activity( array( 'user_id' => $current_user->ID, 'type' => 'sensei_course_status', 'status' => 'in-progress' ), true ); |
||
| 181 | // Need to always return an array, even with only 1 item |
||
| 182 | if ( !is_array($courses) ) {
|
||
| 183 | $courses = array( $courses ); |
||
| 184 | } |
||
| 185 | foreach( $courses AS $course ) {
|
||
| 186 | $course_ids[] = $course->comment_post_ID; |
||
| 187 | } |
||
| 188 | } elseif( 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
||
| 189 | $courses = Sensei_Utils::sensei_check_for_activity( array( 'user_id' => $current_user->ID, 'type' => 'sensei_course_status', 'status' => 'complete' ), true ); |
||
| 190 | // Need to always return an array, even with only 1 item |
||
| 191 | if ( !is_array($courses) ) {
|
||
| 192 | $courses = array( $courses ); |
||
| 193 | } |
||
| 194 | foreach( $courses AS $course ) {
|
||
| 195 | $course_ids[] = $course->comment_post_ID; |
||
| 196 | } |
||
| 197 | } // End If Statement |
||
| 198 | |||
| 199 | $posts_array = array(); |
||
| 200 | |||
| 201 | // course_query() is buggy, it doesn't honour the 1st arg if includes are provided, so instead slice the includes |
||
| 202 | if ( !empty($instance['limit']) && intval( $instance['limit'] ) >= 1 && intval( $instance['limit'] ) < count($course_ids) ) {
|
||
| 203 | |||
| 204 | $course_ids = array_slice( $course_ids, 0, intval( $instance['limit'] ) ); // This does mean the order by is effectively ignored |
||
| 205 | |||
| 206 | } |
||
| 207 | |||
| 208 | if ( ! empty( $course_ids ) ) {
|
||
| 209 | |||
| 210 | $posts_array = Sensei()->course->course_query( intval( $instance['limit'] ), esc_attr( $instance['component'] ), $course_ids ); |
||
| 211 | |||
| 212 | } else {
|
||
| 213 | |||
| 214 | if ( 'activecourses' == esc_attr( $instance['component'] ) || 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
||
| 215 | $posts_array = array(); |
||
| 216 | |||
| 217 | } else {
|
||
| 218 | |||
| 219 | $course_args = array( |
||
| 220 | 'post_type' => 'course', |
||
| 221 | 'orderby' => 'date', |
||
| 222 | 'order' => 'DESC', |
||
| 223 | 'post_status' => 'publish', |
||
| 224 | 'posts_per_page' => $instance['limit'], |
||
| 225 | ); |
||
| 226 | |||
| 227 | $posts_array = get_posts( $course_args ); |
||
| 228 | } |
||
| 229 | |||
| 230 | } // End If Statement |
||
| 231 | |||
| 232 | if ( count( $posts_array ) > 0 ) { ?>
|
||
| 233 | <ul> |
||
| 234 | View Code Duplication | <?php foreach ($posts_array as $post_item){
|
|
| 235 | $post_id = absint( $post_item->ID ); |
||
| 236 | $post_title = $post_item->post_title; |
||
| 237 | $user_info = get_userdata( absint( $post_item->post_author ) ); |
||
| 238 | $author_link = get_author_posts_url( absint( $post_item->post_author ) ); |
||
| 239 | $author_display_name = $user_info->display_name; |
||
| 240 | $author_id = $post_item->post_author; |
||
| 241 | ?> |
||
| 242 | <li class="fix"> |
||
| 243 | <?php do_action( 'sensei_course_image', $post_id ); ?> |
||
| 244 | <a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>" title="<?php echo esc_attr( $post_title ); ?>"><?php echo $post_title; ?></a> |
||
| 245 | <br /> |
||
| 246 | <?php if ( isset( Sensei()->settings->settings[ 'course_author' ] ) && ( Sensei()->settings->settings[ 'course_author' ] ) ) { ?>
|
||
| 247 | <span class="course-author"><?php _e( 'by ', 'woothemes-sensei' ); ?><a href="<?php echo esc_url( $author_link ); ?>" title="<?php echo esc_attr( $author_display_name ); ?>"><?php echo esc_html( $author_display_name ); ?></a></span> |
||
| 248 | <br /> |
||
| 249 | <?php } // End If Statement ?> |
||
| 250 | <span class="course-lesson-count"><?php echo Sensei()->course->course_lesson_count( $post_id ) . ' ' . __( 'Lessons', 'woothemes-sensei' ); ?></span> |
||
| 251 | <br /> |
||
| 252 | <?php sensei_simple_course_price( $post_id ); ?> |
||
| 253 | </li> |
||
| 254 | <?php } // End For Loop ?> |
||
| 255 | <?php if ( 'activecourses' == esc_attr( $instance['component'] ) || 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
||
| 256 | $my_account_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] ); |
||
| 257 | echo '<li class="my-account fix"><a href="'. esc_url( get_permalink( $my_account_page_id ) ) .'">'.__('My Courses', 'woothemes-sensei').' <span class="meta-nav"></span></a></li>';
|
||
| 258 | } // End If Statement ?> |
||
| 259 | </ul> |
||
| 260 | <?php } else {
|
||
| 261 | // No posts returned. This means the user either has no active or no completed courses. |
||
| 262 | if( isset( $instance['component'] ) ) {
|
||
| 263 | |||
| 264 | if ( 'featuredcourses' == $instance['component'] ) {
|
||
| 265 | |||
| 266 | _e( 'You have no featured courses.', 'woothemes-sensei' ); |
||
| 267 | |||
| 268 | } elseif ( 'activecourses' == $instance['component'] ) {
|
||
| 269 | |||
| 270 | _e( 'You have no active courses.', 'woothemes-sensei' ); |
||
| 271 | |||
| 272 | } elseif ( 'completedcourses' == $instance['component'] ) {
|
||
| 273 | _e( 'You have no completed courses.', 'woothemes-sensei' ); |
||
| 274 | |||
| 275 | }else{
|
||
| 276 | |||
| 277 | _e( 'You have no courses.', 'woothemes-sensei' ); |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | } // end if compoenetn status instance |
||
| 282 | |||
| 283 | } // End If Statement |
||
| 284 | |||
| 285 | } // End load_component() |
||
| 286 | |||
| 287 | } // End Class |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.