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) |
|
| 54 | { |
||
| 55 | 8 | parent::__construct($connection); |
|
| 56 | |||
| 57 | 8 | $this->connection = $connection; |
|
| 58 | 8 | $this->index = 'quota.connections.' . $connection; |
|
| 59 | |||
| 60 | 8 | $this->timezone = config($this->index . '.timezone'); |
|
| 61 | 8 | if (is_null($this->timezone)) { |
|
| 62 | $this->timezone = config('quota.default_timezone'); |
||
| 63 | } |
||
| 64 | |||
| 65 | 8 | $this->log_table = config($this->index . '.log_table'); |
|
| 66 | |||
| 67 | //Bootstrap log if record does not exist. |
||
| 68 | 8 | $dtz = new \DateTimeZone($this->timezone); |
|
| 69 | 8 | $now = new \DateTime(date("Y-m-d"), $dtz); |
|
| 70 | 8 | $date = $now->format("Y-m-d"); |
|
| 71 | 8 | $log_records = DB::select( |
|
| 72 | 'SELECT * FROM' . |
||
| 73 | 8 | ' ' . $this->log_table . |
|
| 74 | 8 | ' WHERE date = ' . '\'' . $date . '\'' . |
|
| 75 | 8 | ' AND connection = ' . '\'' . $connection . '\'' |
|
| 76 | 8 | ); |
|
| 77 | |||
| 78 | 8 | if (empty($log_records)) { |
|
| 79 | 8 | \Artisan::call('quota:reset', [ |
|
| 80 | 8 | 'date' => $date, |
|
| 81 | 'connection' => $connection |
||
| 82 | 8 | ]); |
|
| 83 | 8 | } |
|
| 84 | 8 | } |
|
| 85 | |||
| 86 | 1 | public function enforce() |
|
| 87 | { |
||
| 88 | 1 | return $this->consume(); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Helper. |
||
| 93 | * |
||
| 94 | * TODO: make static? |
||
| 95 | * |
||
| 96 | * @param PeriodicQuota $quota |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 8 | public function dateInTimezone() |
|
| 100 | { |
||
| 101 | 8 | $dtz = new \DateTimeZone($this->getTimezone()); |
|
| 102 | 8 | $now = new \DateTime(date("Y-m-d"), $dtz); |
|
| 103 | 8 | return($now->format("Y-m-d")); |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Hiiiiit iiiiiit ! :) |
||
| 108 | * Record a hit in the log table. |
||
| 109 | * |
||
| 110 | * @param int $hits so far |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | 3 | View Code Duplication | public function hit($hits) |
| 114 | { |
||
| 115 | 3 | $date = $this->dateInTimezone(); |
|
| 116 | 3 | $hits = $hits + 1; |
|
| 117 | |||
| 118 | 3 | DB::statement( |
|
| 119 | 3 | 'UPDATE ' . $this->log_table . |
|
| 120 | 3 | ' SET hits = ' . $hits . |
|
| 121 | 3 | ', updated_at = ' . '\'' . Carbon::now()->toDateTimeString() . '\'' . |
|
| 122 | 3 | ' WHERE date = ' . '\'' . $date . '\'' . |
|
| 123 | 3 | ' AND connection = ' . '\'' . $this->connection . '\'' |
|
| 124 | 3 | ); |
|
| 125 | 3 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Record a miss in the log table. |
||
| 129 | * |
||
| 130 | * @param int $misses so far |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | 1 | View Code Duplication | public function miss($misses) |
| 134 | { |
||
| 135 | 1 | $date = $this->dateInTimezone(); |
|
| 136 | 1 | $misses = $misses + 1; |
|
| 137 | |||
| 138 | 1 | DB::statement( |
|
| 139 | 1 | 'UPDATE ' . $this->log_table . |
|
| 140 | 1 | ' SET misses = ' . $misses . |
|
| 141 | 1 | ', updated_at = ' . '\'' . Carbon::now()->toDateTimeString() . '\'' . |
|
| 142 | 1 | ' WHERE date = ' . '\'' . $date . '\'' . |
|
| 143 | 1 | ' AND connection = ' . '\'' . $this->connection . '\'' |
|
| 144 | 1 | ); |
|
| 145 | 1 | } |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Attempt to... HIT dat a$$! :) |
||
| 149 | * |
||
| 150 | * @param integer $tokens future use. |
||
| 151 | * @return boolean true on success |
||
| 152 | * |
||
| 153 | * @throws ErrorException |
||
| 154 | */ |
||
| 155 | 2 | public function consume($tokens = 1) |
|
| 156 | { |
||
| 157 | 2 | $date = $this->dateInTimezone(); |
|
| 158 | |||
| 159 | 2 | $stats = $this->getStats($date); |
|
| 160 | 2 | $hits = (integer) $stats->hits; |
|
| 161 | |||
| 162 | 2 | if ($this->limit < ($hits + 1)) { |
|
| 163 | $this->miss($stats->misses); |
||
| 164 | throw new \ErrorException( |
||
| 165 | __CLASS__ . '::' . __FUNCTION__ . |
||
| 166 | ' Overquota. Exceeded daily limit: ' . $this->limit |
||
| 167 | ); |
||
| 168 | } else { |
||
| 169 | 2 | $this->hit($stats->hits); |
|
| 170 | 2 | $result = true; |
|
| 171 | } |
||
| 172 | 2 | return isset($result); |
|
| 173 | } |
||
| 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.