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:
Complex classes like DatabaseDriver 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DatabaseDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class DatabaseDriver implements DriverInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var ConnectionManager |
||
| 30 | */ |
||
| 31 | private $connections; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var QueryBuilder |
||
| 35 | */ |
||
| 36 | private $connection; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Sets the connection manager. |
||
| 40 | * |
||
| 41 | * @param ConnectionManager $manager |
||
| 42 | * |
||
| 43 | * @return self |
||
| 44 | */ |
||
| 45 | public function setConnectionManager(ConnectionManager $manager) |
||
| 46 | { |
||
| 47 | $this->connections = $manager; |
||
| 48 | |||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Gets the connection manager. |
||
| 54 | * |
||
| 55 | * @return ConnectionManager |
||
| 56 | */ |
||
| 57 | public function getConnectionManager() |
||
| 58 | { |
||
| 59 | return $this->connections; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the default database connection. |
||
| 64 | * |
||
| 65 | * @param QueryBuilder $db |
||
| 66 | * |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | public function setConnection(QueryBuilder $db) |
||
| 70 | { |
||
| 71 | $this->connection = $db; |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Gets the database connection. |
||
| 78 | * |
||
| 79 | * @param string|false $id connection ID |
||
| 80 | * |
||
| 81 | * @throws DriverException when the connection has not been set yet |
||
| 82 | * |
||
| 83 | * @return QueryBuilder |
||
| 84 | */ |
||
| 85 | public function getConnection($id) |
||
| 86 | { |
||
| 87 | if ($this->connections) { |
||
| 88 | try { |
||
| 89 | if ($id) { |
||
|
|
|||
| 90 | return $this->connections->get($id); |
||
| 91 | } else { |
||
| 92 | return $this->connections->getDefault(); |
||
| 93 | } |
||
| 94 | } catch (JAQBException $e) { |
||
| 95 | throw new DriverException($e->getMessage()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!$this->connection) { |
||
| 100 | throw new DriverException('The database driver has not been given a connection!'); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $this->connection; |
||
| 104 | } |
||
| 105 | |||
| 106 | View Code Duplication | public function createModel(Model $model, array $parameters) |
|
| 107 | { |
||
| 108 | $values = $this->serialize($parameters); |
||
| 109 | $tablename = $model->getTablename(); |
||
| 110 | $db = $this->getConnection($model->getConnection()); |
||
| 111 | |||
| 112 | try { |
||
| 113 | return $db->insert($values) |
||
| 114 | ->into($tablename) |
||
| 115 | ->execute() instanceof PDOStatement; |
||
| 116 | } catch (PDOException $original) { |
||
| 117 | $e = new DriverException('An error occurred in the database driver when creating the '.$model::modelName().': '.$original->getMessage()); |
||
| 118 | $e->setException($original); |
||
| 119 | throw $e; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getCreatedID(Model $model, $propertyName) |
||
| 124 | { |
||
| 125 | try { |
||
| 126 | $id = $this->getConnection($model->getConnection())->lastInsertId(); |
||
| 127 | } catch (PDOException $original) { |
||
| 128 | $e = new DriverException('An error occurred in the database driver when getting the ID of the new '.$model::modelName().': '.$original->getMessage()); |
||
| 129 | $e->setException($original); |
||
| 130 | throw $e; |
||
| 131 | } |
||
| 132 | |||
| 133 | return Model::cast($model::getProperty($propertyName), $id); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function loadModel(Model $model) |
||
| 137 | { |
||
| 138 | $tablename = $model->getTablename(); |
||
| 139 | $db = $this->getConnection($model->getConnection()); |
||
| 140 | |||
| 141 | try { |
||
| 142 | $row = $db->select('*') |
||
| 143 | ->from($tablename) |
||
| 144 | ->where($model->ids()) |
||
| 145 | ->one(); |
||
| 146 | } catch (PDOException $original) { |
||
| 147 | $e = new DriverException('An error occurred in the database driver when loading an instance of '.$model::modelName().': '.$original->getMessage()); |
||
| 148 | $e->setException($original); |
||
| 149 | throw $e; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!is_array($row)) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $row; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function updateModel(Model $model, array $parameters) |
||
| 160 | { |
||
| 161 | if (count($parameters) == 0) { |
||
| 162 | return true; |
||
| 163 | } |
||
| 164 | |||
| 165 | $values = $this->serialize($parameters); |
||
| 166 | $tablename = $model->getTablename(); |
||
| 167 | $db = $this->getConnection($model->getConnection()); |
||
| 168 | |||
| 169 | try { |
||
| 170 | return $db->update($tablename) |
||
| 171 | ->values($values) |
||
| 172 | ->where($model->ids()) |
||
| 173 | ->execute() instanceof PDOStatement; |
||
| 174 | } catch (PDOException $original) { |
||
| 175 | $e = new DriverException('An error occurred in the database driver when updating the '.$model::modelName().': '.$original->getMessage()); |
||
| 176 | $e->setException($original); |
||
| 177 | throw $e; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | View Code Duplication | public function deleteModel(Model $model) |
|
| 182 | { |
||
| 183 | $tablename = $model->getTablename(); |
||
| 184 | $db = $this->getConnection($model->getConnection()); |
||
| 185 | |||
| 186 | try { |
||
| 187 | return $db->delete($tablename) |
||
| 188 | ->where($model->ids()) |
||
| 189 | ->execute() instanceof PDOStatement; |
||
| 190 | } catch (PDOException $original) { |
||
| 191 | $e = new DriverException('An error occurred in the database driver while deleting the '.$model::modelName().': '.$original->getMessage()); |
||
| 192 | $e->setException($original); |
||
| 193 | throw $e; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | public function queryModels(Query $query) |
||
| 198 | { |
||
| 199 | $modelClass = $query->getModel(); |
||
| 200 | $model = new $modelClass(); |
||
| 201 | $tablename = $model->getTablename(); |
||
| 202 | |||
| 203 | // build a DB query from the model query |
||
| 204 | $dbQuery = $this->getConnection($model->getConnection()) |
||
| 205 | ->select($this->prefixSelect('*', $tablename)) |
||
| 206 | ->from($tablename) |
||
| 207 | ->where($this->prefixWhere($query->getWhere(), $tablename)) |
||
| 208 | ->limit($query->getLimit(), $query->getStart()) |
||
| 209 | ->orderBy($this->prefixSort($query->getSort(), $tablename)); |
||
| 210 | |||
| 211 | // join conditions |
||
| 212 | foreach ($query->getJoins() as $join) { |
||
| 213 | list($foreignModelClass, $column, $foreignKey) = $join; |
||
| 214 | |||
| 215 | $foreignModel = new $foreignModelClass(); |
||
| 216 | $foreignTablename = $foreignModel->getTablename(); |
||
| 217 | $condition = $this->prefixColumn($column, $tablename).'='.$this->prefixColumn($foreignKey, $foreignTablename); |
||
| 218 | |||
| 219 | $dbQuery->join($foreignTablename, $condition); |
||
| 220 | } |
||
| 221 | |||
| 222 | try { |
||
| 223 | return $dbQuery->all(); |
||
| 224 | } catch (PDOException $original) { |
||
| 225 | $e = new DriverException('An error occurred in the database driver while performing the '.$model::modelName().' query: '.$original->getMessage()); |
||
| 226 | $e->setException($original); |
||
| 227 | throw $e; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | public function totalRecords(Query $query) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Marshals a value to storage. |
||
| 252 | * |
||
| 253 | * @param mixed $value |
||
| 254 | * |
||
| 255 | * @return mixed serialized value |
||
| 256 | */ |
||
| 257 | public function serializeValue($value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Serializes an array of values. |
||
| 269 | * |
||
| 270 | * @param array $values |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | private function serialize(array $values) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a prefixed select statement. |
||
| 285 | * |
||
| 286 | * @param string $columns |
||
| 287 | * @param string $tablename |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function prefixSelect($columns, $tablename) |
||
| 292 | { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns a prefixed where statement. |
||
| 303 | * |
||
| 304 | * @param string $columns |
||
| 305 | * @param string $tablename |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | private function prefixWhere(array $where, $tablename) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns a prefixed sort statement. |
||
| 331 | * |
||
| 332 | * @param string $columns |
||
| 333 | * @param string $tablename |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | private function prefixSort(array $sort, $tablename) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Prefix columns with tablename that contains only |
||
| 348 | * alphanumeric/underscores/*. |
||
| 349 | * |
||
| 350 | * @param string $column |
||
| 351 | * @param string $tablename |
||
| 352 | * |
||
| 353 | * @return string prefixed column |
||
| 354 | */ |
||
| 355 | private function prefixColumn($column, $tablename) |
||
| 363 | } |
||
| 364 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: