Categories::main()   F
last analyzed

Complexity

Conditions 38
Paths > 20000

Size

Total Lines 150
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 38
eloc 95
nc 71110
nop 0
dl 0
loc 150
rs 0
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
namespace Alixar\Controllers;
4
5
use Alxarafe\Helpers\Skin;
6
use Alixar\Base\AlixarController;
7
use Alixar\Models\Category;
8
use Alixar\Views\CategoriesView;
9
use Alixar\Views\CategoriesIndexView;
10
use Alixar\Helpers\Globals;
11
use Alixar\Helpers\AlDolUtils;
12
13
class Categories extends AlixarController
14
{
15
16
    public $action;
17
    public $cancel;
18
    public $origin;
19
    public $catorigin;
20
    public $type;
21
    public $urlfrom;
22
    public $backtopage;
23
    public $id;
24
    public $socid;
25
    public $label;
26
    public $description;
27
    public $color;
28
    public $visible;
29
    public $parent;
30
    public $object;
31
    public $catname;
32
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    function index(): void
39
    {
40
        parent::index();
41
42
        // Load translation files required by the page
43
        Globals::$langs->load("categories");
44
45
        // Security check
46
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
0 ignored issues
show
Bug introduced by alxarafe
The method GETPOST() does not exist on Alixar\Helpers\AlDolUtils. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $this->socid = AlDolUtils::GETPOST('socid', 'int');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        if (!Globals::$user->rights->categorie->lire) {
48
            accessforbidden();
49
        }
50
51
        $this->getVars();
52
53
        Skin::$view = new CategoriesIndexView($this);
54
    }
55
56
    function getVars()
57
    {
58
59
        $this->action = AlDolUtils::GETPOST('action', 'alpha');
60
        $this->cancel = AlDolUtils::GETPOST('cancel', 'alpha');
61
        $this->origin = AlDolUtils::GETPOST('origin', 'alpha');
62
        $this->catorigin = AlDolUtils::GETPOST('catorigin', 'int');
63
        $this->type = AlDolUtils::GETPOST('type', 'alpha');
64
        $this->urlfrom = AlDolUtils::GETPOST('urlfrom', 'alpha');
65
        $this->backtopage = AlDolUtils::GETPOST('backtopage', 'alpha');
66
67
        $this->id = AlDolUtils::GETPOST('id', 'int');
68
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
69
        $this->label = AlDolUtils::GETPOST('label');
70
        $this->description = AlDolUtils::GETPOST('description');
71
        $this->color = AlDolUtils::GETPOST('color');
72
        $this->visible = AlDolUtils::GETPOST('visible');
73
        $this->parent = AlDolUtils::GETPOST('parent');
74
        $this->catname = AlDolUtils::GETPOST('catname', 'alpha');
75
    }
76
77
    function main()
78
    {
79
        Skin::$view = new CategoriesView($this);
80
        if ($this->origin) {
81
            if ($this->type == Category::TYPE_PRODUCT) {
82
                $idProdOrigin = $this->origin;
83
            }
84
            if ($this->type == Category::TYPE_SUPPLIER) {
85
                $idSupplierOrigin = $this->origin;
86
            }
87
            if ($this->type == Category::TYPE_CUSTOMER) {
88
                $idCompanyOrigin = $this->origin;
89
            }
90
            if ($this->type == Category::TYPE_MEMBER) {
91
                $idMemberOrigin = $this->origin;
92
            }
93
            if ($this->type == Category::TYPE_CONTACT) {
94
                $idContactOrigin = $this->origin;
95
            }
96
            if ($this->type == Category::TYPE_PROJECT) {
97
                $idProjectOrigin = $this->origin;
98
            }
99
        }
100
101
        if ($this->catorigin && $this->type == Category::TYPE_PRODUCT) {
102
            $idCatOrigin = $this->catorigin;
103
        }
104
105
        $this->object = new Category();
106
107
        $extrafields = new AlExtraFields();
108
        $extralabels = $extrafields->fetch_name_optionals_label($this->object->table_element);
109
110
        // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
111
        Globals::$hookManager->initHooks(array('categorycard'));
112
113
114
        /*
115
         * 	Actions
116
         */
117
118
        // Add action
119
        if ($this->action == 'add' && Globals::$user->rights->categorie->creer) {
120
            // Action ajout d'une categorie
121
            if ($this->cancel) {
122
                if ($this->urlfrom) {
123
                    header("Location: " . $urlfrom);
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $urlfrom seems to be never defined.
Loading history...
124
                    exit;
125
                }
126
                if ($idProdOrigin) {
127
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type);
128
                    exit;
129
                }
130
                if ($idCompanyOrigin) {
131
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type);
132
                    exit;
133
                }
134
                if ($idSupplierOrigin) {
135
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type);
136
                    exit;
137
                }
138
                if ($idMemberOrigin) {
139
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type);
140
                    exit;
141
                }
