Completed
Push — develop ( 111dce...b8d476 )
by Timothy
02:35
created

Util::backupData()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 56
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 8
nop 1
dl 0
loc 56
rs 8.7592
c 0
b 0
f 0

How to fix   Long Method   

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 declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2016 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query\Drivers\Pgsql;
16
17
use Query\Drivers\AbstractUtil;
18
19
/**
20
 * Posgres-specific backup, import and creation methods
21
 */
22
class Util extends AbstractUtil {
23
24
	/**
25
	 * Create an SQL backup file for the current database's structure
26
	 *
27
	 * @return string
28
	 */
29
	public function backupStructure()
30
	{
31
		// @TODO Implement Backup function
32
		return '';
33
	}
34
35
	/**
36
	 * Create an SQL backup file for the current database's data
37
	 *
38
	 * @param array $exclude
39
	 * @return string
40
	 */
41
	public function backupData($exclude=[])
42
	{
43
		$tables = $this->getDriver()->getTables();
44
45
		// Filter out the tables you don't want
46
		if( ! empty($exclude))
47
		{
48
			$tables = array_diff($tables, $exclude);
49
		}
50
51
		$outputSql = '';
52
53
		// Get the data for each object
54
		foreach($tables as $t)
55
		{
56
			$sql = 'SELECT * FROM "'.trim($t).'"';
57
			$res = $this->getDriver()->query($sql);
0 ignored issues
show
Unused Code introduced by
The call to DriverInterface::query() has too many arguments starting with $sql.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
			$objRes = $res->fetchAll(\PDO::FETCH_ASSOC);
59
60
			// Don't add to the file if the table is empty
61
			if (count($objRes) < 1)
62
			{
63
				continue;
64
			}
65
66
			$res = NULL;
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
68
			// Nab the column names by getting the keys of the first row
69
			$columns = @array_keys($objRes[0]);
70
71
			$insertRows = [];
72
73
			// Create the insert statements
74
			foreach($objRes as $row)
75
			{
76
				$row = array_values($row);
77
78
				// Quote values as needed by type
79
				$row = array_map([$this->getDriver(), 'quote'], $row);
80
				$row = array_map('trim', $row);
81
82
83
				$rowString = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
84
85
				$row = NULL;
86
87
				$insertRows[] = $rowString;
88
			}
89
90
			$objRes = NULL;
0 ignored issues
show
Unused Code introduced by
$objRes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
92
			$outputSql .= "\n\n".implode("\n", $insertRows)."\n";
93
		}
94
95
		return $outputSql;
96
	}
97
}