Completed
Push — master ( bda1e7...f49c09 )
by Stefano
02:18
created

Hash   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 24.18 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 0
cbo 1
dl 22
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 3 1
A verify() 0 3 1
A methods() 0 3 1
A can() 0 3 1
A __callStatic() 0 3 1
C uuid() 22 30 8

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
/**
4
 * Hash
5
 *
6
 * Hashing shorthands.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
14
class Hash {
15
    use Module;
16
17
    /**
18
     * Create ah hash for payload
19
     * @param  mixed $payload The payload string/object/array
20
     * @param  integer $method  The hashing method, default is "md5"
21
     * @return string          The hash string
22
     */
23
    public static function make($payload,$method='md5'){
24
        return hash($method,serialize($payload));
25
    }
26
27
    /**
28
     * Verify if given payload matches hash
29
     * @param  mixed $payload  The payload string/object/array
30
     * @param  string $hash    The hash string
31
     * @param  integer $method The hashing method
32
     * @return bool            Returns `true` if payload matches hash
33
     */
34
    public static function verify($payload,$hash,$method='md5'){
35
        return static::make($payload,$method) == $hash;
36
    }
37
38
    /**
39
     * List registered hashing algorithms
40
     *
41
     * @method methods
42
     *
43
     * @return array   Array containing the list of supported hashing algorithms.
44
     */
45
    public static function methods(){
46
        return hash_algos();
47
    }
48
49
50
    /**
51
     * Check if an alghoritm is registered in current PHP
52
     *
53
     * @method can
54
     *
55
     * @param  string $algo The hashing algorithm name
56
     *
57
     * @return bool
58
     */
59
    public static function can($algo){
60
        return in_array($algo,hash_algos());
61
    }
62
63
    /**
64
     * Static magic for creating hashes with a specified algorithm.
65
     *
66
     * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms
67
     */
68
    public static function __callStatic($method,$params){
69
        return self::make(current($params),$method);
70
    }
71
72
    public static function uuid($type=4, $namespace='', $name=''){
73
      switch($type){
74 View Code Duplication
        case 3: if(preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
75
        '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) return false;
76
        $nhex = str_replace(array('-','{','}'), '', $namespace);
77
        $nstr = ''; for($i = 0; $i < strlen($nhex); $i+=2)
78
        $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
79
        $hash = md5($nstr . $name);
80
        return sprintf('%08s-%04s-%04x-%04x-%12s',
81
        substr($hash, 0, 8), substr($hash, 8, 4),
82
        (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
83
        (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
84
        substr($hash, 20, 12));
85 View Code Duplication
        case 5: if(preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
86
        '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) return false;
87
        $nhex = str_replace(array('-','{','}'), '', $namespace);
88
        $nstr = ''; for($i = 0; $i < strlen($nhex); $i+=2)
89
        $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
90
        $hash = sha1($nstr . $name);
91
        return sprintf('%08s-%04s-%04x-%04x-%12s',
92
        substr($hash, 0, 8), substr($hash, 8, 4),
93
        (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
94
        (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
95
        substr($hash, 20, 12));
96
        default: case 4: return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
1 ignored issue
show
Unused Code introduced by
case 4: return sprin...5), mt_rand(0, 65535)); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
97
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
98
        mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
99
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
100
      }
101
    }
102
103
104
}
105