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 |
||
| 30 | class PeriodicQuota extends Quota |
||
|
|
|||
| 31 | { |
||
| 32 | /** |
||
| 33 | * The timezone authority. |
||
| 34 | * Used to roll the log records in the database. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $timezone; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The database table name |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $log_table; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Construct instance. |
||
| 49 | * |
||
| 50 | * @param string $connection |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 8 | public function __construct($connection) |
|
| 83 | |||
| 84 | 1 | public function enforce() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Helper. |
||
| 91 | * |
||
| 92 | * TODO: make static? |
||
| 93 | * |
||
| 94 | * @param PeriodicQuota $quota |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 8 | public function dateInTimezone() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Hiiiiit iiiiiit ! :) |
||
| 106 | * Record a hit in the log table. |
||
| 107 | * |
||
| 108 | * @param int $hits so far |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | 3 | View Code Duplication | public function hit($hits) |
| 124 | |||
| 125 | /** |
||
| 126 | * Record a miss in the log table. |
||
| 127 | * |
||
| 128 | * @param int $misses so far |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 1 | View Code Duplication | public function miss($misses) |
| 144 | |||
| 145 | /** |
||
| 146 | * Attempt to... HIT dat a$$! :) |
||
| 147 | * |
||
| 148 | * @param integer $tokens future use. |
||
| 149 | * @return boolean true on success |
||
| 150 | * |
||
| 151 | * @throws ErrorException |
||
| 152 | */ |
||
| 153 | 2 | public function consume($tokens = 1) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Get statistics from log table. |
||
| 177 | * |
||
| 178 | * TODO: REFACTOR rename `date` to `period` |
||
| 179 | * to reflect ability to track different periodic |
||
| 180 | * limits. |
||
| 181 | * |
||
| 182 | * @param string $date |
||
| 183 | * @return stdObject |
||
| 184 | * @throws Exception |
||
| 185 | */ |
||
| 186 | 3 | public function getStats($date) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get the timezone |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 8 | public function getTimezone() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the log table name |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 5 | public function getLogTable() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the timezone authority |
||
| 216 | * |
||
| 217 | * @param string |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function setTimezone($timezone) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the log_table name |
||
| 227 | * |
||
| 228 | * @param string |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function setLogTable($log_table) |
||
| 235 | } |
||
| 236 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.