| Conditions | 8 |
| Paths | 48 |
| Total Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 56 | public function get_items( $request ) { |
||
| 57 | $page = $request->get_param( 'page' ); |
||
| 58 | if ( ! isset( $page ) ) { |
||
| 59 | $page = 1; |
||
| 60 | } |
||
| 61 | $next_page = $page + 1; |
||
| 62 | $exclude_ids = $request->get_param( 'exclude_ids' ); |
||
| 63 | if ( ! isset( $exclude_ids ) ) { |
||
| 64 | $exclude_ids = array(); |
||
| 65 | } |
||
| 66 | $params = $request->get_params(); |
||
| 67 | if ( ! isset( $params ) ) { |
||
| 68 | $params = array(); |
||
| 69 | } |
||
| 70 | $attributes = wp_parse_args( |
||
| 71 | $params, |
||
| 72 | wp_list_pluck( $this->get_attribute_schema(), 'default' ) |
||
| 73 | ); |
||
| 74 | |||
| 75 | $article_query_args = Newspack_Blocks::build_articles_query( $attributes ); |
||
|
|
|||
| 76 | |||
| 77 | $query = array_merge( |
||
| 78 | $article_query_args, |
||
| 79 | array( |
||
| 80 | 'post__not_in' => $exclude_ids, |
||
| 81 | ) |
||
| 82 | ); |
||
| 83 | |||
| 84 | // Run Query. |
||
| 85 | $article_query = new WP_Query( $query ); |
||
| 86 | |||
| 87 | // Defaults. |
||
| 88 | $items = array(); |
||
| 89 | $ids = array(); |
||
| 90 | $next_url = ''; |
||
| 91 | |||
| 92 | // The Loop. |
||
| 93 | while ( $article_query->have_posts() ) { |
||
| 94 | $article_query->the_post(); |
||
| 95 | $html = Newspack_Blocks::template_inc( |
||
| 96 | __DIR__ . '/templates/article.php', |
||
| 97 | array( |
||
| 98 | 'attributes' => $attributes, |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | |||
| 102 | if ( $request->get_param( 'amp' ) ) { |
||
| 103 | $html = $this->generate_amp_partial( $html ); |
||
| 104 | } |
||
| 105 | $items[]['html'] = $html; |
||
| 106 | $ids[] = get_the_ID(); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Provide next URL if there are more pages. |
||
| 110 | if ( $next_page <= $article_query->max_num_pages ) { |
||
| 111 | $next_url = add_query_arg( |
||
| 112 | array_merge( |
||
| 113 | array_map( |
||
| 114 | function ( $attribute ) { |
||
| 115 | return false === $attribute ? '0' : str_replace( '#', '%23', $attribute ); |
||
| 116 | }, |
||
| 117 | $attributes |
||
| 118 | ), |
||
| 119 | array( |
||
| 120 | 'exclude_ids' => false, |
||
| 121 | 'page' => $next_page, |
||
| 122 | 'amp' => $request->get_param( 'amp' ), |
||
| 123 | ) |
||
| 124 | ), |
||
| 125 | rest_url( '/newspack-blocks/v1/articles' ) |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | return rest_ensure_response( |
||
| 130 | array( |
||
| 131 | 'items' => $items, |
||
| 132 | 'ids' => $ids, |
||
| 133 | 'next' => $next_url, |
||
| 134 | ) |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 189 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.