Completed
Pull Request — master (#74)
by
unknown
04:47
created

Paragonie_RandomAdapter::do_random_bytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * Random_* Compatibility Library 
4
 * for using the new PHP 7 random_* API in PHP 5 projects
5
 * 
6
 * The MIT License (MIT)
7
 * 
8
 * Copyright (c) 2015 Paragon Initiative Enterprises
9
 * 
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 * 
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 * 
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
 * SOFTWARE.
27
 */
28
29
class Paragonie_RandomAdapter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     * Windows with PHP < 5.3.0 will not have the function
33
     * openssl_random_pseudo_bytes() available, so let's use
34
     * CAPICOM to work around this deficiency.
35
     * 
36
     * @param int $bytes
37
     * 
38
     * @throws Exception
39
     * 
40
     * @return string
41
     */
42
    protected static function do_random_bytes($bytes)
0 ignored issues
show
Unused Code introduced by
The parameter $bytes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        $buf = '';
45
        $util = new COM('CAPICOM.Utilities.1');
0 ignored issues
show
Unused Code introduced by
The call to com::__construct() has too many arguments starting with 'CAPICOM.Utilities.1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
        $execCount = 0;
47
        /**
48
         * Let's not let it loop forever. If we run N times and fail to
49
         * get N bytes of random data, then CAPICOM has failed us.
50
         */
51
        do {
52
            $buf .= base64_decode($util->GetRandom($bytes, 0));
53
            if (isset($buf[$bytes - 1])) {
54
                /**
55
                 * Return our random entropy buffer here:
56
                 */
57
                return $buf;
58
            }
59
            ++$execCount; 
60
        } while ($execCount < $bytes);
61
    }
62
}
63