142
                if ($idContactOrigin) {
143
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type);
144
                    exit;
145
                }
146
                if ($idProjectOrigin) {
147
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type);
148
                    exit;
149
                }
150
                header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $this->type);
151
                exit;
152
            }
153
154
            $object->label = $this->label;
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $object seems to be never defined.
Loading history...
155
            $object->color = $this->color;
156
            $object->description = AlDolUtils::dol_htmlcleanlastbr($this->description);
0 ignored issues
show
Bug introduced by alxarafe
The method dol_htmlcleanlastbr() does not exist on Alixar\Helpers\AlDolUtils. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            /** @scrutinizer ignore-call */ 
157
            $object->description = AlDolUtils::dol_htmlcleanlastbr($this->description);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
            $object->socid = ($this->socid ? $this->socid : 'null');
158
            $object->visible = $this->visible;
159
            $object->type = $this->type;
160
161
            if ($parent != "-1") {
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $parent seems to be never defined.
Loading history...
162
                $object->fk_parent = $parent;
163
            }
164
165
            $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
166
            if ($ret < 0) {
167
                $error++;
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $error seems to be never defined.
Loading history...
168
            }
169
170
            if (!$object->label) {
171
                $error++;
172
                setEventMessages(Globals::$langs->trans("ErrorFieldRequired", Globals::$langs->transnoentities("Ref")), null, 'errors');
173
                $this->action = 'create';
174
            }
175
176
            // Create category in database
177
            if (!$error) {
178
                $result = $object->create($user);
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $user seems to be never defined.
Loading history...
179
                if ($result > 0) {
180
                    $this->action = 'confirmed';
181
                    $_POST["addcat"] = '';
182
                } else {
183
                    setEventMessages($object->error, $object->errors, 'errors');
184
                }
185
            }
186
        }
187
188
        // Confirm action
189
        if (($this->action == 'add' || $this->action == 'confirmed') && Globals::$user->rights->categorie->creer) {
190
            // Action confirmation de creation categorie
191
            if ($this->action == 'confirmed') {
192
                if ($urlfrom) {
193
                    header("Location: " . $urlfrom);
194
                    exit;
195
                }
196
                if ($backtopage) {
0 ignored issues
show
Comprehensibility Best Practice introduced by alxarafe
The variable $backtopage seems to be never defined.
Loading history...
197
                    header("Location: " . $backtopage);
198
                    exit;
199
                }
200
                if ($idProdOrigin) {
201
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
202
                    exit;
203
                }
204
                if ($idCompanyOrigin) {
205
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
206
                    exit;
207
                }
208
                if ($idSupplierOrigin) {
209
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
210
                    exit;
211
                }
212
                if ($idMemberOrigin) {
213
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
214
                    exit;
215
                }
216
                if ($idContactOrigin) {
217
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
218
                    exit;
219
                }
220
                if ($idProjectOrigin) {
221
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
222
                    exit;
223
                }
224
225
                header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $result . '&type=' . $this->type);
226
                exit;
227
            }
228
        }
229
    }
230
}
231