Passed
Push — development ( b3bbd1...d832f6 )
by Nils
05:00 queued 01:04
created

changeDB()   F

Complexity

Conditions 21
Paths 688

Size

Total Lines 56
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 21
Bugs 0 Features 0
Metric Value
cc 21
eloc 42
c 21
b 0
f 0
nc 688
nop 0
dl 0
loc 56
rs 3.5714

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 62 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
 * @package       upgrade_db_1.08.php
4
 * @author        Nils Laumaillé <[email protected]>
5
 * @version       2.1.27
6
 * @copyright     2009-2018 Nils Laumaillé
7
 * @license       GNU GPL-3.0
8
 * @link          https://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
60
//This will permit to update DB due to major change in log_items table for 1.08 version needs.
61
62
function changeDB()
63
{
64
    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...
65
    $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...
66
    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

66
    while ($data = mysqli_fetch_array(/** @scrutinizer ignore-type */ $res)) {
Loading history...
67
        $action = "";
68
        //ENGLISH
69
        foreach ($english_vals as $lang) {
70
            if ($lang[1] == $data['action']) {
71
                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']);
72
                $found = true;
73
                $action = $lang[0];
74
            }
75
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
76
                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']."'");
77
                $found = true;
78
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
79
                $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

79
                $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...
80
                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]."'");
81
                $found = true;
82
            }
83
        }
84
85
        //FRENCH
86
        $action = "";
87
        foreach ($french_vals as $lang) {
88
            if ($lang[1] == $data['action']) {
89
                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']);
90
                $found = true;
91
                $action = $lang[0];
92
            }
93
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
94
                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']."'");
95
                $found = true;
96
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
97
                $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."'"));
98
                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]."'");
99
                $found = true;
100
            }
101
        }
102
103
        //SPANISH
104
        $action = "";
105
        foreach ($spanish_vals as $lang) {
106
            if ($lang[1] == $data['action']) {
107
                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']);
108
                $found = true;
109
                $action = $lang[0];
110
            }
111
            if ($lang[1] == $data['raison'] && !empty($data['raison'])) {
112
                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']."'");
113
                $found = true;
114
            } elseif ($lang[1] == trim(substr($data['raison'], 0, strpos($data['raison'], ":"))) && !empty($data['raison'])) {
115
                $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."'"));
116
                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]."'");
117
                $found = true;
118
            }
119
        }
120
    }
121
}
122