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 |
||
| 31 | abstract class ModelLayer extends Client |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * getClientType |
||
| 35 | * |
||
| 36 | * @see ClientInterface |
||
| 37 | */ |
||
| 38 | public function getClientType() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * getClientIdentifier |
||
| 45 | * |
||
| 46 | * @see ClientInterface |
||
| 47 | */ |
||
| 48 | public function getClientIdentifier() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * shutdown |
||
| 55 | * |
||
| 56 | * @see ClientInterface |
||
| 57 | */ |
||
| 58 | public function shutdown() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * startTransaction |
||
| 64 | * |
||
| 65 | * Start a new transaction. |
||
| 66 | * |
||
| 67 | * @access protected |
||
| 68 | * @return ModelLayer $this |
||
| 69 | */ |
||
| 70 | protected function startTransaction() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * setDeferrable |
||
| 79 | * |
||
| 80 | * Set given constraints to deferred/immediate in the current transaction. |
||
| 81 | * This applies to constraints being deferrable or deferred by default. |
||
| 82 | * If the keys is an empty arrays, ALL keys will be set at the given state. |
||
| 83 | * @see http://www.postgresql.org/docs/9.0/static/sql-set-constraints.html |
||
| 84 | * |
||
| 85 | * @access protected |
||
| 86 | * @param array $keys |
||
| 87 | * @param string $state |
||
| 88 | * @throws ModelLayerException if not valid state |
||
| 89 | * @return ModelLayer $this |
||
| 90 | */ |
||
| 91 | protected function setDeferrable(array $keys, $state) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * setTransactionIsolationLevel |
||
| 139 | * |
||
| 140 | * Transaction isolation level tells PostgreSQL how to manage with the |
||
| 141 | * current transaction. The default is "READ COMMITTED". |
||
| 142 | * @see http://www.postgresql.org/docs/9.0/static/sql-set-transaction.html |
||
| 143 | * |
||
| 144 | * @access protected |
||
| 145 | * @param string $isolation_level |
||
| 146 | * @throws ModelLayerException if not valid isolation level |
||
| 147 | * @return ModelLayer $this |
||
| 148 | */ |
||
| 149 | View Code Duplication | protected function setTransactionIsolationLevel($isolation_level) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * setTransactionAccessMode |
||
| 179 | * |
||
| 180 | * Transaction access modes tell PostgreSQL if transaction are able to |
||
| 181 | * write or read only. |
||
| 182 | * @see http://www.postgresql.org/docs/9.0/static/sql-set-transaction.html |
||
| 183 | * |
||
| 184 | * @access protected |
||
| 185 | * @param string $access_mode |
||
| 186 | * @throws ModelLayerException if not valid access mode |
||
| 187 | * @return ModelLayer $this |
||
| 188 | */ |
||
| 189 | View Code Duplication | protected function setTransactionAccessMode($access_mode) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * setSavePoint |
||
| 218 | * |
||
| 219 | * Set a savepoint in a transaction. |
||
| 220 | * |
||
| 221 | * @access protected |
||
| 222 | * @param string $name |
||
| 223 | * @return ModelLayer $this |
||
| 224 | */ |
||
| 225 | protected function setSavepoint($name) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * releaseSavepoint |
||
| 235 | * |
||
| 236 | * Drop a savepoint. |
||
| 237 | * |
||
| 238 | * @access protected |
||
| 239 | * @param string $name |
||
| 240 | * @return ModelLayer $this |
||
| 241 | */ |
||
| 242 | protected function releaseSavepoint($name) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * rollbackTransaction |
||
| 252 | * |
||
| 253 | * Rollback a transaction. If a name is specified, the transaction is |
||
| 254 | * rollback to the given savepoint. Otherwise, the whole transaction is |
||
| 255 | * rollback. |
||
| 256 | * |
||
| 257 | * @access protected |
||
| 258 | * @param string|null $name |
||
| 259 | * @return ModelLayer $this |
||
| 260 | */ |
||
| 261 | protected function rollbackTransaction($name = null) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * commitTransaction |
||
| 275 | * |
||
| 276 | * Commit a transaction. |
||
| 277 | * |
||
| 278 | * @access protected |
||
| 279 | * @return ModelLayer $this |
||
| 280 | */ |
||
| 281 | protected function commitTransaction() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * isInTransaction |
||
| 290 | * |
||
| 291 | * Tell if a transaction is open or not. |
||
| 292 | * |
||
| 293 | * @see Cient |
||
| 294 | * @access protected |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | protected function isInTransaction() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * isTransactionOk |
||
| 310 | * |
||
| 311 | * In PostgreSQL, an error during a transaction cancels all the queries and |
||
| 312 | * rollback the transaction on commit. This method returns the current |
||
| 313 | * transaction's status. If no transactions are open, it returns null. |
||
| 314 | * |
||
| 315 | * @access public |
||
| 316 | * @return bool|null |
||
| 317 | */ |
||
| 318 | protected function isTransactionOk() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * sendNotify |
||
| 335 | * |
||
| 336 | * Send a NOTIFY event to the database server. An optional data can be sent |
||
| 337 | * with the notification. |
||
| 338 | * |
||
| 339 | * @access protected |
||
| 340 | * @param string $channel |
||
| 341 | * @param string $data |
||
| 342 | * @return ModelLayer $this |
||
| 343 | */ |
||
| 344 | protected function sendNotify($channel, $data = '') |
||
| 352 | |||
| 353 | /** |
||
| 354 | * executeAnonymousQuery |
||
| 355 | * |
||
| 356 | * Proxy to Connection::executeAnonymousQuery() |
||
| 357 | * |
||
| 358 | * @access protected |
||
| 359 | * @param string $sql |
||
| 360 | * @return ResultHandler |
||
| 361 | */ |
||
| 362 | protected function executeAnonymousQuery($sql) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * escapeIdentifier |
||
| 373 | * |
||
| 374 | * Proxy to Connection::escapeIdentifier() |
||
| 375 | * |
||
| 376 | * @access protected |
||
| 377 | * @param string $string |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | protected function escapeIdentifier($string) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * escapeLiteral |
||
| 391 | * |
||
| 392 | * Proxy to Connection::escapeLiteral() |
||
| 393 | * |
||
| 394 | * @access protected |
||
| 395 | * @param string $string |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | protected function escapeLiteral($string) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * getModel |
||
| 409 | * |
||
| 410 | * Proxy to Session::getModel(); |
||
| 411 | * |
||
| 412 | * @access protected |
||
| 413 | * @param string model identifier |
||
| 414 | * @return Model |
||
| 415 | */ |
||
| 416 | protected function getModel($identifier) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * sendParameter |
||
| 425 | * |
||
| 426 | * Send a parameter to the server. |
||
| 427 | * The parameter MUST have been properly checked and escaped if needed as |
||
| 428 | * it is going to be passed AS IS to the server. Sending untrusted |
||
| 429 | * parameters may lead to potential SQL injection. |
||
| 430 | * |
||
| 431 | * @access private |
||
| 432 | * @param string $sql |
||
| 433 | * @param string $identifier |
||
| 434 | * @param string $parameter |
||
| 435 | * @return ModelLayer $this |
||
| 436 | */ |
||
| 437 | private function sendParameter($sql, $identifier, $parameter = null) |
||
| 450 | } |
||
| 451 |