Completed
Push — master ( d9b5c0...4d4232 )
by Anthony
12:25 queued 06:54
created

Sodium::generate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
nc 2
cc 3
eloc 4
nop 1
1
<?php
2
/**
3
 * The libsodium Random Number Source
4
 *
5
 * This uses the libsodium secure generator to generate high strength numbers
6
 *
7
 * PHP version 5.3
8
 *
9
 * @category   PHPCryptLib
10
 * @package    Random
11
 * @subpackage Source
12
 * @author     Anthony Ferrara <[email protected]>
13
 * @author     Ben Ramsey <[email protected]>
14
 * @copyright  2011 The Authors
15
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
16
 * @version    Build @@version@@
17
 * @link       https://paragonie.com/book/pecl-libsodium
18
 * @link       http://pecl.php.net/package/libsodium
19
 */
20
21
namespace RandomLib\Source;
22
23
use SecurityLib\Strength;
24
25
/**
26
 * The libsodium Random Number Source
27
 *
28
 * This uses the libsodium secure generator to generate high strength numbers
29
 *
30
 * @category   PHPCryptLib
31
 * @package    Random
32
 * @subpackage Source
33
 * @author     Anthony Ferrara <[email protected]>
34
 * @author     Ben Ramsey <[email protected]>
35
 */
36
class Sodium implements \RandomLib\Source {
37
38
    /**
39
     * A property that may be forcibly set to `false` in the constructor, for
40
     * the purpose of testing this source
41
     *
42
     * @var bool
43
     */
44
    private $hasLibsodium = false;
45
46
    /**
47
     * Constructs a libsodium Random Number Source
48
     *
49
     * @param bool $useLibsodium May be set to `false` to disable libsodium for
50
     *                           testing purposes
51
     */
52
    public function __construct($useLibsodium = true) {
53
        if ($useLibsodium && extension_loaded('libsodium')) {
54
            $this->hasLibsodium = true;
55
        }
56
    }
57
58
    /**
59
     * Return an instance of Strength indicating the strength of the source
60
     *
61
     * @return Strength An instance of one of the strength classes
62
     */
63
    public static function getStrength() {
64
        return new Strength(Strength::HIGH);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SecurityLib\...ityLib\Strength::HIGH); (SecurityLib\Strength) is incompatible with the return type declared by the interface RandomLib\Source::getStrength of type RandomLib\Strength.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
    }
66
67
    /**
68
     * Generate a random string of the specified size
69
     *
70
     * @param int $size The size of the requested random string
71
     *
72
     * @return string A string of the requested size
73
     */
74
    public function generate($size) {
75
        if (!$this->hasLibsodium || $size < 1) {
76
            return str_repeat(chr(0), $size);
77
        }
78
79
        return \Sodium\randombytes_buf($size);
80
    }
81
82
}
83