Completed
Pull Request — master (#543)
by
unknown
09:12
created

indexController::doInit()   C

Complexity

Conditions 13
Paths 50

Size

Total Lines 57
Code Lines 40

Duplication

Lines 8
Ratio 14.04 %

Importance

Changes 0
Metric Value
cc 13
eloc 40
nc 50
nop 0
dl 8
loc 57
rs 6.5962
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Controller\FrontController;
13
use Xoops\Core\FixedGroups;
14
15
/**
16
 * XOOPS global entry
17
 *
18
 * @copyright       XOOPS Project (http://xoops.org)
19
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         core
21
 * @since           2.0.0
22
 * @author          Kazumi Ono <[email protected]>
23
 * @author          Skalpa Keo <[email protected]>
24
 * @author          Taiwen Jiang <[email protected]>
25
 * @version         $Id$
26
 */
27
28
$mainfile = __DIR__ . '/mainfile.php';
29
if (file_exists($mainfile)) {
30
    include $mainfile;
31
} elseif (file_exists(__DIR__ . '/install/index.php')) {
32
    header('Location: install/index.php');
33
    exit;
34
}
35
unset($mainfile);
36
37
class indexController extends FrontController
38
{
39
	public function doInit()
0 ignored issues
show
Coding Style introduced by
doInit uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
	{
41
		parent::doInit();
42
		
43
		$this->startEvent = 'core.index.start';
44
	
45
		//check if start page is defined
46
		$startpage = $this->xoops->getConfig('startpage');
47
		if ($this->xoops->isActiveModule($startpage)) {
48
			// Temporary solution for start page redirection
49
			define('XOOPS_STARTPAGE_REDIRECTED', 1);
50
			$module_handler = $this->xoops->getHandlerModule();
51
			$this->xoops->module = $this->xoops->getModuleByDirname($startpage);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->xoops->getModuleByDirname($startpage) can also be of type boolean. However, the property $module is declared as type null|object<Xoops\Core\K...l\Handlers\XoopsModule>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
			if (!$this->xoops->isModule() || !$this->xoops->module->getVar('isactive')) {
53
				$this->xoops->header();
54
				echo "<h4>" . XoopsLocale::E_NO_MODULE . "</h4>";
55
				$this->xoops->footer();
56
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method doInit() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
			}
58
			$moduleperm_handler = $this->xoops->getHandlerGroupPermission();
59
			$mid = $this->xoops->module->getVar('mid');
60
			if ($this->xoops->isUser()) {
61
				if (!$moduleperm_handler->checkRight('module_read', $mid, $this->xoops->user->getGroups())) {
62
					$this->xoops->redirect($this->xoops_url, 1, XoopsLocale::E_NO_ACCESS_PERMISSION, false);
63
				}
64
				$this->xoops->userIsAdmin = $this->xoops->user->isAdmin($mid);
65
			} else {
66
				if (!$moduleperm_handler->checkRight('module_read', $mid, FixedGroups::ANONYMOUS)) {
67
					$this->xoops->redirect($this->xoops_url . "/user.php", 1, XoopsLocale::E_NO_ACCESS_PERMISSION);
68
				}
69
			}
70
			if ($this->xoops->module->getVar('hasconfig') == 1
71
				|| $this->xoops->module->getVar('hascomments') == 1
72
				|| $this->xoops->module->getVar('hasnotification') == 1
73
			) {
74
				$this->xoops->moduleConfig = $this->xoops->getModuleConfigs();
75
			}
76
77
			chdir('modules/' . $startpage . '/');
78
			$this->xoops->loadLanguage('main', $this->xoops->module->getVar('dirname', 'n'));
79
			$parsed = parse_url($this->xoops_url);
80
			$url = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : 'http://';
81 View Code Duplication
			if (isset($parsed['host'])) {
82
				$url .= $parsed['host'];
83
				if (isset($parsed['port'])) {
84
					$url .= ':' . $parsed['port'];
85
				}
86
			} else {
87
				$url .= $_SERVER['HTTP_HOST'];
88
			}
89
90
			$_SERVER['REQUEST_URI'] =
91
				substr($this->xoops_url, strlen($url)) . '/modules/' . $startpage . '/index.php';
92
			include $this->xoops->path('modules/' . $startpage . '/index.php');
93
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method doInit() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
94
		}
95
	}
96
	
97
	public function doHeader()
98
	{
99
		$this->xoops->setOption('show_cblock', 1);
100
		$this->template = "module:system/system_homepage.tpl";
101
		parent::doHeader();
102
	}
103
}
104
105
$controller = new indexController();
106
$controller->run();
107