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 Auth |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array Internal lookup table |
||
| 34 | */ |
||
| 35 | private $lookup = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string Initialization key |
||
| 39 | */ |
||
| 40 | private $initKey = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var integer Seconds between key refreshes |
||
| 44 | */ |
||
| 45 | private $refreshSeconds = 30; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var integer The length of codes to generate |
||
| 49 | */ |
||
| 50 | private $codeLength = 6; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var integer Range plus/minus for "window of opportunity" on allowed codes |
||
| 54 | */ |
||
| 55 | private $range = 2; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Initialize the object and set up the lookup table |
||
| 59 | * Optionally the Initialization key |
||
| 60 | * |
||
| 61 | * @param string $initKey Initialization key |
||
|
|
|||
| 62 | */ |
||
| 63 | public function __construct($initKey = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Build the base32 lookup table |
||
| 74 | */ |
||
| 75 | public function buildLookup() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the current "range" value |
||
| 86 | * @return integer Range value |
||
| 87 | */ |
||
| 88 | public function getRange() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set the "range" value |
||
| 95 | * |
||
| 96 | * @param integer $range Range value |
||
| 97 | * @return \TOTP\Auth instance |
||
| 98 | */ |
||
| 99 | public function setRange($range) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set the initialization key for the object |
||
| 110 | * |
||
| 111 | * @param string $key Initialization key |
||
| 112 | * @throws \InvalidArgumentException If hash is not valid base32 |
||
| 113 | * @return \TOTP\Auth instance |
||
| 114 | */ |
||
| 115 | public function setInitKey($key) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the current Initialization key |
||
| 126 | * |
||
| 127 | * @return string Initialization key |
||
| 128 | */ |
||
| 129 | public function getInitKey() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set the contents of the internal lookup table |
||
| 136 | * |
||
| 137 | * @param array $lookup Lookup data set |
||
| 138 | * @throws \InvalidArgumentException If lookup given is not an array |
||
| 139 | * @return \TOTP\Auth instance |
||
| 140 | */ |
||
| 141 | public function setLookup($lookup) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the current lookup data set |
||
| 152 | * |
||
| 153 | * @return array Lookup data |
||
| 154 | */ |
||
| 155 | public function getLookup() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the number of seconds for code refresh currently set |
||
| 162 | * |
||
| 163 | * @return integer Refresh in seconds |
||
| 164 | */ |
||
| 165 | public function getRefresh() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the number of seconds to refresh codes |
||
| 172 | * |
||
| 173 | * @param integer $seconds Seconds to refresh |
||
| 174 | * @throws \InvalidArgumentException If seconds value is not numeric |
||
| 175 | * @return \TOTP\Auth instance |
||
| 176 | */ |
||
| 177 | public function setRefresh($seconds) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the current length for generated codes |
||
| 188 | * |
||
| 189 | * @return integer Code length |
||
| 190 | */ |
||
| 191 | public function getCodeLength() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the length of the generated codes |
||
| 198 | * |
||
| 199 | * @param integer $length Code length |
||
| 200 | * @return \TOTP\Auth instance |
||
| 201 | */ |
||
| 202 | public function setCodeLength($length) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Validate the given code |
||
| 210 | * |
||
| 211 | * @param string $code Code entered by user |
||
| 212 | * @param string $initKey Initialization key |
||
| 213 | * @param string $timestamp Timestamp for calculation |
||
| 214 | * @param integer $range Seconds before/after to validate hash against |
||
| 215 | * @throws \InvalidArgumentException If incorrect code length |
||
| 216 | * @return boolean Pass/fail of validation |
||
| 217 | */ |
||
| 218 | public function validateCode($code, $initKey = null, $timestamp = null, $range = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Generate a one-time code |
||
| 240 | * |
||
| 241 | * @param string $initKey Initialization key [optional] |
||
| 242 | * @param string $timestamp Timestamp for calculation [optional] |
||
| 243 | * @return string Generated code/hash |
||
| 244 | */ |
||
| 245 | public function generateOneTime($initKey = null, $timestamp = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Generate a code/hash |
||
| 262 | * Useful for making Initialization codes |
||
| 263 | * |
||
| 264 | * @param integer $length Length for the generated code |
||
| 265 | * @return string Generated code |
||
| 266 | */ |
||
| 267 | public function generateCode($length = 16) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Generate the timestamp for the calculation |
||
| 281 | * |
||
| 282 | * @return integer Timestamp |
||
| 283 | */ |
||
| 284 | public function generateTimestamp() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Truncate the given hash down to just what we need |
||
| 291 | * |
||
| 292 | * @param string $hash Hash to truncate |
||
| 293 | * @return string Truncated hash value |
||
| 294 | */ |
||
| 295 | public function truncateHash($hash) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Base32 decoding function |
||
| 309 | * |
||
| 310 | * @param string $hash The base32-encoded hash |
||
| 311 | * @throws \InvalidArgumentException When hash is not valid |
||
| 312 | * @return string Binary value of hash |
||
| 313 | */ |
||
| 314 | public function base32_decode($hash) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns a URL to QR code for embedding the QR code |
||
| 343 | * |
||
| 344 | * @param string $name The name |
||
| 345 | * @param string $code The generated code |
||
| 346 | * @return string The URL to the QR code |
||
| 347 | */ |
||
| 348 | public function getQrCodeUrl($name, $code) |
||
| 353 | } |
||
| 354 | |||
| 355 | ?> |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.