Completed
Pull Request — develop (#518)
by Agel_Nash
05:20
created

SqlParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 12
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

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
3
// MySQL Dump Parser
4
// SNUFFKIN/ Alex 2004
5
6
class SqlParser {
7
	public $host;
8
	public $dbname;
9
	public $prefix;
10
	public $user;
11
	public $password;
12
	public $mysqlErrors;
13
	public $conn;
14
	public $installFailed;
15
	public $sitename;
16
	public $adminname;
17
	public $adminemail;
18
	public $adminpass;
19
	public $managerlanguage;
20
	public $mode;
21
	public $fileManagerPath;
22
	public $imgPath;
23
	public $imgUrl;
24
	public $dbMODx;
25
	public $dbVersion;
26
    public $connection_charset;
27
    public $connection_method;
28
    public $ignoreDuplicateErrors;
29
30
	public function __construct($host, $user, $password, $db, $prefix='modx_', $adminname, $adminemail, $adminpass, $connection_charset= 'utf8', $managerlanguage='english', $connection_method = 'SET CHARACTER SET', $auto_template_logic = 'parent') {
31
		$this->host = $host;
32
		$this->dbname = $db;
33
		$this->prefix = $prefix;
34
		$this->user = $user;
35
		$this->password = $password;
36
		$this->adminpass = $adminpass;
37
		$this->adminname = $adminname;
38
		$this->adminemail = $adminemail;
39
		$this->connection_charset = $connection_charset;
40
		$this->connection_method = $connection_method;
41
		$this->ignoreDuplicateErrors = false;
42
		$this->managerlanguage = $managerlanguage;
43
        $this->autoTemplateLogic = $auto_template_logic;
0 ignored issues
show
Bug introduced by
The property autoTemplateLogic does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
	}
45
46
	public function connect() {
47
		$this->conn = mysqli_connect($this->host, $this->user, $this->password);
48
		mysqli_select_db($this->conn, $this->dbname);
49
		if (function_exists('mysqli_set_charset')) mysqli_set_charset($this->conn, $this->connection_charset);
50
51
		$this->dbVersion = 3.23; // assume version 3.23
52
		if(function_exists("mysqli_get_server_info")) {
53
			$ver = mysqli_get_server_info($this->conn);
54
			$this->dbMODx 	 = version_compare($ver,"4.0.2");
55
			$this->dbVersion = (float) $ver; // Typecasting (float) instead of floatval() [PHP < 4.2]
56
		}
57
58
        mysqli_query($this->conn,"{$this->connection_method} {$this->connection_charset}");
59
	}
60
61
    public function process($filename) {
62
	    global $custom_placeholders;
63
64
		// check to make sure file exists
65
		if (!file_exists($filename)) {
66
			$this->mysqlErrors[] = array("error" => "File '$filename' not found");
67
			$this->installFailed = true ;
68
			return false;
69
		}
70
71
		$fh = fopen($filename, 'r');
72
		$idata = '';
73
74
		while (!feof($fh)) {
75
			$idata .= fread($fh, 1024);
76
		}
77
78
		fclose($fh);
79
		$idata = str_replace("\r", '', $idata);
80
81
		// check if in upgrade mode
82
		if ($this->mode=="upd") {
83
			// remove non-upgradeable parts
84
			$s = strpos($idata,"non-upgrade-able[[");
85
			$e = strpos($idata,"]]non-upgrade-able")+17;
86
			if($s && $e) $idata = str_replace(substr($idata,$s,$e-$s)," Removed non upgradeable items",$idata);
87
		}
88
89
		// replace {} tags
90
		$idata = str_replace('{PREFIX}', $this->prefix, $idata);
91
		$idata = str_replace('{ADMIN}', $this->adminname, $idata);
92
		$idata = str_replace('{ADMINEMAIL}', $this->adminemail, $idata);
93
		$idata = str_replace('{ADMINPASS}', $this->adminpass, $idata);
94
		$idata = str_replace('{IMAGEPATH}', $this->imgPath, $idata);
95
		$idata = str_replace('{IMAGEURL}', $this->imgUrl, $idata);
96
		$idata = str_replace('{FILEMANAGERPATH}', $this->fileManagerPath, $idata);
97
		$idata = str_replace('{MANAGERLANGUAGE}', $this->managerlanguage, $idata);
98
		$idata = str_replace('{AUTOTEMPLATELOGIC}', $this->autoTemplateLogic, $idata);
99
		/*$idata = str_replace('{VERSION}', $modx_version, $idata);*/
100
101
		// Replace custom placeholders
102
		foreach($custom_placeholders as $key=>$val) {
103
			if (strpos($idata, '{'.$key.'}') !== false) {
104
				$idata = str_replace('{'.$key.'}', $val, $idata);
105
			}
106
		}
107
108
		$sql_array = explode("\n\n", $idata);
109
110
		$num = 0;
111
		foreach($sql_array as $sql_entry) {
112
			$sql_do = trim($sql_entry, "\r\n; ");
113
114
			if (preg_match('/^\#/', $sql_do)) continue;
115
116
			// strip out comments and \n for mysql 3.x
117
			if ($this->dbVersion <4.0) {
118
				$sql_do = preg_replace("~COMMENT.*[^']?'.*[^']?'~","",$sql_do);
119
				$sql_do = str_replace('\r', "", $sql_do);
120
				$sql_do = str_replace('\n', "", $sql_do);
121
			}
122
123
124
			$num = $num + 1;
125
			if ($sql_do) mysqli_query($this->conn, $sql_do);
126
			if(mysqli_error($this->conn)) {
127
				// Ignore duplicate and drop errors - Raymond
128
				if ($this->ignoreDuplicateErrors){
129
					if (mysqli_errno($this->conn) == 1060 || mysqli_errno($this->conn) == 1061 || mysqli_errno($this->conn) == 1062 ||mysqli_errno($this->conn) == 1091) continue;
130
				}
131
				// End Ignore duplicate
132
				$this->mysqlErrors[] = array("error" => mysqli_error($this->conn), "sql" => $sql_do);
133
				$this->installFailed = true;
134
			}
135
		}
136
	}
137
138
    public function close() {
139
		mysqli_close($this->conn);
140
	}
141
}
142