View   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_view() 0 11 1
A get_blade_view() 0 5 1
B get_page_templates_list() 0 26 4
1
<?php
2
/**
3
 * View Loader.
4
 * Loads the corresponding template based on request.
5
 *
6
 * @package Classy
7
 */
8
9
namespace Classy;
10
11
/**
12
 * Class View.
13
 */
14
class View {
15
16
	/**
17
	 * Views folder.
18
	 *
19
	 * @var string
20
	 */
21
	public static $folder = 'views';
22
23
	/**
24
	 * Returns view name to show based on request value.
25
	 *
26
	 * @return string
27
	 */
28
	public static function get_view() {
29
30
		$request = Hierarchy::get_current_request();
31
32
		$file = Hierarchy::get_available_file( 'view', $request );
33
34
		$view = self::get_blade_view( $file );
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by \Classy\Hierarchy::get_a..._file('view', $request) on line 32 can also be of type false; however, Classy\View::get_blade_view() 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...
35
36
		return $view;
37
38
	}
39
40
	/**
41
	 * Replaces all slashes with dots.
42
	 *
43
	 * @param string $view View's name.
44
	 *
45
	 * @return string
46
	 */
47
	public static function get_blade_view( $view ) {
48
49
		return str_replace( '/', '.', $view );
50
51
	}
52
53
	/**
54
	 * Returns list of theme page templates.
55
	 *
56
	 * @return array
57
	 */
58
	public static function get_page_templates_list() {
59
60
		$templates = array();
61
62
		$files = (array) glob( CLASSY_THEME_PATH . '/' . self::$folder . '/*/*.blade.php' );
63
64
		foreach ( $files as $filename ) {
65
66
			if ( ! empty( $filename ) ) {
67
68
				if ( ! preg_match( '/\{\{\-\-\s*Template Name:(.*)\s*\-\-\}\}/mi', file_get_contents( $filename ), $header ) ) { continue; }
69
70
				$template_name = trim( $header[1] );
71
72
				preg_match( '/\/([^\/]*)\.blade.php$/is', $filename, $filename_match );
73
74
				$template_file = 'classy-' . $filename_match[1];
75
76
				$templates[ $template_file ] = $template_name;
77
78
			}
79
		}
80
81
		return $templates;
82
83
	}
84
}
85