PKCS7Encoder::decode()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Wechat\Utils\Code;
4
5
use Wechat\Utils\Code\ErrorCode;
6
7
/**
8
 * PKCS7Encoder class
9
 *
10
 * 提供基于PKCS7算法的加解密接口.
11
 */
12
class PKCS7Encoder
13
{
14
    public static $block_size = 32;
15
16
    /**
17
     * 对需要加密的明文进行填充补位
18
     *
19
     * @param $text 需要进行填充补位操作的明文
20
     *
21
     * @return 补齐明文字符串
22
     */
23
    public function encode($text)
24
    {
25
        $block_size  = PKCS7Encoder::$block_size;
0 ignored issues
show
Unused Code introduced by
$block_size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $text_length = strlen($text);
27
        //计算需要填充的位数
28
        $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
29
        if ($amount_to_pad == 0) {
30
            $amount_to_pad = PKCS7Encoder::block_size;
31
        }
32
        //获得补位所用的字符
33
        $pad_chr = chr($amount_to_pad);
34
        $tmp     = "";
35
        for ($index = 0; $index < $amount_to_pad; $index++) {
36
            $tmp .= $pad_chr;
37
        }
38
39
        return $text . $tmp;
40
    }
41
42
    /**
43
     * 对解密后的明文进行补位删除
44
     *
45
     * @param decrypted 解密后的明文
46
     *
47
     * @return 删除填充补位后的明文
48
     */
49 View Code Duplication
    public function decode($text)
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...
50
    {
51
        $pad = ord(substr($text, -1));
52
        if ($pad < 1 || $pad > 32) {
53
            $pad = 0;
54
        }
55
56
        return substr($text, 0, (strlen($text) - $pad));
57
    }
58
}
59