Test Failed
Push — master ( 64f1fb...87da03 )
by Joe
01:58
created

Loader::get_routes()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 8.8571
1
<?php
2
3
namespace MyAdmin\Plugins;
4
5
/**
6
 * Class Loader
7
 *
8
 * Here are some of the regexes ive used to change code to using this class:
9
 *  ('[^']*') *=> *('[^']*'),
10
 *  $loader->add_requirement(\1, \2);
11
 *
12
 * @package MyAdmin
13
 */
14
class Loader {
0 ignored issues
show
Coding Style introduced by
The property $admin_routes is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $public_routes is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
15
	protected $requirements;
16
	protected $routes;
17
	protected $admin_routes;
18
	protected $public_routes;
19
20
	/**
21
	 * Loader constructor.
22
	 */
23
	public function __construct() {
24
		$this->requirements = [];
25
		$this->routes = [];
26
		$this->admin_routes = [];
27
		$this->public_routes = [];
28
	}
29
30
	/**
31
	 * gets the page routes
32
	 *
33
	 * @param bool $include_admin
34
	 * @return array of routes
35
	 */
36
	public function get_routes($include_admin = FALSE) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $include_admin is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
37
		//if ($include_admin === FALSE && $GLOBALS['tf']->ima === 'admin')
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
			//$include_admin = TRUE;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
		$routes = array_merge($this->public_routes, $this->routes);
40
		if ($include_admin === TRUE)
41
			$routes = array_merge($this->admin_routes, $routes);
42
		uksort($routes, function($a, $b) {
43
			if (strlen($a) == strlen($b)) {
44
				if ($a == $b)
45
					return 0;
46
				return ($a > $b) ? -1 : 1;
47
			} else
48
				return (strlen($a) > strlen($b)) ? -1 : 1;
49
		});
50
		//myadmin_log('route', 'debug', json_encode($routes), __LINE__, __FILE__);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
		return $routes;
52
	}
53
54
	/**
55
	 * gets the admin page routes
56
	 *
57
	 * @return array of routes
58
	 */
59
	public function get_admin_routes() {
60
		return $this->admin_routes;
61
	}
62
63
	/**
64
	 * gets the public page routes
65
	 *
66
	 * @return array of routes
67
	 */
68
	public function get_public_routes() {
69
		return $this->public_routes;
70
	}
71
72
	/**
73
	 * returns the value of a setting
74
	 *
75
	 * @param string $setting
76
	 * @return mixed the value of the setting
77
	 */
78
	public function get_setting($setting) {
79
		return constant($setting);
80
	}
81
82
	/**
83
	 * adds a requirement into the loader and registers it as a page with the router
84
	 *
85
	 * @param string $function php function name or class.class_name
86
	 * @param string $source php source file
87
	 * @param string $namespace optional php namespace
88
	 */
89
	public function add_page_requirement($function, $source, $namespace = '') {
90
		$this->routes['/'.$function] = $namespace.$function;
91
		$this->routes['/admin/'.$function] = $namespace.$function;
92
		$this->add_requirement($function, $source, $namespace);
93
	}
94
95
	/**
96
	 * adds a requirement into the loader and registers it as a page with the router
97
	 *
98
	 * @param string $function php function name or class.class_name
99
	 * @param string $source php source file
100
	 * @param string $namespace optional php namespace
101
	 */
102
	public function add_root_page_requirement($function, $source, $namespace = '') {
103
		$this->routes['/'.$function] = $namespace.$function;
104
		$this->add_requirement($function, $source, $namespace);
105
	}
106
107
	/**
108
	 * adds a requirement into the loader and registers it as a page with the router
109
	 *
110
	 * @param string $function php function name or class.class_name
0 ignored issues
show
Bug introduced by
There is no parameter named $function. 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...
111
	 * @param string $path source file path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. 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...
112
	 */
113
	public function add_public_path($page, $source) {
114
		$this->public_routes['/'.$page] = $source;
115
	}
116
117
	/**
118
	 * adds a requirement into the loader and registers it as a page with the router
119
	 *
120
	 * @param string $function php function name or class.class_name
121
	 * @param string $source php source file
122
	 * @param string $namespace optional php namespace
123
	 */
124
	public function add_ajax_page_requirement($function, $source, $namespace = '') {
125
		$this->routes['/ajax/'.$function] = $namespace.$function;
126
		$this->add_requirement($function, $source, $namespace);
127
	}
128
129
	/**
130
	 * adds a requirement into the loader and registers it as a page with the router
131
	 *
132
	 * @param string $function php function name or class.class_name
133
	 * @param string $source php source file
134
	 * @param string $namespace optional php namespace
135
	 */
136
	public function add_admin_page_requirement($function, $source, $namespace = '') {
137
		$this->admin_routes['/admin/'.$function] = $namespace.$function;
138
		$this->add_requirement($function, $source, $namespace);
139
	}
140
141
	/**
142
	 * adds a requirement into the loader
143
	 *
144
	 * @param string $function php function name or class.class_name
145
	 * @param string $source php source file
146
	 * @param string $namespace optional php namespace
147
	 */
148
	public function add_requirement($function, $source, $namespace = '') {
149
		$this->requirements[$function] = $namespace.$source;
150
	}
151
152
	/**
153
	 * gets an array of requirements for loading
154
	 *
155
	 * @return array the array of requirements
156
	 */
157
	public function get_requirements() {
158
		return $this->requirements;
159
	}
160
}
161
162