HashDataSignature   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 10
loc 50
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A generate() 0 4 1
A validate() 10 10 2

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
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
9
namespace yiiviet\payment;
10
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Lớp HashDataSignature hổ trợ tạo chữ ký dữ liệu thông qua các kiểu mã hóa hash 1 chiều như md5, sha1, sha256...
15
 *
16
 * @author Vuong Minh <[email protected]>
17
 * @since 1.0.2
18
 */
19
class HashDataSignature extends DataSignature
20
{
21
22
    /**
23
     * @var string tên loại mã hóa. Ví dụ: md5, sha1, sha256...
24
     */
25
    public $hashAlgo;
26
27
    /**
28
     * @var bool phân biệt ký tự hoa thường khi xác minh chữ ký.
29
     * @since 1.0.3
30
     */
31
    public $caseSensitive = true;
32
33
    /**
34
     * @inheritdoc
35
     * @throws InvalidConfigException
36
     */
37 10
    public function init()
38
    {
39 10
        if ($this->hashAlgo === null) {
40
            throw new InvalidConfigException('Property `hashAlgo` must be set!');
41
        }
42
43 10
        parent::init();
44 10
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 10
    public function generate(): string
50
    {
51 10
        return hash($this->hashAlgo, $this->getData());
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 5 View Code Duplication
    public function validate(string $expect): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 5
        $actual = $this->generate();
60
61 5
        if ($this->caseSensitive) {
62 5
            return strcmp($expect, $actual) === 0;
63
        } else {
64
            return strcasecmp($expect, $actual) === 0;
65
        }
66
    }
67
68
}
69