Completed
Push — master ( 50ebea...f30043 )
by Andrew
02:27
created

ClassyHierarchy::check_request()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 21
c 1
b 0
f 0
nc 18
nop 0
dl 0
loc 43
rs 4.947

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Implements Wordpress Hierarchy 
5
 */
6
7
class ClassyHierarchy {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
	/**
10
	 * Stores current request for multiple use
11
	 * 
12
	 * @var string/null
13
	 */
14
	protected static $current_request = null;
15
16
17
	/**
18
	 * Protected function to get current request type
19
	 * 
20
	 * @return string
21
	 */
22
	protected static function check_request() {
23
24
		if ( is_404()) : return '404';
25
26
		elseif ( is_search() ) : return 'search';
27
28
		elseif ( is_front_page() ) : return 'front-page';
29
30
		elseif ( is_home() ) : return 'home';
31
32
		elseif ( is_post_type_archive() ) : return 'post_type_archive';
33
34
		elseif ( is_tax() ) : return 'taxonomy';
35
36
		elseif ( is_attachment() ) : return 'attachment';
37
38
		elseif ( is_single() ) : return 'single';
39
40
		elseif ( self::is_classy_template() ) : return 'classy-template';
41
42
		elseif ( is_page() ) : return 'page';
43
44
		elseif ( is_singular() ) : return 'singular';
45
46
		elseif ( is_category() ) : return 'category';
47
48
		elseif ( is_tag() ) : return 'tag';
49
50
		elseif ( is_author() ) : return 'author';
51
52
		elseif ( is_date() ) : return 'date';
53
54
		elseif ( is_archive() ) : return 'archive';
55
56
		elseif ( is_paged() ) : return 'paged';
57
58
		else :
59
		
60
			return 'index';
61
62
		endif;
63
64
	}
65
66
	/**
67
	 * Returns current request type
68
	 * 
69
	 * @return string
70
	 */
71
	public static function get_current_request() {
72
73
		if ( null === self::$current_request ) {
74
75
			self::$current_request = self::check_request();
76
77
		}
78
79
		return self::$current_request;
80
81
	}
82
83
84
	/**
85
	 * Returns file's absolute path
86
	 * 
87
	 * @param  string $type     template/scope
88
	 * @param  string $template path to template ex: "post/archive"
89
	 * @return string           full fule path
90
	 */
91
	public static function get_file_path($type = 'template', $template) {
92
93
		if ($type == 'template') {
94
95
			$folder = ClassyTemplate::$folder;
96
97
			return THEME_PATH . $folder . '/' . $template . '.blade.php';			
98
		
99
		} elseif ($type == 'scope') {
100
101
			$folder = ClassyScope::$folder;
102
103
			return THEME_PATH . $folder . '/' . $template . '.php';			
104
105
		}
106
107
	}
108
109
110
	/**
111
	 * Checks if template exists
112
	 * 
113
	 * @param  string $type template/scope
114
	 * @param  string $template in blade path format, ex: layout/header
0 ignored issues
show
Bug introduced by
There is no parameter named $template. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
	 * @return boolean true/false
116
	 */
117
	public static function file_exists($type = 'template', $file) {
118
119
		$file = str_replace('.', '/', $file);
120
121
		$file_path = self::get_file_path($type, $file);
122
123
		return file_exists($file_path);
124
125
	}
126
127
128
	/**
129
	 * Returns template name for render, based on type of request
130
	 *
131
	 * @param  string $type template/scope
132
	 * @param  string $type 
133
	 * @return array 
134
	 */
135
	public static function get_available_file($type = 'template', $page) {
136
137
		$templates = self::get_request_templates_list($page);
138
139
		foreach ($templates as $template) {
140
141
			if ( self::file_exists($type, $template) ):
142
143
				return $template;
144
145
			endif;
146
147
		}
148
149
		return false;
150
151
	}
152
153
154
	/**
155
	 * Returns list of templates to check, based on type of request
156
	 * 
157
	 * @param  string $type 
158
	 * @return array
159
	 */
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 ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
365
366
	/**
367
	 * Checks if this is classy custom template
368
	 * 
369
	 * @return boolean
370
	 */
371
	public static function is_classy_template() {
372
373
		return self::get_classy_template() ? true : false;
374
	}
375
376
377
	/**
378
	 * Returns classy template name or boolean if this is not classy template
379
	 * 
380
	 * @return mixed
381
	 */
382
	public static function get_classy_template() {
383
384
		$template_slug = get_page_template_slug();
385
386
		preg_match('/classy\-(.*)/', $template_slug, $matches);
387
388
		if ( $matches && isset($matches[1]) ) return $matches[1];
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
389
390
		return false;
391
392
	}
393
394
}