Passed
Pull Request — master (#15)
by Anton
03:07
created

Section::_section()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 7
nc 7
nop 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Frames\Admin
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Frames\Admin {
11
12
	use Frames, Frames\Status, Modules\Auth, Ajax, Request, Template;
13
14
	abstract class Section extends Frames\Section {
15
16
		# Define active section
17
18
		const SECTION = SECTION_ADMIN;
19
20
		# Define phrases list
21
22
		const PHRASES = ['Admin', 'Ajax', 'Common', 'Install', 'Mail', 'Menuitem', 'Page', 'Range', 'User', 'Variable', 'Widget'];
23
24
		# Page settings
25
26
		protected $_title = '';
27
28
		/**
29
		 * Handle a form area request
30
		 */
31
32
		private function handleFormArea() {
33
34
			# Check auth
35
36
			if (($this instanceof Area\Auth) && Auth::check()) Request::redirect(INSTALL_PATH . '/admin');
37
38
			# Handle request
39
40
			if (Template::isBlock($result = $this->handle())) return (new View\Form($this->_title))->display($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->handle() on line 40 can also be of type null; however, Frames\Admin\View\Form::display() does only seem to accept object<Template\Block>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41
42
			# ------------------------
43
44
			return Status::displayError404();
45
		}
46
47
		/**
48
		 * Handle a panel area request
49
		 */
50
51
		private function handlePanelArea() {
52
53
			# Check auth
54
55
			if (!Auth::check() || ((false !== Request::get('logout')) && Auth::logout())) {
56
57
				Request::redirect(INSTALL_PATH . '/admin/login');
58
			}
59
60
			# Handle request
61
62
			$request = (!Request::isSpecial('navigate') ? 'display' : 'navigate');
63
64
			$result = $this->handle(Request::isAjax() && ($request === 'display'));
0 ignored issues
show
Unused Code introduced by
The call to Section::handle() has too many arguments starting with \Request::isAjax() && $request === 'display'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
65
66
			if (Template::isBlock($result)) return (new View\Panel($this->_title))->$request($result);
67
68
			if (Ajax::isResponse($result)) return Ajax::output($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->handle(\Request::...$request === 'display') on line 64 can also be of type null or object<Template\Block>; however, Ajax::output() does only seem to accept object<Ajax\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
70
			# ------------------------
71
72
			return Status::displayError404();
73
		}
74
75
		/**
76
		 * The branch method for the admin section
77
		 */
78
79
		protected function _section() {
80
81
			# Check for restricted access
82
83
			if ('' !== CONFIG_ADMIN_IP) {
84
85
				$ips = preg_split('/ +/', CONFIG_ADMIN_IP, -1, PREG_SPLIT_NO_EMPTY);
86
87
				if (!in_array(REQUEST_CLIENT_IP, $ips, true)) return Status::displayError404();
88
			}
89
90
			# Handle request
91
92
			if (($this instanceof Area\Auth) || ($this instanceof Area\Install)) return $this->handleFormArea();
93
94
			if ($this instanceof Area\Panel) return $this->handlePanelArea();
95
96
			# ------------------------
97
98
			return Status::displayError404();
99
		}
100
	}
101
}
102