| Conditions | 30 |
| Paths | 65 |
| Total Lines | 181 |
| Code Lines | 84 |
| Lines | 62 |
| Ratio | 34.25 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 82 | private static function get_request_templates_list($type) { |
||
| 83 | |||
| 84 | $templates = array(); |
||
| 85 | |||
| 86 | |||
| 87 | // Home |
||
| 88 | |||
| 89 | if ( $type == 'home' ) : |
||
| 90 | |||
| 91 | $templates[] = 'home'; |
||
| 92 | $templates[] = 'index'; |
||
| 93 | |||
| 94 | |||
| 95 | // Single |
||
| 96 | |||
| 97 | elseif ( $type == 'single' ) : |
||
| 98 | |||
| 99 | $post_type = get_query_var( 'post_type' ); |
||
| 100 | |||
| 101 | $templates[] = 'single-' . $post_type; |
||
| 102 | |||
| 103 | $templates[] = 'single'; |
||
| 104 | |||
| 105 | // Post type |
||
| 106 | |||
| 107 | elseif ( $type == 'post_type_archive' ) : |
||
| 108 | |||
| 109 | $post_type = get_query_var( 'post_type' ); |
||
| 110 | |||
| 111 | $templates[] = 'archive-' . $post_type; |
||
| 112 | |||
| 113 | $templates[] = 'archive'; |
||
| 114 | |||
| 115 | |||
| 116 | // Taxonomy |
||
| 117 | |||
| 118 | elseif ( $type == 'taxonomy' ): |
||
| 119 | |||
| 120 | $term = get_queried_object(); |
||
| 121 | |||
| 122 | if ( ! empty( $term->slug ) ) { |
||
| 123 | |||
| 124 | $taxonomy = $term->taxonomy; |
||
| 125 | |||
| 126 | $templates[] = "taxonomy-$taxonomy-{$term->slug}"; |
||
| 127 | $templates[] = "taxonomy-$taxonomy"; |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | $templates[] = 'taxonomy'; |
||
| 132 | |||
| 133 | // Category |
||
| 134 | |||
| 135 | View Code Duplication | elseif ( $type == 'category' ): |
|
| 136 | |||
| 137 | $category = get_queried_object(); |
||
| 138 | |||
| 139 | if ( ! empty( $category->slug ) ) { |
||
| 140 | $templates[] = "category-{$category->slug}"; |
||
| 141 | $templates[] = "category-{$category->term_id}"; |
||
| 142 | } |
||
| 143 | $templates[] = 'category'; |
||
| 144 | |||
| 145 | |||
| 146 | // Attachment |
||
| 147 | |||
| 148 | elseif ( $type == 'attachment' ): |
||
| 149 | |||
| 150 | $attachment = get_queried_object(); |
||
| 151 | |||
| 152 | if ( $attachment ) { |
||
| 153 | |||
| 154 | if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
||
| 155 | |||
| 156 | list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
||
| 157 | |||
| 158 | } else { |
||
| 159 | |||
| 160 | list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | if ( ! empty( $subtype ) ) { |
||
| 165 | $templates[] = "{$type}-{$subtype}"; |
||
| 166 | $templates[] = "{$subtype}"; |
||
| 167 | } |
||
| 168 | $templates[] = "{$type}"; |
||
| 169 | |||
| 170 | } |
||
| 171 | $templates[] = 'attachment'; |
||
| 172 | |||
| 173 | |||
| 174 | // Tag |
||
| 175 | |||
| 176 | View Code Duplication | elseif ( $type == 'tag' ): |
|
| 177 | |||
| 178 | $tag = get_queried_object(); |
||
| 179 | |||
| 180 | if ( ! empty( $tag->slug ) ) { |
||
| 181 | $templates[] = "tag-{$tag->slug}"; |
||
| 182 | $templates[] = "tag-{$tag->term_id}"; |
||
| 183 | } |
||
| 184 | $templates[] = 'tag'; |
||
| 185 | |||
| 186 | |||
| 187 | // Author |
||
| 188 | |||
| 189 | elseif ( $type == 'author' ): |
||
| 190 | |||
| 191 | $author = get_queried_object(); |
||
| 192 | |||
| 193 | if ( $author instanceof WP_User ) { |
||
| 194 | $templates[] = "author-{$author->user_nicename}"; |
||
| 195 | $templates[] = "author-{$author->ID}"; |
||
| 196 | } |
||
| 197 | $templates[] = 'author'; |
||
| 198 | |||
| 199 | |||
| 200 | // Front Page |
||
| 201 | |||
| 202 | View Code Duplication | elseif ( $type == 'front-page' ): |
|
| 203 | |||
| 204 | $id = get_queried_object_id(); |
||
| 205 | |||
| 206 | $pagename = get_query_var('pagename'); |
||
| 207 | |||
| 208 | if ( ! $pagename && $id ) { |
||
| 209 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
||
| 210 | $post = get_queried_object(); |
||
| 211 | if ( $post ) |
||
| 212 | $pagename = $post->post_name; |
||
| 213 | } |
||
| 214 | |||
| 215 | $template = get_post_meta('theme-page-template', $id); |
||
| 216 | |||
| 217 | if ( $template != 'index' ) |
||
| 218 | $templates[] = $template; |
||
| 219 | if ( $pagename ) |
||
| 220 | $templates[] = "page-$pagename"; |
||
| 221 | if ( $id ) |
||
| 222 | $templates[] = "page-$id"; |
||
| 223 | $templates[] = ''; |
||
| 224 | |||
| 225 | // Page |
||
| 226 | |||
| 227 | View Code Duplication | elseif ( $type == 'page' ): |
|
| 228 | |||
| 229 | $id = get_queried_object_id(); |
||
| 230 | |||
| 231 | $template = get_post_meta('theme-page-template', $id); |
||
| 232 | |||
| 233 | $pagename = get_query_var('pagename'); |
||
| 234 | |||
| 235 | if ( ! $pagename && $id ) { |
||
| 236 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
||
| 237 | $post = get_queried_object(); |
||
| 238 | if ( $post ) |
||
| 239 | $pagename = $post->post_name; |
||
| 240 | } |
||
| 241 | |||
| 242 | if ( $template != 'index' ) |
||
| 243 | $templates[] = $template; |
||
| 244 | if ( $pagename ) |
||
| 245 | $templates[] = "page-$pagename"; |
||
| 246 | if ( $id ) |
||
| 247 | $templates[] = "page-$id"; |
||
| 248 | $templates[] = 'page'; |
||
| 249 | |||
| 250 | |||
| 251 | // Default |
||
| 252 | |||
| 253 | else: |
||
| 254 | |||
| 255 | $templates[] = $type; |
||
| 256 | |||
| 257 | endif; |
||
| 258 | |||
| 259 | |||
| 260 | return $templates; |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 403 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.