Completed
Push — master ( a25af9...a12ace )
by Federico
02:41
created

Query::queryInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
	class Query extends Module {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
		public $connection;
4
		public $currentConnection;
5
		public function __construct() {
6
			parent::__construct();
7
			$this->connection = [];
8
			$this->currentConnection = null;
9
		}
10
		public function addConnection( $_name, $_connection ) {
11
			$this->connection["$_name"] = $_connection;
12
			$this->currentConnection = $_connection;
13
		}
14
		public function setConnection( $_name ) {
15
			$this->currentConnection = $this->connection["$_name"];
16
		}
17
		public function query( $_query ) {
18
			$this->stdQuery($_query);
19
			return true;
20
		}
21
		public function queryInsert( $_query ) {
22
			$temp = $this->stdQuery($_query);
0 ignored issues
show
Unused Code introduced by
$temp 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...
23
			return $this->currentConnection->database->lastInsertId();
24
		}
25
26
		public function queryFetch( $_query ) {
27
			$temp = $this->stdQuery($_query);
28
			return $temp->fetchAll(PDO::FETCH_ASSOC);
29
		}
30
31
		public function queryArray( $_query ) {
32
			$temp = $this->stdQuery($_query);
33
			return $temp->fetchAll(PDO::FETCH_COLUMN, 0);
34
		}
35
36
		private function stdQuery($_query) {
37
			$database = $this->currentConnection->database;
38
			$error = "Errore query [$_query]";
39
			$query = $database->prepare($_query);
40
			$_result = $query->execute();
41
			if(!$_result) {
42
				echo "$_query<br>";
43
				echo "Something wrong: ".$error;
44
				var_dump($query->errorInfo());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query->errorInfo()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
45
				var_dump($database->errorInfo());
46
				exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method stdQuery() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
47
			}
48
			return $query;
49
		}
50
	}
51
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
52