Conditions | 26 |
Paths | 43 |
Total Lines | 204 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
160 | private static function get_request_templates_list($type) { |
||
161 | |||
162 | $templates = array(); |
||
163 | |||
164 | // Home |
||
165 | |||
166 | if ( $type == 'home' ) : |
||
167 | |||
168 | $templates[] = 'home'; |
||
169 | |||
170 | // Single |
||
171 | |||
172 | elseif ( $type == 'single' ) : |
||
173 | |||
174 | $post_type = get_post_type(); |
||
175 | |||
176 | $templates[] = $post_type . '.single'; |
||
177 | |||
178 | $templates[] = 'single'; |
||
179 | |||
180 | // Post type |
||
181 | |||
182 | elseif ( $type == 'post_type_archive' ) : |
||
183 | |||
184 | $post_type = get_post_type(); |
||
185 | |||
186 | $templates[] = $post_type . '.archive'; |
||
187 | |||
188 | $templates[] = 'archive'; |
||
189 | |||
190 | |||
191 | // Taxonomy |
||
192 | |||
193 | elseif ( $type == 'taxonomy' ): |
||
194 | |||
195 | $term = get_queried_object(); |
||
196 | |||
197 | if ( ! empty( $term->slug ) ) { |
||
198 | |||
199 | $taxonomy = $term->taxonomy; |
||
200 | |||
201 | $templates[] = "taxonomy.$taxonomy-{$term->slug}"; |
||
202 | $templates[] = "taxonomy.$taxonomy"; |
||
203 | |||
204 | } |
||
205 | |||
206 | $templates[] = 'taxonomy.taxonomy'; |
||
207 | |||
208 | $templates[] = 'taxonomy'; |
||
209 | |||
210 | // Category |
||
211 | |||
212 | elseif ( $type == 'category' ): |
||
213 | |||
214 | $category = get_queried_object(); |
||
215 | |||
216 | if ( ! empty( $category->slug ) ) { |
||
217 | $templates[] = "category.{$category->slug}"; |
||
218 | $templates[] = "category.{$category->term_id}"; |
||
219 | } |
||
220 | |||
221 | $templates[] = 'category.category'; |
||
222 | |||
223 | $templates[] = 'category'; |
||
224 | |||
225 | |||
226 | // Attachment |
||
227 | |||
228 | elseif ( $type == 'attachment' ): |
||
229 | |||
230 | $attachment = get_queried_object(); |
||
231 | |||
232 | if ( $attachment ) { |
||
233 | |||
234 | if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
||
235 | |||
236 | list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
||
237 | |||
238 | } else { |
||
239 | |||
240 | list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
||
241 | |||
242 | } |
||
243 | |||
244 | if ( ! empty( $subtype ) ) { |
||
245 | $templates[] = "attachment.{$type}.{$subtype}"; |
||
246 | $templates[] = "attachment.{$subtype}"; |
||
247 | |||
248 | $templates[] = "{$type}.{$subtype}"; |
||
249 | $templates[] = "{$subtype}"; |
||
250 | } |
||
251 | |||
252 | $templates[] = "attachment.{$type}"; |
||
253 | $templates[] = "{$type}"; |
||
254 | |||
255 | } |
||
256 | |||
257 | $templates[] = 'attachment.attachment'; |
||
258 | |||
259 | $templates[] = 'attachment'; |
||
260 | |||
261 | |||
262 | // Tag |
||
263 | |||
264 | elseif ( $type == 'tag' ): |
||
265 | |||
266 | $tag = get_queried_object(); |
||
267 | |||
268 | if ( ! empty( $tag->slug ) ) { |
||
269 | $templates[] = "post.tag.{$tag->slug}"; |
||
270 | $templates[] = "post.tag.{$tag->term_id}"; |
||
271 | |||
272 | $templates[] = "tag.{$tag->slug}"; |
||
273 | $templates[] = "tag.{$tag->term_id}"; |
||
274 | } |
||
275 | $templates[] = 'post.tag'; |
||
276 | |||
277 | $templates[] = 'tag'; |
||
278 | |||
279 | |||
280 | // Author |
||
281 | |||
282 | elseif ( $type == 'author' ): |
||
283 | |||
284 | $author = get_queried_object(); |
||
285 | |||
286 | if ( $author instanceof WP_User ) { |
||
287 | $templates[] = "post.author.{$author->user_nicename}"; |
||
288 | $templates[] = "post.author.{$author->ID}"; |
||
289 | |||
290 | $templates[] = "author.{$author->user_nicename}"; |
||
291 | $templates[] = "author.{$author->ID}"; |
||
292 | } |
||
293 | |||
294 | $templates[] = 'post.author'; |
||
295 | |||
296 | $templates[] = 'author'; |
||
297 | |||
298 | |||
299 | // Front Page |
||
300 | |||
301 | elseif ( $type == 'front-page' ): |
||
302 | |||
303 | $templates[] = 'front-page.front-page'; |
||
304 | $templates[] = 'front-page'; |
||
305 | |||
306 | $templates[] = 'home.home'; |
||
307 | $templates[] = 'home'; |
||
308 | |||
309 | $templates = array_merge($templates, self::get_request_templates_list('post_type_archive')); |
||
310 | |||
311 | // Page |
||
312 | |||
313 | elseif ( $type == 'classy-template' ): |
||
314 | |||
315 | $template = self::get_classy_template(); |
||
316 | |||
317 | $templates[] = $template; |
||
318 | |||
319 | $templates[] = 'page.' . $template; |
||
320 | |||
321 | $templates[] = 'template.' . $template; |
||
322 | |||
323 | |||
324 | elseif ( $type == 'page' ): |
||
325 | |||
326 | $id = get_queried_object_id(); |
||
327 | |||
328 | $template = get_post_meta('theme-page-template', $id); |
||
329 | |||
330 | $pagename = get_query_var('pagename'); |
||
331 | |||
332 | if ( ! $pagename && $id ) { |
||
333 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
||
334 | $post = get_queried_object(); |
||
335 | if ( $post ) |
||
336 | $pagename = $post->post_name; |
||
337 | } |
||
338 | |||
339 | if ( $template && $template != 'index' ) |
||
340 | $templates[] = $template; |
||
341 | if ( $pagename ) |
||
342 | $templates[] = "page.$pagename"; |
||
343 | if ( $id ) |
||
344 | $templates[] = "page.$id"; |
||
345 | |||
346 | $templates[] = 'page.page'; |
||
347 | |||
348 | $templates[] = 'page'; |
||
349 | |||
350 | |||
351 | // Default |
||
352 | |||
353 | else: |
||
354 | |||
355 | $templates[] = $type; |
||
356 | |||
357 | endif; |
||
358 | |||
359 | $templates[] = 'index'; |
||
360 | |||
361 | return $templates; |
||
362 | |||
363 | } |
||
364 | |||
394 | } |
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.