Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Scope::get_common_scope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Classy;
4
5
/**
6
 * View's scope.
7
 *
8
 * Loads the scope (data).
9
 */
10
class Scope {
11
12
	protected static $common = null;
13
14
	public static $folder = 'scope';
15
16
	/**
17
	 * Returns request scope.
18
	 *
19
	 * @return array
20
	 */
21
22
	/**
23
	 * @todo: Write description here.
24
	 *
25
	 * @param string|null $view_name View's name.
26
	 *
27
	 * @return array
28
	 */
29
	public static function get_scope( $view_name = null ) {
30
31
		$scope = self::get_common_scope();
32
33
		if ( is_string( $view_name ) ) {
34
35
			$scope = self::extend_scope( $scope, $view_name );
36
37
		} else {
38
39
			$request = Hierarchy::get_current_request();
40
41
			$file = Hierarchy::get_available_file( 'scope', $request );
42
43
			$scope = self::extend_scope( $scope, $file );
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by \Classy\Hierarchy::get_a...file('scope', $request) on line 41 can also be of type false; however, Classy\Scope::extend_scope() 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...
44
45
		}
46
47
		return $scope;
48
49
	}
50
51
	/**
52
	 * Extends Scope with scope that is defined in theme_name/scope folder.
53
	 *
54
	 * @param array  $scope
55
	 * @param string $view_name View's name.
56
	 *
57
	 * @return array
58
	 */
59
	public static function extend_scope( $scope, $view_name ) {
60
61
		$scope = array_merge( $scope, self::require_scope( $view_name ) );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $scope. This often makes code more readable.
Loading history...
62
63
		return $scope;
64
65
	}
66
67
	/**
68
	 * Returns Common Scope.
69
	 *
70
	 * @return array
71
	 */
72
	public static function get_common_scope() {
73
74
		if ( null === self::$common ) {
75
76
			self::$common = self::require_scope( 'common' );
77
78
		}
79
80
		return self::$common;
81
82
	}
83
84
	/**
85
	 * Requires file's scope.
86
	 *
87
	 * @param  string $filename View's file name.
88
	 *
89
	 * @return array
90
	 */
91
	public static function require_scope( $filename ) {
92
93
		$_return = array();
94
95
		$file = Hierarchy::get_file_path( 'scope', $filename );
96
97
		if ( file_exists( $file ) ) {
98
99
			require $file;
100
101
		}
102
103
		if ( isset( $data ) ) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
104
105
			$_return = $data;
106
107
		}
108
109
		return $_return;
110
111
	}
112
}
113