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