Passed
Branch development (e0e718)
by Nils
04:45
created

changeDB()   F

Complexity

Conditions 21
Paths 688

Size

Total Lines 56
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 19
Bugs 0 Features 0
Metric Value
cc 21
eloc 42
nc 688
nop 0
dl 0
loc 56
rs 3.5714
c 19
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 64 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @file          upgrade_db_1.08.php
4
 * @author        Nils Laumaillé
5
 * @version       2.1.27
6
 * @copyright     (c) 2009-2017 Nils Laumaillé
7
 * @licensing     GNU GPL-3.0
8
 * @link          http://www.teampass.net
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
require_once('../sources/SecureHandler.php');
16
session_start();
17
require_once '../includes/config/settings.php';
18
//ENGLISH
19
$english_vals = array(
20
    array('at_modification', "Modification"),
21
    array('at_creation', "Creation"),
22
    array('at_delete', "Deletion"),
23
    array('at_pw', "Password changed."),
24
    array('at_category', "Group"),
25
    array('at_personnel', "Personnal"),
26
    array('at_description', "Description"),
27
    array('at_url', "Url"),
28
    array('at_login', "Login"),
29
    array('at_label', "Label")
30
);
31
//FRENCH
32
$french_vals = array(
33
    array('at_modification', "Modification"),
34
    array('at_creation', "Création"),
35
    array('at_delete', "Suppression"),
36
    array('at_pw', "Mot de passe changé."),
37
    array('at_category', "Group"),
38
    array('at_personnel', "Personnel"),
39
    array('at_description', "Description."),
40
    array('at_url', "Url"),
41
    array('at_login', "Login"),
42
    array('at_label', "Label")
43
);
44
//SPANISH
45
$spanish_vals = array(
46
    array('at_modification', "Modificacion"),
47
    array('at_creation', "Creacion"),
48
    array('at_delete', "Borrado"),
49
    array('at_pw', "Contraseéa cambiada."),
50
    array('at_category', "Grupo"),
51
    array('at_personnel', "Personal"),
52
    array('at_description', "Descripcion."),
53
    array('at_url', "Url"),
54
    array('at_login', "Login"),
55
    array('at_label', "Etiqueta")
56
);
57
58
changeDB();
59
changeDB();
60
changeDB();
61
62
//This will permit to update DB due to major change in log_items table for 1.08 version needs.
63
64
function changeDB()
65
{
66
    global $spanish_vals, $french_vals, $english_vals, $dbTmp;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
67
    $res = mysqli_query($dbTmp, "SELECT * FROM ".$pre."log_items") or die(mysqli_error($dbTmp));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Comprehensibility Best Practice introduced by
The variable $pre seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
68
    while ($data = mysqli_fetch_array($res)) {
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type boolean; however, parameter $result of mysqli_fetch_array() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

68
    while ($data = mysqli_fetch_array(/** @scrutinizer ignore-type */ $res)) {
Loading history...
69
        $action = "";
70
        //ENGLISH
71
        foreach ($english_vals as $lang) {
72
            if ($lang[1] == $data['action']) {
73
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET action = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']);
74
                $found = true;
75
                $action = $lang[0];
76
            }
77
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
78
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data['action']."'");
79
                $found = true;
80
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
81
                $data1 = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT action FROM ".$pre."log_items WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$action."'"));
0 ignored issues
show
Bug introduced by
It seems like mysqli_query($dbTmp, 'SE...on ='' . $action . ''') can also be of type boolean; however, parameter $result of mysqli_fetch_row() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

81
                $data1 = mysqli_fetch_row(/** @scrutinizer ignore-type */ mysqli_query($dbTmp, "SELECT action FROM ".$pre."log_items WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$action."'"));
Loading history...
82
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]." ".substr($data['raison'], strpos($data['raison'], ":"))."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data1[0]."'");
83
                $found = true;
84
            }
85
        }
86
87
        //FRENCH
88
        $action = "";
89
        foreach ($french_vals as $lang) {
90
            if ($lang[1] == $data['action']) {
91
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET action = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']);
92
                $found = true;
93
                $action = $lang[0];
94
            }
95
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
96
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data['action']."'");
97
                $found = true;
98
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
99
                $data1 = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT action FROM ".$pre."log_items WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$action."'"));
100
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]." ".substr($data['raison'], strpos($data['raison'], ":"))."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data1[0]."'");
101
                $found = true;
102
            }
103
        }
104
105
        //SPANISH
106
        $action = "";
107
        foreach ($spanish_vals as $lang) {
108
            if ($lang[1] == $data['action']) {
109
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET action = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']);
110
                $found = true;
111
                $action = $lang[0];
112
            }
113
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
114
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data['action']."'");
115
                $found = true;
116
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
117
                $data1 = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT action FROM ".$pre."log_items WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$action."'"));
118
                mysqli_query($dbTmp, "UPDATE ".$pre."log_items SET raison = '".$lang[0]." ".substr($data['raison'], strpos($data['raison'], ":"))."' WHERE id_item=".$data['id_item']." AND date =".$data['date']." AND id_user =".$data['id_user']." AND raison ='".$data['raison']."' AND action ='".$data1[0]."'");
119
                $found = true;
120
            }
121
        }
122
    }
123
}
124