Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Random   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 35 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 2
c 2
b 1
f 0
lcom 0
cbo 0
dl 14
loc 40
rs 10
ccs 10
cts 10
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateOneTimeToken() 7 7 1
A generateKey() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
use RandomLib\Factory;
15
16
/**
17
 * XOOPS Random generator
18
 *
19
 * @category  Xmf\Random
20
 * @package   Xmf
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2015-2016 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @version   Release: 1.0
25
 * @link      http://xoops.org
26
 */
27
class Random
28
{
29
    /**
30
     * Create a one time token
31
     *
32
     * Generates a low strength random number of size $bytes and hash with the
33
     * algorithm specified in $hash.
34
     *
35
     * @param string  $hash  hash function to use
36
     * @param integer $bytes the number of random bit to generate
37
     *
38
     * @return string hashed token
39
     */
40 9 View Code Duplication
    public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
41
    {
42 9
        $factory = new Factory;
43 9
        $generator = $factory->getLowStrengthGenerator();
44 9
        $token = hash($hash, $generator->generate($bytes));
45 9
        return $token;
46
    }
47
48
    /**
49
     * Create a medium strength key
50
     *
51
     * Generates a medium strength random number of size $bytes and hash with the
52
     * algorithm specified in $hash.
53
     *
54
     * @param string  $hash  hash function to use
55
     * @param integer $bytes the number of random bytes to generate
56
     *
57
     * @return string hashed token
58
     */
59 1 View Code Duplication
    public static function generateKey($hash = 'sha512', $bytes = 128)
60
    {
61 1
        $factory = new Factory;
62 1
        $generator = $factory->getMediumStrengthGenerator();
63 1
        $token = hash($hash, $generator->generate($bytes));
64 1
        return $token;
65
    }
66
}
67