Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

SmartobjectDbupdater::updateTable()   F

Complexity

Conditions 17
Paths 6561

Size

Total Lines 48
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 20
nc 6561
nop 1
dl 0
loc 48
rs 2.5331
c 0
b 0
f 0

How to fix   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 namespace XoopsModules\Smartfaq;
2
3
/**
4
 * SmartobjectDbupdater class
5
 *
6
 * Class performing the database update for the module
7
 *
8
 * @package SmartObject
9
 * @author  marcan <[email protected]>
10
 * @link    http://www.smartfactory.ca The SmartFactory
11
 */
12
class SmartobjectDbupdater
13
{
14
    /**
15
     * SmartobjectDbupdater constructor.
16
     */
17
    public function __construct()
18
    {
19
    }
20
21
    /**
22
     * Use to execute a general query
23
     *
24
     * @param string $query   query that will be executed
25
     * @param string $goodmsg message displayed on success
26
     * @param string $badmsg  message displayed on error
27
     *
28
     * @return bool true if success, false if an error occured
29
     *
30
     */
31
    public function runQuery($query, $goodmsg, $badmsg)
32
    {
33
        global $xoopsDB;
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...
34
        $ret = $xoopsDB->queryF($query);
35
        if (!$ret) {
36
            echo "&nbsp;&nbsp;$badmsg<br>";
37
38
            return false;
39
        } else {
40
            echo "&nbsp;&nbsp;$goodmsg<br>";
41
42
            return true;
43
        }
44
    }
45
46
    /**
47
     * Use to rename a table
48
     *
49
     * @param string $from name of the table to rename
50
     * @param string $to   new name of the renamed table
51
     *
52
     * @return bool true if success, false if an error occured
53
     */
54
    public function renameTable($from, $to)
55
    {
56
        global $xoopsDB;
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...
57
58
        $from = $xoopsDB->prefix($from);
59
        $to   = $xoopsDB->prefix($to);
60
61
        $query = sprintf('ALTER TABLE %s RENAME %s', $from, $to);
62
        $ret   = $xoopsDB->queryF($query);
63
        if (!$ret) {
64
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_RENAME_TABLE_ERR, $from) . '<br>';
65
66
            return false;
67
        } else {
68
            echo '&nbsp;&nbsp;' . sprintf(_SDU_MSG_RENAME_TABLE, $from, $to) . '<br>';
69
70
            return true;
71
        }
72
    }
73
74
    /**
75
     * Use to update a table
76
     *
77
     * @param object $table {@link SmartDbTable} that will be updated
78
     *
79
     * @see SmartDbTable
80
     *
81
     * @return bool true if success, false if an error occured
82
     */
83
    public function updateTable($table)
84
    {
85
        global $xoopsDB;
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...
86
87
        $ret = true;
88
89
        // If table has a structure, create the table
90
        if ($table->getStructure()) {
91
            $ret = $table->createTable() && $ret;
92
        }
93
94
        // If table is flag for drop, drop it
95
        if ($table->getFlagForDrop) {
96
            $ret = $table->dropTable() && $ret;
97
        }
98
99
        // If table has data, insert it
100
        if ($table->getData()) {
101
            $ret = $table->addData() && $ret;
102
        }
103
104
        // If table has new fields to be added, add them
105
        if ($table->getNewFields()) {
106
            $ret = $table->addNewFields() && $ret;
107
        }
108
109
        // If table has altered field, alter the table
110
        if ($table->getAlteredFields()) {
111
            $ret = $table->alterTable() && $ret;
112
        }
113
114
        // If table has updated field values, update the table
115
        if ($table->getUpdatedFields()) {
116
            $ret = $table->updateFieldsValues($table) && $ret;
117
        }
118
119
        // If table has dropped field, alter the table
120
        if ($table->getDroppedFields()) {
121
            $ret = $table->dropFields($table) && $ret;
122
        }
123
        //felix
124
        // If table has updated field values, update the table
125
        if ($table->getUpdatedWhere()) {
126
            $ret = $table->UpdateWhereValues($table) && $ret;
127
        }
128
129
        return $ret;
130
    }
131
}
132