Completed
Push — master ( c58c9b...07c22d )
by Jacob
02:25
created

MySqlDatabase   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 15.31 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 15
loc 98
rs 10
c 1
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A connect() 0 6 2
A changeDatabase() 0 8 3
A lazyLoadConnection() 0 4 2
A query() 0 11 3
A logQuery() 0 7 1
A getTime() 0 7 1
A getReadableTime() 15 15 4
A __destruct() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/* - - - - - - - - - - - - - - - - - - - - -
4
5
 Title : PHP Quick Profiler MySQL Class
6
 Author : Created by Ryan Campbell
7
 URL : http://particletree.com/features/php-quick-profiler/
8
9
 Last Updated : April 22, 2009
10
11
 Description : A simple database wrapper that includes
12
 logging of queries.
13
14
- - - - - - - - - - - - - - - - - - - - - */
15
16
namespace Particletree\Pqp;
17
18
class MySqlDatabase {
19
20
	private $host;			
21
	private $user;		
22
	private $password;	
23
	private $database;	
24
	public $queryCount = 0;
25
	public $queries = array();
26
	public $conn;
27
	
28
	/*------------------------------------
29
	          CONFIG CONNECTION
30
	------------------------------------*/
31
	
32
	function __construct($host, $user, $password) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
		$this->host = $host;
34
		$this->user = $user;
35
		$this->password = $password;
36
	}
37
	
38
	function connect($new = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
		$this->conn = mysql_connect($this->host, $this->user, $this->password, $new);
40
		if(!$this->conn) {
41
			throw new Exception('We\'re working on a few connection issues.');
42
		}
43
	}
44
	
45
	function changeDatabase($database) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
		$this->database = $database;
47
		if($this->conn) {
48
			if(!mysql_select_db($database, $this->conn)) {
49
				throw new CustomException('We\'re working on a few connection issues.');
50
			}
51
		}
52
	}
53
	
54
	function lazyLoadConnection() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
		$this->connect(true);
56
		if($this->database) $this->changeDatabase($this->database);
57
	}
58
	
59
	/*-----------------------------------
60
	   				QUERY
61
	------------------------------------*/
62
	
63
	function query($sql) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
		if(!$this->conn) $this->lazyLoadConnection();
65
		$start = $this->getTime();
66
		$rs = mysql_query($sql, $this->conn);
67
		$this->queryCount += 1;
68
		$this->logQuery($sql, $start);
69
		if(!$rs) {
70
			throw new Exception('Could not execute query.');
71
		}
72
		return $rs;
73
	}
74
	
75
	/*-----------------------------------
76
	          	DEBUGGING
77
	------------------------------------*/
78
	
79
	function logQuery($sql, $start) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
		$query = array(
81
				'sql' => $sql,
82
				'time' => ($this->getTime() - $start)*1000
83
			);
84
		array_push($this->queries, $query);
85
	}
86
	
87
	function getTime() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
		$time = microtime();
89
		$time = explode(' ', $time);
90
		$time = $time[1] + $time[0];
91
		$start = $time;
92
		return $start;
93
	}
94
	
95 View Code Duplication
	public function getReadableTime($time) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
		$ret = $time;
97
		$formatter = 0;
98
		$formats = array('ms', 's', 'm');
99
		if($time >= 1000 && $time < 60000) {
100
			$formatter = 1;
101
			$ret = ($time / 1000);
102
		}
103
		if($time >= 60000) {
104
			$formatter = 2;
105
			$ret = ($time / 1000) / 60;
106
		}
107
		$ret = number_format($ret,3,'.','') . ' ' . $formats[$formatter];
108
		return $ret;
109
	}
110
	
111
	function __destruct()  {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
		@mysql_close($this->conn);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
113
	}
114
	
115
}
116
117
?>
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...
118