Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Maintenance::dumpTableData()   C

Complexity

Conditions 10
Paths 3

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 55
rs 6.8372
cc 10
eloc 41
nc 3
nop 1

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * maintenance extensions
14
 *
15
 * @package   Maintenance
16
 * @author    Mage Grégory (AKA Mage), Cointin Maxime (AKA Kraven30)
17
 * @copyright 2000-2016 XOOPS Project (http://xoops.org)
18
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @link      http://xoops.org
20
 */
21
class Maintenance
22
{
23
    /**
24
     * @var XoopsMySQLDatabase
25
     */
26
    public $db;
27
28
    /**
29
     * @var string
30
     */
31
    public $prefix;
32
33
    /**
34
     * Constructor
35
     */
36
    public function __construct()
37
    {
38
        $db           = \XoopsDatabaseFactory::getDatabaseConnection();
39
        $this->db     = $db;
40
        $this->prefix = $this->db->prefix . '_';
41
    }
42
43
    /**
44
     * Display Tables
45
     *
46
     * @param bool $array true to return as array, false to return as comma delimited string
47
     *
48
     * @return array|string list of tables
49
     */
50
    public function displayTables($array = true)
51
    {
52
        $tables = array();
53
        $result = $this->db->queryF('SHOW TABLES');
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::queryF() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        while ($myrow = $this->db->fetchArray($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->db->queryF('SHOW TABLES') on line 53 can also be of type boolean; however, XoopsMySQLDatabase::fetchArray() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The method XoopsMySQLDatabase::fetchArray() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
55
            $value = array_values($myrow);
56
            $value = substr($value[0], strlen($this->prefix));
57
            $tables[$value] = $value;
58
        }
59
        if ($array) {
60
            return $tables;
61
        } else {
62
            return implode(',', $tables);
63
        }
64
    }
65
66
    /**
67
     * Dump table structure
68
     *
69
     * @param string $table table name
70
     * @param int    $drop  1 to include drop table if exists
71
     *
72
     * @return array 'ret[sql_text] = dump, ret[structure] = display structure
73
     */
74
    public function dumpTableStructure($table, $drop)
75
    {
76
        $sql_text = '';
77
        $verif = false;
78
        $result = $this->db->queryF('SHOW create table `' . $table . '`;');
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::queryF() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
        if ($result) {
80
            if ($row = $this->db->fetchArray($result)) {
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::fetchArray() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
                $sql_text .= "# Table structure for table `" . $table . "` \n\n";
82
                if ($drop == 1) {
83
                    $sql_text .= "DROP TABLE IF EXISTS `" . $table . "`;\n\n";
84
                }
85
                $verif = true;
86
                $sql_text .= $row['Create Table'] . ";\n\n";
87
            }
88
        }
89
        $this->db->freeRecordSet($result);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::freeRecordSet() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
        $ret['sql_text'] = $sql_text;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
91
        $ret['structure'] = $verif;
92
        return $ret;
93
    }
94
95
    /**
96
     * Dump table data
97
     *
98
     * @param string $table table name
99
     *
100
     * @return array 'ret[sql_text] = dump, ret[records] = display records
101
     */
102
    public function dumpTableData($table)
103
    {
104
        $sql_text = '';
105
        $count = 0;
106
        $result = $this->db->queryF('SELECT * FROM ' . $table . ';');
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::queryF() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
107
        if ($result) {
108
            $num_rows = $this->db->getRowsNum($result);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::getRowsNum() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
            $num_fields = $this->db->getFieldsNum($result);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::getFieldsNum() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
110
111
            if ($num_rows > 0) {
112
                $field_type = array();
113
                $i = 0;
114
                while ($i < $num_fields) {
115
                    $field_type[] = $this->db->getFieldType($result, $i);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::getFieldType() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
116
                    ++$i;
117
                }
118
119
                $sql_text .= "INSERT INTO `" . $table . "` values\n";
120
                $index = 0;
121
                while ($row = $this->db->fetchRow($result)) {
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::fetchRow() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122
                    ++$count;
123
                    $sql_text .= "(";
124
                    for ($i = 0; $i < $num_fields; ++$i) {
125
                        if (is_null($row[$i])) {
126
                            $sql_text .= "null";
127
                        } else {
128
                            switch ($field_type[$i]) {
129
                                case 'int':
130
                                    $sql_text .= $row[$i];
131
                                    break;
132
                                default:
133
                                    $sql_text .= "'" . $this->db->escape($row[$i]) . "'";
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::escape() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
134
                            }
135
                        }
136
                        if ($i < $num_fields - 1) {
137
                            $sql_text .= ",";
138
                        }
139
                    }
140
                    $sql_text .= ")";
141
142
                    if ($index < $num_rows - 1) {
143
                        $sql_text .= ",";
144
                    } else {
145
                        $sql_text .= ";";
146
                    }
147
                    $sql_text .= "\n";
148
                    ++$index;
149
                }
150
            }
151
        }
152
        $this->db->freeRecordSet($result);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsMySQLDatabase::freeRecordSet() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
        $ret['sql_text'] = $sql_text . "\n\n";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
154
        $ret['records'] = $count;
155
        return $ret;
156
    }
157
158
    /**
159
     * Dump Write
160
     *
161
     * @param string $sql_text SQL to write
162
     *
163
     * @return array 'ret[file_name] = file name, ret[write] = write
164
     */
165
    public function dump_write($sql_text)
166
    {
167
        $file_name = "dump_" . date("Y.m.d") . "_" . date("H.i.s") . ".sql";
168
        $path_file = "../dump/" . $file_name;
169
        if (file_put_contents($path_file, $sql_text)) {
170
            $write = true;
171
        } else {
172
            $write = false;
173
        }
174
        $ret['file_name'] = $file_name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
175
        $ret['write'] = $write;
176
        return $ret;
177
    }
178
}
179