| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 160 |
| Lines | 6 |
| Ratio | 3.75 % |
| 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 |
||
| 20 | function newspack_blocks_render_block_homepage_articles( $attributes ) { |
||
| 21 | // Don't output the block inside RSS feeds. |
||
| 22 | if ( is_feed() ) { |
||
| 23 | return; |
||
| 24 | } |
||
| 25 | |||
| 26 | // This will let the FSE plugin know we need CSS/JS now. |
||
| 27 | do_action( 'newspack_blocks_render_homepage_articles' ); |
||
| 28 | |||
| 29 | $article_query = new \WP_Query( \Newspack_Blocks::build_articles_query( $attributes ) ); |
||
| 30 | |||
| 31 | $classes = \Newspack_Blocks::block_classes( 'homepage-articles', $attributes, array( 'wpnbha' ) ); |
||
| 32 | |||
| 33 | if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) { |
||
| 34 | $classes .= ' is-grid'; |
||
| 35 | } |
||
| 36 | if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) { |
||
| 37 | $classes .= ' columns-' . $attributes['columns']; |
||
| 38 | } |
||
| 39 | if ( $attributes['showImage'] ) { |
||
| 40 | $classes .= ' show-image'; |
||
| 41 | } |
||
| 42 | View Code Duplication | if ( $attributes['showImage'] && isset( $attributes['mediaPosition'] ) ) { |
|
| 43 | $classes .= ' image-align' . $attributes['mediaPosition']; |
||
| 44 | } |
||
| 45 | if ( isset( $attributes['typeScale'] ) ) { |
||
| 46 | $classes .= ' ts-' . $attributes['typeScale']; |
||
| 47 | } |
||
| 48 | View Code Duplication | if ( $attributes['showImage'] && isset( $attributes['imageScale'] ) ) { |
|
| 49 | $classes .= ' is-' . $attributes['imageScale']; |
||
| 50 | } |
||
| 51 | if ( $attributes['showImage'] ) { |
||
| 52 | $classes .= ' is-' . $attributes['imageShape']; |
||
| 53 | } |
||
| 54 | if ( $attributes['showImage'] && $attributes['mobileStack'] ) { |
||
| 55 | $classes .= ' mobile-stack'; |
||
| 56 | } |
||
| 57 | if ( $attributes['showCaption'] ) { |
||
| 58 | $classes .= ' show-caption'; |
||
| 59 | } |
||
| 60 | if ( $attributes['showCategory'] ) { |
||
| 61 | $classes .= ' show-category'; |
||
| 62 | } |
||
| 63 | if ( isset( $attributes['className'] ) ) { |
||
| 64 | $classes .= ' ' . $attributes['className']; |
||
| 65 | } |
||
| 66 | |||
| 67 | if ( '' !== $attributes['textColor'] || '' !== $attributes['customTextColor'] ) { |
||
| 68 | $classes .= ' has-text-color'; |
||
| 69 | } |
||
| 70 | if ( '' !== $attributes['textColor'] ) { |
||
| 71 | $classes .= ' has-' . $attributes['textColor'] . '-color'; |
||
| 72 | } |
||
| 73 | |||
| 74 | $styles = ''; |
||
| 75 | |||
| 76 | if ( '' !== $attributes['customTextColor'] ) { |
||
| 77 | $styles = 'color: ' . $attributes['customTextColor'] . ';'; |
||
| 78 | } |
||
| 79 | $articles_rest_url = add_query_arg( |
||
| 80 | array_merge( |
||
| 81 | array_map( |
||
| 82 | function ( $attribute ) { |
||
| 83 | return false === $attribute ? '0' : str_replace( '#', '%23', $attribute ); |
||
| 84 | }, |
||
| 85 | $attributes |
||
| 86 | ), |
||
| 87 | array( |
||
| 88 | 'page' => 2, |
||
| 89 | 'amp' => \Newspack_Blocks::is_amp(), |
||
| 90 | ) |
||
| 91 | ), |
||
| 92 | rest_url( '/newspack-blocks/v1/articles' ) |
||
| 93 | ); |
||
| 94 | |||
| 95 | $page = $article_query->paged; |
||
| 96 | if ( ! isset( $page ) ) { |
||
| 97 | $page = 1; |
||
| 98 | } |
||
| 99 | |||
| 100 | $has_more_pages = ( ++$page ) <= $article_query->max_num_pages; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Hide the "More" button on private sites. |
||
| 104 | * |
||
| 105 | * Client-side fetching from a private WP.com blog requires authentication, |
||
| 106 | * which is not provided in the current implementation. |
||
| 107 | * See https://github.com/Automattic/newspack-blocks/issues/306. |
||
| 108 | */ |
||
| 109 | $is_blog_private = (int) get_option( 'blog_public' ) === -1; |
||
| 110 | |||
| 111 | $has_more_button = ! $is_blog_private && $has_more_pages && (bool) $attributes['moreButton']; |
||
| 112 | |||
| 113 | if ( $has_more_button ) { |
||
| 114 | $classes .= ' has-more-button'; |
||
| 115 | } |
||
| 116 | |||
| 117 | ob_start(); |
||
| 118 | |||
| 119 | if ( $article_query->have_posts() ) : ?> |
||
| 120 | <?php if ( $has_more_button && \Newspack_Blocks::is_amp() ) : ?> |
||
| 121 | <amp-script layout="container" src="<?php echo esc_url( plugins_url( '/newspack-blocks/amp/homepage-articles/view.js' ) ); ?>"> |
||
| 122 | <?php endif; ?> |
||
| 123 | <div |
||
| 124 | class="<?php echo esc_attr( $classes ); ?>" |
||
| 125 | style="<?php echo esc_attr( $styles ); ?>" |
||
| 126 | > |
||
| 127 | <div data-posts data-current-post-id="<?php the_ID(); ?>"> |
||
| 128 | <?php if ( '' !== $attributes['sectionHeader'] ) : ?> |
||
| 129 | <h2 class="article-section-title"> |
||
| 130 | <span><?php echo wp_kses_post( $attributes['sectionHeader'] ); ?></span> |
||
| 131 | </h2> |
||
| 132 | <?php endif; ?> |
||
| 133 | <?php |
||
| 134 | echo wp_kses_post( |
||
| 135 | \Newspack_Blocks::template_inc( |
||
| 136 | __DIR__ . '/templates/articles-list.php', |
||
| 137 | array( |
||
|
|
|||
| 138 | 'articles_rest_url' => $articles_rest_url, |
||
| 139 | 'article_query' => $article_query, |
||
| 140 | 'attributes' => $attributes, |
||
| 141 | ) |
||
| 142 | ) |
||
| 143 | ); |
||
| 144 | ?> |
||
| 145 | </div> |
||
| 146 | <?php |
||
| 147 | |||
| 148 | if ( $has_more_button ) : |
||
| 149 | ?> |
||
| 150 | <button type="button" data-next="<?php echo esc_url( $articles_rest_url ); ?>"> |
||
| 151 | <?php |
||
| 152 | if ( ! empty( $attributes['moreButtonText'] ) ) { |
||
| 153 | echo esc_html( $attributes['moreButtonText'] ); |
||
| 154 | } else { |
||
| 155 | esc_html_e( 'Load more posts', 'jetpack' ); |
||
| 156 | } |
||
| 157 | ?> |
||
| 158 | </button> |
||
| 159 | <p class="loading"> |
||
| 160 | <?php esc_html_e( 'Loading…', 'jetpack' ); ?> |
||
| 161 | </p> |
||
| 162 | <p class="error"> |
||
| 163 | <?php esc_html_e( 'Something went wrong. Please refresh the page and/or try again.', 'jetpack' ); ?> |
||
| 164 | </p> |
||
| 165 | |||
| 166 | <?php endif; ?> |
||
| 167 | |||
| 168 | </div> |
||
| 169 | <?php if ( $has_more_button && \Newspack_Blocks::is_amp() ) : ?> |
||
| 170 | </amp-script> |
||
| 171 | <?php endif; ?> |
||
| 172 | <?php |
||
| 173 | endif; |
||
| 174 | |||
| 175 | $content = ob_get_clean(); |
||
| 176 | \Newspack_Blocks::enqueue_view_assets( 'homepage-articles' ); |
||
| 177 | |||
| 178 | return $content; |
||
| 179 | } |
||
| 180 | |||
| 293 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.