| 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.