Passed
Push — master ( b494ba...29c51f )
by Jean-Christophe
11:04
created

PDOWrapper   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Test Coverage

Coverage 72.36%

Importance

Changes 9
Bugs 1 Features 1
Metric Value
wmc 51
eloc 90
c 9
b 1
f 1
dl 0
loc 222
ccs 89
cts 123
cp 0.7236
rs 7.92

38 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeys() 0 2 1
A query() 0 2 1
A queryColumn() 0 2 1
A executeStatement() 0 2 1
A savePoint() 0 2 1
A getRowNum() 0 2 1
A commit() 0 2 1
A getFieldsInfos() 0 2 1
A releasePoint() 0 2 1
A rollbackPoint() 0 2 1
A lastInsertId() 0 2 1
A beginTransaction() 0 2 1
A inTransaction() 0 2 1
A getDriverMetaDatas() 0 10 3
A queryAll() 0 2 1
A rollBack() 0 2 1
A toStringOperator() 0 2 1
A ping() 0 2 2
A fetchOne() 0 8 2
A prepareStatement() 0 2 1
A getStatement() 0 4 1
A statementRowCount() 0 2 1
A fetchAllColumn() 0 7 2
A getTablesName() 0 2 1
A fetchAll() 0 8 2
A bindValueFromStatement() 0 2 1
A fetchColumn() 0 5 2
A nestable() 0 2 1
A groupConcat() 0 2 1
A __construct() 0 3 1
A connect() 0 7 2
A getAvailableDrivers() 0 2 1
A getForeignKeys() 0 2 1
A _optPrepareAndExecute() 0 12 3
A getDSN() 0 6 2
A _optExecuteAndFetch() 0 11 3
A execute() 0 2 1
A quoteValue() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like PDOWrapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PDOWrapper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\db\providers\pdo;
4
5
use Ubiquity\db\providers\AbstractDbWrapper;
6
use Ubiquity\exceptions\DBException;
7
8
/**
9
 * Ubiquity\db\providers$PDOWrapper
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.3
14
 * @property \PDO $dbInstance
15
 *
16
 */
