Completed
Push — master ( 4d4232...ffcfa4 )
by Anthony
14:02 queued 11:53
created

lib/RandomLib/Source/CAPICOM.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * The Capicom Random Number Source
4
 *
5
 * This uses the Windows CapiCom Com object to generate random numbers
6
 *
7
 * PHP version 5.3
8
 *
9
 * @category   PHPCryptLib
10
 * @package    Random
11
 * @subpackage Source
12
 * @author     Anthony Ferrara <[email protected]>
13
 * @copyright  2011 The Authors
14
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
15
 * @version    Build @@version@@
16
 */
17
18
namespace RandomLib\Source;
19
20
use SecurityLib\Strength;
21
22
/**
23
 * The Capicom Random Number Source
24
 *
25
 * This uses the Windows CapiCom Com object to generate random numbers
26
 *
27
 * @category   PHPCryptLib
28
 * @package    Random
29
 * @subpackage Source
30
 * @author     Anthony Ferrara <[email protected]>
31
 * @codeCoverageIgnore
32
 */
33
class CAPICOM implements \RandomLib\Source {
34
35
    /**
36
     * Return an instance of Strength indicating the strength of the source
37
     *
38
     * @return Strength An instance of one of the strength classes
39
     */
40
    public static function getStrength() {
41
        return new Strength(Strength::MEDIUM);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SecurityLib\...yLib\Strength::MEDIUM); (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...
42
    }
43
44
    /**
45
     * Generate a random string of the specified size
46
     *
47
     * @param int $size The size of the requested random string
48
     *
49
     * @return string A string of the requested size
50
     */
51
    public function generate($size) {
52
        if (!class_exists('\\COM', false)) {
53
            return str_repeat(chr(0), $size);
54
        }
55
        try {
56
            $util = new \COM('CAPICOM.Utilities.1');
57
            $data = base64_decode($util->GetRandom($size, 0));
58
            return str_pad($data, $size, chr(0));
59
        } catch (\Exception $e) {
60
            unset($e);
61
            return str_repeat(chr(0), $size);
62
        }
63
    }
64
65
}
66