Passed
Push — main ( d8f8f2...7eb3b3 )
by Rafael
47:22
created

BlockedLogAdminController::blockedlog()   B

Complexity

Conditions 10
Paths 22

Size

Total Lines 64
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 35
nc 22
nop 1
dl 0
loc 64
rs 7.6666
c 1
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
/* Copyright (C) 2017       ATM Consulting          <[email protected]>
4
 * Copyright (C) 2017-2018  Laurent Destailleur     <[email protected]>
5
 * Copyright (C) 2024       Rafael San José         <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace DoliModules\BlockedLog\Controller;
22
23
global $conf;
24
global $db;
25
global $user;
26
global $hookmanager;
27
global $user;
28
global $menumanager;
29
global $langs;
30
global $mysoc;
31
32
/**
33
 *  \file       htdocs/blockedlog/admin/blockedlog.php
34
 *  \ingroup    blockedlog
35
 *  \brief      Page setup for blockedlog module
36
 */
37
38
use DoliCore\Base\DolibarrController;
39
40
// Load Dolibarr environment
41
require BASE_PATH . '/main.inc.php';
42
require_once BASE_PATH . '/../Dolibarr/Modules/BlockedLog/Lib/BlockedLog.php';
43
require_once BASE_PATH . '/../Dolibarr/Lib/Admin.php';
44
45
46
class BlockedLogAdminController extends DolibarrController
47
{
48
    public function blockedlog(bool $executeActions = true): bool
49
    {
50
        global $conf;
51
        global $db;
52
        global $user;
53
        global $hookmanager;
54
        global $user;
55
        global $menumanager;
56
        global $langs;
57
        global $mysoc;
58
59
// Load translation files required by the page
60
        $langs->loadLangs(['admin', 'blockedlog', 'other']);
61
62
// Access Control
63
        if (!$user->admin || empty($conf->blockedlog->enabled)) {
64
            accessforbidden();
65
        }
66
67
// Get Parameters
68
        $action = GETPOST('action', 'aZ09');
69
        $backtopage = GETPOST('backtopage', 'alpha');
70
        $withtab = GETPOSTINT('withtab');
71
72
73
        /*
74
         * Actions
75
         */
76
77
        $reg = [];
78
        if (preg_match('/set_(.*)/', $action, $reg)) {
79
            $code = $reg[1];
80
            $values = GETPOST($code);
81
            if (is_array($values)) {
82
                $values = implode(',', $values);
83
            }
84
85
            if (dolibarr_set_const($db, $code, $values, 'chaine', 0, '', $conf->entity) > 0) {
86
                header("Location: " . $_SERVER['PHP_SELF'] . ($withtab ? '?withtab=' . $withtab : ''));
87
                exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
88
            } else {
89
                dol_print_error($db);
90
            }
91
        }
92
93
        if (preg_match('/del_(.*)/', $action, $reg)) {
94
            $code = $reg[1];
95
            if (dolibarr_del_const($db, $code, 0) > 0) {
96
                header("Location: " . $_SERVER['PHP_SELF'] . ($withtab ? '?withtab=' . $withtab : ''));
97
                exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
98
            } else {
99
                dol_print_error($db);
100
            }
101
        }
102
103
104
        /*
105
         *	View
106
         */
107
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BlockedLog/Views/admin_blocked_log.php');
108
109
        $db->close();
110
111
        return true;
112
    }
113
}
114