17
class PDOWrapper extends AbstractDbWrapper {
18
	protected static $savepointsDrivers = [ 'pgsql' => true,'mysql' => true,'sqlite' => true ];
19
	private static $quotes = [ 'mysql' => '`','sqlite' => '"','pgsql' => '"' ];
20
	protected $driversMetasClasses = [ 'mysql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\MysqlDriverMetas','pgsql' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\PgsqlDriverMetas','sqlite' => '\\Ubiquity\\db\\providers\\pdo\\drivers\\SqliteDriverMetas' ];
21
	protected $transactionLevel = 0;
22
	protected $dbType;
23
	protected $driverMetaDatas;
24
25
	/**
26
	 *
27
	 * @throws DBException
28
	 * @return \Ubiquity\db\providers\pdo\drivers\AbstractDriverMetaDatas
29
	 */
30 30
	protected function getDriverMetaDatas() {
31 30
		if (! isset ( $this->driverMetaDatas )) {
32 30
			if (isset ( $this->driversMetasClasses [$this->dbType] )) {
33 30
				$metaClass = $this->driversMetasClasses [$this->dbType];
34 30
				$this->driverMetaDatas = new $metaClass ( $this->dbInstance );
35
			} else {
36
				throw new DBException ( "{$this->dbType} driver is not yet implemented!" );
37
			}
38
		}
39 30
		return $this->driverMetaDatas;
40
	}
41
42 89
	public function __construct($dbType = 'mysql') {
43 89
		$this->quote = self::$quotes [$dbType] ?? '';
44 89
		$this->dbType = $dbType;
45 89
	}
46
47
	public function fetchAllColumn($statement, array $values = null, string $column = null) {
48
		$result = false;
49
		if ($statement->execute ( $values )) {
50
			$result = $statement->fetchAll ( \PDO::FETCH_COLUMN, $column );
51
		}
52
		$statement->closeCursor ();
53
		return $result;
54
	}
55
56 10
	public function lastInsertId($name = null) {
57 10
		return $this->dbInstance->lastInsertId ( $name );
58
	}
59
60 23
	public function fetchAll($statement, array $values = null, $mode = null) {
61 23
		if ($statement->execute ( $values )) {
62 23
			$result = $statement->fetchAll ( $mode ?? \PDO::FETCH_ASSOC);
63 23
			$statement->closeCursor ();
64 23
			return $result;
65
		}
66
67
		return false;
68
	}
69
70
	public function fetchOne($statement, array $values = null, $mode = null) {
71
		if ($statement->execute ( $values )) {
72
			$result = $statement->fetch ( $mode ?? \PDO::FETCH_ASSOC);
73
			$statement->closeCursor ();
74
			return $result;
75
		}
76
77
		return false;
78
	}
79
80 2
	public static function getAvailableDrivers() {
81 2
		return \PDO::getAvailableDrivers ();
82
	}
83
84 30
	public function prepareStatement(string $sql) {
85 30
		return $this->dbInstance->prepare ( $sql );
86
	}
87
88
	public function fetchColumn($statement, array $values = null, int $columnNumber = null) {
89
		if ($statement->execute ( $values )) {
90
			return $statement->fetchColumn ( $columnNumber );
91
		}
92
		return false;
93
	}
94
95 78
	public function getStatement($sql) {
96 78
		$st = $this->dbInstance->prepare ( $sql );
97 78
		$st->setFetchMode ( \PDO::FETCH_ASSOC );
98 78
		return $st;
99
	}
100
101 1
	public function execute($sql) {
102 1
		return $this->dbInstance->exec ( $sql );
103
	}
104
105 84
	public function connect(string $dbType, $dbName, $serverName, string $port, string $user, string $password, array $options) {
106 84
		$options [\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
107 84
		if (isset ( $options ['quote'] )) {
108
			$this->quote = $options ['quote'];
109
			unset ( $options ['quote'] );
110
		}
111 84
		$this->dbInstance = new \PDO ( $this->getDSN ( $serverName, $port, $dbName, $dbType ), $user, $password, $options );
112 84
	}
113
114 84
	public function getDSN(string $serverName, string $port, string $dbName, string $dbType = 'mysql') {
115 84
		$charsetString = [ 'mysql' => 'charset=UTF8','pgsql' => 'options=\'--client_encoding=UTF8\'','sqlite' => '' ] [$dbType] ?? 'charset=UTF8';
116 84
		if ($dbType === 'sqlite') {
117
			return "sqlite:{$dbName}";
118
		}
119 84
		return $dbType . ":dbname={$dbName};host={$serverName};{$charsetString};port=" . $port;
120
	}
121
122
	public function bindValueFromStatement($statement, $parameter, $value) {
123
		return $statement->bindValue ( ":" . $parameter, $value );
124
	}
125
126
	public function query(string $sql) {
127
		return $this->dbInstance->query ( $sql );
128
	}
129
130
	public function queryAll(string $sql, int $fetchStyle = null) {
131
		return $this->dbInstance->query ( $sql )->fetchAll ( $fetchStyle );
132
	}
133
134 4
	public function queryColumn(string $sql, int $columnNumber = null) {
135 4
		return $this->dbInstance->query ( $sql )->fetchColumn ( $columnNumber );
136
	}
137
138
	public function executeStatement($statement, array $values = null) {
139
		return $statement->execute ( $values );
140
	}
141
142 3
	public function getTablesName() {
143 3
		return $this->getDriverMetaDatas ()->getTablesName ();
144
	}
145
146
	public function statementRowCount($statement) {
147
		return $statement->rowCount ();
148
	}
149
150
	public function inTransaction() {
151
		return $this->dbInstance->inTransaction ();
152
	}
153
154 5
	public function commit() {
155 5
		return $this->dbInstance->commit ();
156
	}
157
158 3
	public function rollBack() {
159 3
		return $this->dbInstance->rollBack ();
160
	}
161
162 9
	public function beginTransaction() {
163 9
		return $this->dbInstance->beginTransaction ();
164
	}
165
166 4
	public function savePoint($level) {
167 4
		$this->dbInstance->exec ( 'SAVEPOINT LEVEL' . $level );
168 4
	}
169
170 2
	public function releasePoint($level) {
171 2
		$this->dbInstance->exec ( 'RELEASE SAVEPOINT LEVEL' . $level );
172 2
	}
173
174 2
	public function rollbackPoint($level) {
175 2
		$this->dbInstance->exec ( 'ROLLBACK TO SAVEPOINT LEVEL' . $level );
176 2
	}
177
178 4
	public function nestable() {
179 4
		return isset ( self::$savepointsDrivers [$this->dbType] );
180
	}
181
182 4
	public function ping() {
183 4
		return ($this->dbInstance != null) && (1 === \intval ( $this->queryColumn ( 'SELECT 1', 0 ) ));
184
	}
185
186 1
	public function getPrimaryKeys($tableName) {
187 1
		return $this->getDriverMetaDatas ()->getPrimaryKeys ( $tableName );
188
	}
189
190 1
	public function getForeignKeys($tableName, $pkName, $dbName = null) {
191 1
		return $this->getDriverMetaDatas ()->getForeignKeys ( $tableName, $pkName, $dbName );
192
	}
193
194 1
	public function getFieldsInfos($tableName) {
195 1
		return $this->getDriverMetaDatas ()->getFieldsInfos ( $tableName );
196
	}
197
198 70
	public function _optPrepareAndExecute($sql, array $values = null, $one = false) {
199 70
		$statement = $this->_getStatement ( $sql );
200 70
		$result = false;
201 70
		if ($statement->execute ( $values )) {
202 70
			if ($one) {
203 42
				$result = $statement->fetch ( \PDO::FETCH_ASSOC );
204
			} else {
205 61
				$result = $statement->fetchAll ( \PDO::FETCH_ASSOC );
206
			}
207
		}
208 70
		$statement->closeCursor ();
209 70
		return $result;
210
	}
211
212 1
	public function _optExecuteAndFetch($statement, array $values = null, $one = false) {
213 1
		if ($statement->execute ( $values )) {
214 1
			if ($one) {
215 1
				$row = $statement->fetch ( \PDO::FETCH_ASSOC );
216
			} else {
217 1
				$row = $statement->fetchAll ( \PDO::FETCH_ASSOC );
218
			}
219 1
			$statement->closeCursor ();
220 1
			return $row;
221
		}
222
		return false;
223
	}
224
225 1
	public function quoteValue($value, $type = 2) {
226 1
		return $this->dbInstance->quote ( $value, $type );
227
	}
228
229 3
	public function getRowNum(string $tableName, string $pkName, string $condition): int {
230 3
		return $this->getDriverMetaDatas ()->getRowNum ( $tableName, $pkName, $condition );
231
	}
232
233 23
	public function groupConcat(string $fields, string $separator): string {
234 23
		return $this->getDriverMetaDatas ()->groupConcat ( $fields, $separator );
235
	}
236
237 24
	public function toStringOperator() {
238 24
		return $this->getDriverMetaDatas ()->toStringOperator ();
239
	}
240
}
241