Passed
Push — main ( 875825...95c1dc )
by Rafael
48:39
created

BookMarksCardController::index()   F

Complexity

Conditions 23
Paths 1510

Size

Total Lines 119
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 72
c 1
b 0
f 0
nc 1510
nop 1
dl 0
loc 119
rs 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) 2001-2003  Rodolphe Quiedeville    <[email protected]>
4
 * Copyright (C) 2005-2022  Laurent Destailleur     <[email protected]>
5
 * Copyright (C) 2014       Marcos García           <[email protected]>
6
 * Copyright (C) 2024       Rafael San José         <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace DoliModules\BookMarks\Controller;
23
24
global $conf;
25
global $db;
26
global $user;
27
global $hookmanager;
28
global $user;
29
global $menumanager;
30
global $langs;
31
global $mysoc;
32
33
use DoliCore\Base\DolibarrController;
34
35
36
/**
37
 *    \file       htdocs/bookmarks/card.php
38
 *    \ingroup    bookmark
39
 *    \brief      Page display/creation of bookmarks
40
 */
41
42
use DoliCore\Form\Form;
43
use DoliCore\Model\Bookmark;
44
45
// Load Dolibarr environment
46
require BASE_PATH . '/main.inc.php';
47
48
class BookMarksCardController extends DolibarrController
49
{
50
51
    public function index(bool $executeActions = true): bool
52
    {
53
        global $conf;
54
        global $db;
55
        global $user;
56
        global $hookmanager;
57
        global $user;
58
        global $menumanager;
59
        global $langs;
60
61
62
// Load translation files required by the page
63
        $langs->loadLangs(['bookmarks', 'other']);
64
65
// Get Parameters
66
        $id = GETPOSTINT("id");
67
        $action = GETPOST("action", "alpha");
68
        $title = (string) GETPOST("title", "alpha");
69
        $url = (string) GETPOST("url", "alpha");
70
        $urlsource = GETPOST("urlsource", "alpha");
71
        $target = GETPOSTINT("target");
72
        $userid = GETPOSTINT("userid");
73
        $position = GETPOSTINT("position");
74
        $backtopage = GETPOST('backtopage', 'alpha');
75
76
// Initialize Objects
77
        $object = new Bookmark($db);
78
        if ($id > 0) {
79
            $object->fetch($id);
80
        }
81
82
// Security check
83
        restrictedArea($user, 'bookmark', $object);
84
85
        $permissiontoread = $user->hasRight('bookmark', 'lire');
86
        $permissiontoadd = $user->hasRight('bookmark', 'creer');
87
        $permissiontodelete = $user->hasRight('bookmark', 'supprimer');
88
89
90
        /*
91
         * Actions
92
         */
93
94
        if ($action == 'add' || $action == 'addproduct' || $action == 'update') {
95
            if ($action == 'update') {
96
                $invertedaction = 'edit';
97
            } else {
98
                $invertedaction = 'create';
99
            }
100
101
            $error = 0;
102
103
            if (GETPOST('cancel', 'alpha')) {
104
                if (empty($backtopage)) {
105
                    $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT . '/bookmarks/list.php'));
106
                }
107
                header("Location: " . $backtopage);
108
                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...
109
            }
110
111
            if ($action == 'update') {
112
                $object->fetch(GETPOSTINT("id"));
113
            }
114
            // Check if null because user not admin can't set an user and send empty value here.
115
            if (!empty($userid)) {
116
                $object->fk_user = $userid;
117
            }
118
            $object->title = $title;
119
            $object->url = $url;
120
            $object->target = $target;
121
            $object->position = $position;
122
123
            if (!$title) {
124
                $error++;
125
                setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("BookmarkTitle")), null, 'errors');
126
            }
127
128
            if (!$url) {
129
                $error++;
130
                setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("UrlOrLink")), null, 'errors');
131
            }
132
133
            if (!$error) {
134
                $object->favicon = 'none';
135
136
                if ($action == 'update') {
137
                    $res = $object->update();
138
                } else {
139
                    $res = $object->create();
140
                }
141
142
                if ($res > 0) {
143
                    if (empty($backtopage)) {
144
                        $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT . '/bookmarks/list.php'));
145
                    }
146
                    header("Location: " . $backtopage);
147
                    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...
148
                } else {
149
                    if ($object->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
150
                        $langs->load("errors");
151
                        setEventMessages($langs->transnoentities("WarningBookmarkAlreadyExists"), null, 'warnings');
152
                    } else {
153
                        setEventMessages($object->error, $object->errors, 'errors');
154
                    }
155
                    $action = $invertedaction;
156
                }
157
            } else {
158
                $action = $invertedaction;
159
            }
160
        }
161
162
        /*
163
         * View
164
         */
165
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookMarks/Views/card.php');
166
167
        $db->close();
168
169
        return true;
170
    }
171
}
172