create_db::create_database()   B
last analyzed

Complexity

Conditions 11
Paths 120

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 120
nop 8
dl 0
loc 32
rs 7.15
c 0
b 0
f 0

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
require_once(dirname(__FILE__).'/../require/settings.php');
3
require_once(dirname(__FILE__).'/../require/class.Connection.php');
4
5
class create_db {
6
7
	/**
8
	 * @param string $filename
9
	 */
10
	public static function import_file($filename) {
11
		$filename = filter_var($filename,FILTER_SANITIZE_STRING);
12
		$Connection = new Connection();
13
		if (!$Connection->connectionExists()) return 'error: DB connection failed';
14
		//Connection::$db->beginTransaction();
15
		$templine = '';
16
		$handle = @fopen($filename,"r");
17
		if ($handle) {
18
			//$lines = file($filename);
19
			//foreach ($lines as $line)
20
			while (($line = fgets($handle,4096)) !== false)
21
			{
22
				if (substr($line,0,2) == '--' || $line == '') continue;
23
				$templine .= $line;
24
				if (substr(trim($line), -1,1) == ';')
25
				{
26
					try {
27
						$sth = $Connection->db->prepare($templine);
28
						$sth->execute();
29
					} catch(PDOException $e) {
30
						return "error (import ".$filename.") : ".$e->getMessage()."\n";
31
					}
32
					$templine = '';
33
				}
34
			}
35
			fclose($handle);
36
		}
37
		//Connection::$db->commit();
38
		$Connection->db = null;
39
		return '';
40
	}
41
42
	public static function import_all_db($directory) {
43
		$error = '';
44
		$dh = opendir($directory);
45
		//foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename)
46
		while(false !== ($filename = readdir($dh)))
47
		{
48
			if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename);
49
		}
50
		return $error;
51
	}
52
53
	public static function create_database($root,$root_pass,$user,$pass,$db,$db_type,$host,$port = '') {
54
		$root = filter_var($root,FILTER_SANITIZE_STRING);
55
		$root_pass = filter_var($root_pass,FILTER_SANITIZE_STRING);
56
		$user = filter_var($user,FILTER_SANITIZE_STRING);
57
		$password = filter_var($pass,FILTER_SANITIZE_STRING);
58
		$db = filter_var($db,FILTER_SANITIZE_STRING);
59
		$db_type = filter_var($db_type,FILTER_SANITIZE_STRING);
60
		$host = filter_var($host,FILTER_SANITIZE_STRING);
61
		if ($db_type == 'mysql' && $port == '') $port = 3306;
62
		elseif ($port == '') $port = 5432;
63
		// Dirty hack
64
		if ($host != 'localhost' && $host != '127.0.0.1') {
65
			$grantright = $_SERVER['SERVER_ADDR'];
66
		} else $grantright = 'localhost';
67
		try {
68
			if ($host == 'localhost') $dbh = new PDO($db_type.':host=127.0.0.1',$root,$root_pass);
69
			else $dbh = new PDO($db_type.':host='.$host.';port='.$port,$root,$root_pass);
70
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
71
			if ($db_type == 'mysql') {
72
				$dbh->exec('CREATE DATABASE IF NOT EXISTS `'.$db.'`;GRANT ALL ON `'.$db."`.* TO '".$user."'@'".$grantright."' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
73
				if ($grantright == 'localhost') $dbh->exec('GRANT ALL ON `'.$db."`.* TO '".$user."'@'127.0.0.1' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
74
			} else if ($db_type == 'pgsql') {
75
				$dbh->exec("CREATE DATABASE ".$db.";");
76
				$dbh->exec("CREATE USER ".$user." WITH PASSWORD '".$password."';
77
					GRANT ALL PRIVILEGES ON DATABASE ".$db." TO ".$user.";");
78
			}
79
		} catch(PDOException $e) {
80
			$dbh = null;
0 ignored issues
show
Unused Code introduced by
$dbh 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...
81
			return "error : ".$e->getMessage();
82
		}
83
		$dbh = null;
0 ignored issues
show
Unused Code introduced by
$dbh 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...
84
	}
85
	
86
}
87
?>