| Total Complexity | 42 |
| Total Lines | 204 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractPdoDatabase 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 AbstractPdoDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | abstract class AbstractPdoDatabase extends \WebServCo\Framework\AbstractLibrary |
||
| 7 | { |
||
| 8 | protected $db; |
||
| 9 | protected $stmt; |
||
| 10 | protected $rows; |
||
| 11 | |||
| 12 | use \WebServCo\Framework\Traits\DatabaseTrait; |
||
| 13 | use \WebServCo\Framework\Traits\DatabaseAddQueryTrait; |
||
| 14 | |||
| 15 | abstract protected function getDataSourceName($host, $port, $dbname); |
||
| 16 | |||
| 17 | public function __construct($settings = []) |
||
| 18 | { |
||
| 19 | parent::__construct($settings); |
||
| 20 | |||
| 21 | try { |
||
| 22 | $dsn = $this->getDataSourceName( |
||
| 23 | $this->setting('connection/host', '127.0.0.1'), |
||
| 24 | $this->setting('connection/port', null), |
||
| 25 | $this->setting('connection/dbname', 'test') |
||
| 26 | ); |
||
| 27 | $this->db = new \PDO( |
||
| 28 | $dsn, |
||
| 29 | $this->setting('connection/username', 'root'), |
||
| 30 | $this->setting('connection/passwd', ''), |
||
| 31 | [ |
||
| 32 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
||
| 33 | \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
||
| 34 | \PDO::ATTR_EMULATE_PREPARES => false, |
||
| 35 | \PDO::ATTR_PERSISTENT => false |
||
| 36 | ] |
||
| 37 | ); |
||
| 38 | } catch (\Exception $e) { // PDOException/RuntimeException/Exception |
||
| 39 | throw new DatabaseException($e->getMessage(), $e); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | public function affectedRows() |
||
| 44 | { |
||
| 45 | if (!($this->stmt instanceof \PDOStatement)) { |
||
| 46 | throw new DatabaseException('No Statement object available.'); |
||
| 47 | } |
||
| 48 | return $this->stmt->rowCount(); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function escape($string) |
||
| 52 | { |
||
| 53 | return $this->db->quote($string); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function getColumn($query, $params = [], $columnNumber = 0) |
||
| 57 | { |
||
| 58 | $this->query($query, $params); |
||
| 59 | return $this->stmt->fetchColumn($columnNumber); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function getRow($query, $params = []) |
||
| 63 | { |
||
| 64 | $this->query($query, $params); |
||
| 65 | return $this->stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getRows($query, $params = []) |
||
| 69 | { |
||
| 70 | $this->query($query, $params); |
||
| 71 | $this->rows = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 72 | return $this->rows; |
||
| 73 | } |
||
| 74 | |||
| 75 | /* |
||
| 76 | * Get last inserted Id. |
||
| 77 | * |
||
| 78 | * https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id |
||
| 79 | * If you insert multiple rows using a single INSERT statement, |
||
| 80 | * LAST_INSERT_ID() returns the value generated for the first inserted row only. |
||
| 81 | * The reason for this is to make it possible to reproduce easily the same |
||
| 82 | * INSERT statement against some other server. |
||
| 83 | * |
||
| 84 | * PDO: |
||
| 85 | * Returns the ID of the last inserted row, or the last value from a sequence object, |
||
| 86 | * depending on the underlying driver. |
||
| 87 | * For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter. |
||
| 88 | */ |
||
| 89 | public function lastInsertId($name = null) |
||
| 92 | } |
||
| 93 | |||
| 94 | public function numRows() |
||
| 95 | { |
||
| 96 | if (!($this->stmt instanceof \PDOStatement)) { |
||
| 97 | throw new DatabaseException('No Statement object available.'); |
||
| 98 | } |
||
| 99 | if ('mysql' == $this->setting('driver')) { |
||
| 100 | return $this->stmt->rowCount(); |
||
| 101 | } |
||
| 102 | $rows = $this->rows ?: $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
| 103 | return count($rows); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function query($query, $params = []) |
||
| 107 | { |
||
| 108 | if (empty($query)) { |
||
| 109 | throw new DatabaseException('No query specified'); |
||
| 110 | } |
||
| 111 | |||
| 112 | try { |
||
| 113 | if (!empty($params)) { |
||
| 114 | $this->stmt = $this->db->prepare($query); |
||
| 115 | $this->bindParams($params); |
||
| 116 | $this->stmt->execute(); |
||
| 117 | } else { |
||
| 118 | $this->stmt = $this->db->query($query); |
||
| 119 | } |
||
| 120 | return $this->stmt; |
||
| 121 | } catch (\PDOException $e) { |
||
| 122 | throw new DatabaseException($e->getMessage(), $e); |
||
| 123 | } catch (\RuntimeException $e) { |
||
| 124 | throw new DatabaseException($e->getMessage(), $e); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | public function transaction($queries) |
||
| 129 | { |
||
| 130 | try { |
||
| 131 | $this->db->beginTransaction(); |
||
| 132 | foreach ($queries as $item) { |
||
| 133 | if (!isset($item[0])) { |
||
| 134 | throw new DatabaseException('No query specified'); |
||
| 135 | } |
||
| 136 | $params = isset($item[1]) ? $item[1] : []; |
||
| 137 | $this->query($item[0], $params); |
||
| 138 | } |
||
| 139 | $this->db->commit(); |
||
| 140 | return true; |
||
| 141 | // \WebServCo\Framework\Exceptions\DatabaseException |
||
| 142 | // PDOException/RuntimeException/Exception |
||
| 143 | } catch (\Exception $e) { |
||
| 144 | $this->db->rollBack(); |
||
| 145 | throw new DatabaseException($e->getMessage(), $e); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | protected function bindParams($data) |
||
| 174 | } |
||
| 175 | |||
| 176 | protected function getDataType($variable) |
||
| 177 | { |
||
| 178 | $type = gettype($variable); |
||
| 179 | |||
| 180 | switch ($type) { |
||
| 181 | case 'NULL': |
||
| 182 | return \PDO::PARAM_NULL; |
||
| 183 | break; |
||
|
|
|||
| 184 | case 'integer': |
||
| 185 | return \PDO::PARAM_INT; |
||
| 186 | break; |
||
| 187 | case 'boolean': |
||
| 188 | // causes data not to be inserted |
||
| 189 | //return \PDO::PARAM_BOOL; |
||
| 190 | //break; |
||
| 191 | case 'string': |
||
| 192 | case 'double': |
||
| 193 | case 'array': |
||
| 194 | case 'object': |
||
| 195 | case 'resource': |
||
| 196 | case 'resource (closed)': |
||
| 197 | case 'unknown type': |
||
| 198 | default: |
||
| 199 | return \PDO::PARAM_STR; |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | protected function validateParam($param) |
||
| 210 | } |
||
| 211 | } |
||
| 212 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.