Complex classes like GoogleAuthenticator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GoogleAuthenticator, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 7 | class GoogleAuthenticator | ||
| 8 | { | ||
| 9 | protected $_codeLength = 6; | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Create new secret. | ||
| 13 | * 16 characters, randomly chosen from the allowed base32 characters. | ||
| 14 | * | ||
| 15 | * @param int $secretLength | ||
| 16 | * | ||
| 17 | * @return string | ||
| 18 | */ | ||
| 19 | public function createSecret($secretLength = 16) | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Calculate the code, with given secret and point in time. | ||
| 52 | * | ||
| 53 | * @param string $secret | ||
| 54 | * @param int|null $timeSlice | ||
| 55 | * | ||
| 56 | * @return string | ||
| 57 | */ | ||
| 58 | public function getCode($secret, $timeSlice = null) | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Get QR-Code URL for image, from google charts. | ||
| 88 | * | ||
| 89 | * @param string $name | ||
| 90 | * @param string $secret | ||
| 91 | * @param string $title | ||
| 92 | * @param array $params | ||
| 93 | * | ||
| 94 | * @return string | ||
| 95 | */ | ||
| 96 | public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = array()) | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now. | ||
| 112 | * | ||
| 113 | * @param string $secret | ||
| 114 | * @param string $code | ||
| 115 | * @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after) | ||
| 116 | * @param int|null $currentTimeSlice time slice if we want use other that time() | ||
| 117 | * | ||
| 118 | * @return bool | ||
| 119 | */ | ||
| 120 | public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null) | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Set the code length, should be >=6. | ||
| 142 | * | ||
| 143 | * @param int $length | ||
| 144 | * | ||
| 145 | * @return PHPGangsta_GoogleAuthenticator | ||
| 146 | */ | ||
| 147 | public function setCodeLength($length) | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Helper class to decode base32. | ||
| 156 | * | ||
| 157 | * @param $secret | ||
| 158 | * | ||
| 159 | * @return bool|string | ||
| 160 | */ | ||
| 161 | protected function _base32Decode($secret) | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Get array with all 32 characters for decoding from/encoding to base32. | ||
| 203 | * | ||
| 204 | * @return array | ||
| 205 | */ | ||
| 206 | protected function _getBase32LookupTable() | ||
| 216 | |||
| 217 | /** | ||
| 218 | * A timing safe equals comparison | ||
| 219 | * more info here: http://blog.ircmaxell.com/2014/11/its-all-about-time.html. | ||
| 220 | * | ||
| 221 | * @param string $safeString The internal (safe) value to be checked | ||
| 222 | * @param string $userString The user submitted (unsafe) value | ||
| 223 | * | ||
| 224 | * @return bool True if the two strings are identical | ||
| 225 | */ | ||
| 226 | private function timingSafeEquals($safeString, $userString) | ||
| 247 | } | ||
| 248 | 
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.