Scope   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
dl 0
loc 85
rs 10
c 4
b 1
f 1
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_scope() 0 13 2
A extend_scope() 0 3 1
A get_common_scope() 0 7 2
A require_scope() 0 15 3
1
<?php
2
/**
3
 * View's scope.
4
 *
5
 * Loads the scope (data).
6
 *
7
 * @package Classy
8
 */
9
10
namespace Classy;
11
12
/**
13
 * Class Scope.
14
 */
15
class Scope {
16
17
	/**
18
	 * Common Scope
19
	 *
20
	 * @var null|array
21
	 */
22
	protected static $common = null;
23
24
	/**
25
	 * Folder with scopes.
26
	 *
27
	 * @var string
28
	 */
29
	public static $folder = 'scope';
30
31
	/**
32
	 * Returns request scope.
33
	 *
34
	 * @param string|null $view_name View's name.
35
	 *
36
	 * @return array
37
	 */
38
	public static function get_scope( $view_name = null ) {
39
		$scope = self::get_common_scope();
40
41
		if ( is_string( $view_name ) ) {
42
			$scope = self::extend_scope( $scope, $view_name );
43
		} else {
44
			$request = Hierarchy::get_current_request();
45
			$file = Hierarchy::get_available_file( 'scope', $request );
46
			$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 45 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...
47
		}
48
49
		return $scope;
50
	}
51
52
	/**
53
	 * Extends Scope with scope that is defined in theme_name/scope folder.
54
	 *
55
	 * @param array  $scope		Data for view file.
56
	 * @param string $view_name View's name.
57
	 *
58
	 * @return array
59
	 */
60
	public static function extend_scope( $scope, $view_name ) {
61
		return array_merge( $scope, self::require_scope( $view_name ) );
62
	}
63
64
	/**
65
	 * Returns Common Scope.
66
	 *
67
	 * @return array
68
	 */
69
	public static function get_common_scope() {
70
		if ( is_null( self::$common ) ) {
71
			self::$common = self::require_scope( 'common' );
72
		}
73
74
		return self::$common;
75
	}
76
77
	/**
78
	 * Requires file's scope.
79
	 *
80
	 * @param  string $filename View's file name.
81
	 *
82
	 * @return array
83
	 */
84
	public static function require_scope( $filename ) {
85
		$_return = array();
86
87
		$file = Hierarchy::get_file_path( 'scope', $filename );
88
89
		if ( file_exists( $file ) ) {
90
			require $file;
91
		}
92
93
		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...
94
			$_return = $data;
95
		}
96
97
		return $_return;
98
	}
99
}
100