| Conditions | 14 |
| Paths | 46 |
| Total Lines | 42 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | public function generateEntropy() {
|
||
| 18 | $isWin = preg_match('/WIN/', PHP_OS);
|
||
| 19 | |||
| 20 | // TODO Fails with "Could not gather sufficient random data" on IIS, temporarily disabled on windows |
||
| 21 | if(!$isWin) {
|
||
| 22 | if(function_exists('mcrypt_create_iv')) {
|
||
| 23 | $e = mcrypt_create_iv(64, MCRYPT_DEV_URANDOM); |
||
| 24 | if($e !== false) return $e; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | // Fall back to SSL methods - may slow down execution by a few ms |
||
| 29 | if (function_exists('openssl_random_pseudo_bytes')) {
|
||
| 30 | $e = openssl_random_pseudo_bytes(64, $strong); |
||
| 31 | // Only return if strong algorithm was used |
||
| 32 | if($strong) return $e; |
||
| 33 | } |
||
| 34 | |||
| 35 | // Read from the unix random number generator |
||
| 36 | if(!$isWin && !ini_get('open_basedir') && is_readable('/dev/urandom') && ($h = fopen('/dev/urandom', 'rb'))) {
|
||
| 37 | $e = fread($h, 64); |
||
| 38 | fclose($h); |
||
| 39 | return $e; |
||
| 40 | } |
||
| 41 | |||
| 42 | // Warning: Both methods below are considered weak |
||
| 43 | |||
| 44 | // try to read from the windows RNG |
||
| 45 | if($isWin && class_exists('COM')) {
|
||
| 46 | try {
|
||
| 47 | $comObj = new COM('CAPICOM.Utilities.1');
|
||
| 48 | |||
| 49 | if(is_callable(array($comObj,'GetRandom'))) {
|
||
| 50 | return base64_decode($comObj->GetRandom(64, 0)); |
||
| 51 | } |
||
| 52 | } catch (Exception $ex) {
|
||
|
|
|||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | // Fallback to good old mt_rand() |
||
| 57 | return uniqid(mt_rand(), true); |
||
| 58 | } |
||
| 59 | |||
| 74 | } |