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

ClassyTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 74
rs 10
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_template() 0 11 1
A get_blade_template() 0 5 1
B get_page_templates_list() 0 27 4
1
<?php 
2
3
/**
4
 * Template Loader
5
 *
6
 * Loads the corresponding template based on request
7
 */
8
class ClassyTemplate {
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...
9
10
	/**
11
	 * Theme twig templates folder
12
	 * 
13
	 * @var string
14
	 */
15
	public static $folder = 'views';
16
17
	/**
18
	 * Returns template to show based on request value
19
	 * 
20
	 * @return string
21
	 */
22
	public static function get_template() {
23
24
		$request = ClassyHierarchy::get_current_request();
25
26
		$file = ClassyHierarchy::get_available_file('template', $request);
27
28
		$template = self::get_blade_template($file);
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by \ClassyHierarchy::get_av...e('template', $request) on line 26 can also be of type false; however, ClassyTemplate::get_blade_template() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
29
30
		return $template;
31
32
	}
33
34
35
	/**
36
	 * Replaces all slashes with dots
37
	 * 
38
	 * @param  string $template
39
	 * @return string           
40
	 */
41
	public static function get_blade_template($template) {
42
43
		return str_replace('/', '.', $template);
44
45
	}
46
47
	
48
	/**
49
	 * Returns list of theme page templates
50
	 * 
51
	 * @return array
52
	 */
53
	public static function get_page_templates_list() {
54
55
		$templates = array();
56
		
57
	    $files = (array) glob( THEME_PATH . '/' . self::$folder . '/*/*.blade.php' );
58
59
		foreach ( $files as $filename ) {
60
			
61
			if ( !empty($filename) ) {
62
63
				if ( ! preg_match( '/\{\{\-\-\s*Template Name:(.*)\s*\-\-\}\}/mi', file_get_contents( $filename ), $header ) ) continue;
64
65
				$template_name = trim($header[1]);
66
67
				preg_match('/\/([^\/]*)\.blade.php$/is', $filename, $filename_match);
68
69
				$template_file = 'classy-' . $filename_match[1];
70
71
				$templates[$template_file] = $template_name;
72
				
73
			}
74
75
		}
76
77
		return $templates;
78
79
	}
80
81
}