SqlParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 12
dl 0
loc 15
rs 9.7666
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
    public $autoTemplateLogic;
30
31
	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') {
32
		$this->host = $host;
33
		$this->dbname = $db;
34
		$this->prefix = $prefix;
35
		$this->user = $user;
36
		$this->password = $password;
37
		$this->adminpass = $adminpass;
38
		$this->adminname = $adminname;
39
		$this->adminemail = $adminemail;
40
		$this->connection_charset = $connection_charset;
41
		$this->connection_method = $connection_method;
42
		$this->ignoreDuplicateErrors = false;
43
		$this->managerlanguage = $managerlanguage;
44
        $this->autoTemplateLogic = $auto_template_logic;
45
	}
46
47
	public function connect() {
48
        $host = explode(':', $this->host, 2);
49
        $this->conn = mysqli_connect($host[0], $this->user, $this->password,'', isset($host[1]) ? $host[1] : null);
50
		mysqli_select_db($this->conn, $this->dbname);
51
		if (function_exists('mysqli_set_charset')) mysqli_set_charset($this->conn, $this->connection_charset);
52
53
		$this->dbVersion = 3.23; // assume version 3.23
54
		if(function_exists("mysqli_get_server_info")) {
55
			$ver = mysqli_get_server_info($this->conn);
56
			$this->dbMODx 	 = version_compare($ver,"4.0.2");
57
			$this->dbVersion = (float) $ver; // Typecasting (float) instead of floatval() [PHP < 4.2]
58
		}
59
60
        mysqli_query($this->conn,"{$this->connection_method} {$this->connection_charset}");
61
	}
62
63
    public function process($filename) {
64
	    global $custom_placeholders;
65
66
		// check to make sure file exists
67
		if (!file_exists($filename)) {
68
			$this->mysqlErrors[] = array("error" => "File '$filename' not found");
69
			$this->installFailed = true ;
70
			return false;
71
		}
72
73
		$fh = fopen($filename, 'r');
74
		$idata = '';
75
76
		while (!feof($fh)) {
77
			$idata .= fread($fh, 1024);
78
		}
79
80
		fclose($fh);
81
		$idata = str_replace("\r", '', $idata);
82
83
		// check if in upgrade mode
84
		if ($this->mode === 'upd') {
85
			// remove non-upgradeable parts
86
			$s = strpos($idata,'non-upgrade-able[[');
87
			$e = strpos($idata,']]non-upgrade-able') + 17;
88
			if($s && $e) {
89
			    $idata = str_replace(substr($idata, $s,$e-$s),' Removed non upgradeable items', $idata);
90
            }
91
		}
92
93
		// replace {} tags
94
		$idata = str_replace('{PREFIX}', $this->prefix, $idata);
95
        $idata = str_replace('{TABLEENCODING}', $this->getTableEncoding(), $idata);
96
		$idata = str_replace('{ADMIN}', $this->adminname, $idata);
97
		$idata = str_replace('{ADMINEMAIL}', $this->adminemail, $idata);
98
		$idata = str_replace('{ADMINPASS}', $this->adminpass, $idata);
99
		$idata = str_replace('{IMAGEPATH}', $this->imgPath, $idata);
100
		$idata = str_replace('{IMAGEURL}', $this->imgUrl, $idata);
101
		$idata = str_replace('{FILEMANAGERPATH}', $this->fileManagerPath, $idata);
102
		$idata = str_replace('{MANAGERLANGUAGE}', $this->managerlanguage, $idata);
103
		$idata = str_replace('{AUTOTEMPLATELOGIC}', $this->autoTemplateLogic, $idata);
104
		/*$idata = str_replace('{VERSION}', $modx_version, $idata);*/
105
106
		// Replace custom placeholders
107
		foreach($custom_placeholders as $key=>$val) {
108 View Code Duplication
			if (strpos($idata, '{'.$key.'}') !== false) {
109
				$idata = str_replace('{'.$key.'}', $val, $idata);
110
			}
111
		}
112
113
		$sql_array = explode("\n\n", $idata);
114
115
		$num = 0;
116
		foreach($sql_array as $sql_entry) {
117
			$sql_do = trim($sql_entry, "\r\n; ");
118
119
			if (preg_match('/^\#/', $sql_do)) continue;
120
121
			// strip out comments and \n for mysql 3.x
122
			if ($this->dbVersion <4.0) {
123
				$sql_do = preg_replace("~COMMENT.*[^']?'.*[^']?'~","",$sql_do);
124
				$sql_do = str_replace('\r', "", $sql_do);
125
				$sql_do = str_replace('\n', "", $sql_do);
126
			}
127
128
129
			$num = $num + 1;
130
			if ($sql_do) mysqli_query($this->conn, $sql_do);
131
			if(mysqli_error($this->conn)) {
132
				// Ignore duplicate and drop errors - Raymond
133
				if ($this->ignoreDuplicateErrors){
134
					if (mysqli_errno($this->conn) == 1060 || mysqli_errno($this->conn) == 1061 || mysqli_errno($this->conn) == 1062 ||mysqli_errno($this->conn) == 1091) continue;
135
				}
136
				// End Ignore duplicate
137
				$this->mysqlErrors[] = array("error" => mysqli_error($this->conn), "sql" => $sql_do);
138
				$this->installFailed = true;
139
			}
140
		}
141
	}
142
143
    public function getTableEncoding()
144
    {
145
        $out = 'DEFAULT CHARSET=' . $this->connection_charset;
146
        if (!empty($this->database_collation)) {
147
            $out .= ' COLLATE=' . $this->database_collation;
0 ignored issues
show
Bug introduced by
The property database_collation 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...
148
        }
149
        return $out;
150
    }
151
152
    public function close() {
153
		mysqli_close($this->conn);
154
	}
155
}
156