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 |
||
| 16 | class Sqlite extends AbstractConnection |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Create a new SQLite database connection. |
||
| 20 | * |
||
| 21 | * Creates an in-memory database if no path is given. |
||
| 22 | * |
||
| 23 | * @param string $path [optional] The path to the SQLite database file |
||
| 24 | * @param array $options [optional] Options for the SQLite PDO connection |
||
| 25 | */ |
||
| 26 | public function __construct($path = null, array $options = array()) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Initiate the connection. |
||
| 36 | */ |
||
| 37 | public function connect() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Close the connection. |
||
| 58 | */ |
||
| 59 | public function disconnect() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Retrieve the query translator. |
||
| 66 | * |
||
| 67 | * @return Translator |
||
| 68 | */ |
||
| 69 | public function translator() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Query the database. |
||
| 80 | * |
||
| 81 | * @param Query|string $query |
||
| 82 | * @param array $parameters [optional] |
||
| 83 | */ |
||
| 84 | public function query($query, array $parameters = array()) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Retrieve error information regarding the last query or connection |
||
| 117 | * attempt. |
||
| 118 | * |
||
| 119 | * Returns null if there is no error. |
||
| 120 | * |
||
| 121 | * @return Error |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function error() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve error information from the PDO connection object. |
||
| 140 | * |
||
| 141 | * Returns null if there is no error. |
||
| 142 | * |
||
| 143 | * @return Error |
||
| 144 | */ |
||
| 145 | protected function connectionError() |
||
| 159 | } |
||
| 160 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: