Completed
Push — 4.x ( f42e95 )
by Akihito
14s queued 11s
created

Randval   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 5
c 6
b 0
f 1
lcom 1
cbo 1
dl 0
loc 56
ccs 12
cts 13
cp 0.9231
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B generate() 0 22 4
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Session;
10
11
use Aura\Session\Exception;
12
13
/**
14
 *
15
 * Generates cryptographically-secure random values.
16
 *
17
 * @package Aura.Session
18
 *
19
 */
20
class Randval implements RandvalInterface
21
{
22
    /**
23
     *
24
     * An object to intercept PHP function calls; this makes testing easier.
25
     *
26
     * @var Phpfunc
27
     *
28
     */
29
    protected $phpfunc;
30
31
    /**
32
     *
33
     * Constructor.
34
     *
35
     * @param Phpfunc $phpfunc An object to intercept PHP function calls;
36
     * this makes testing easier.
37
     *
38
     */
39 35
    public function __construct(Phpfunc $phpfunc)
40
    {
41 35
        $this->phpfunc = $phpfunc;
42 35
    }
43
44
    /**
45
     *
46
     * Returns a cryptographically secure random value.
47
     *
48
     * @return string
49
     *
50
     * @throws Exception if neither openssl nor mcrypt is available.
51
     *
52
     */
53 5
    public function generate()
54
    {
55 5
        $bytes = 32;
56
57 5
        if ($this->phpfunc->extension_loaded('openssl')) {
0 ignored issues
show
Documentation Bug introduced by
The method extension_loaded does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58 5
            return $this->phpfunc->openssl_random_pseudo_bytes($bytes);
0 ignored issues
show
Documentation Bug introduced by
The method openssl_random_pseudo_bytes does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
59
        }
60
61 1
        if ($this->phpfunc->extension_loaded('mcrypt')) {
0 ignored issues
show
Documentation Bug introduced by
The method extension_loaded does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
62 1
            return $this->phpfunc->mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
0 ignored issues
show
Documentation Bug introduced by
The method mcrypt_create_iv does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
        }
64
65 1
        if ($this->phpfunc->function_exists('random_bytes')) {
0 ignored issues
show
Documentation Bug introduced by
The method function_exists does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
            return $this->phpfunc->random_bytes($bytes);
0 ignored issues
show
Documentation Bug introduced by
The method random_bytes does not exist on object<Aura\Session\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
        }
68
69
        $message = "Cannot generate cryptographically secure random values. "
70
                 . "Please install extension 'openssl' or 'mcrypt', or use "
71 1
                 . "another cryptographically secure implementation.";
72
73 1
        throw new Exception($message);
74
    }
75
}
76