Completed
Push — master ( b43f52...fd9653 )
by Jianhua
04:25
created

checkSensitiveWords()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 10
eloc 20
c 2
b 0
f 1
nc 6
nop 2
dl 0
loc 31
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Illuminate\Database\Eloquent\Model;
4
use App\Foundation\Tire;
5
use Illuminate\Support\Facades\Cache;
6
use App\Model\Admin\SensitiveWord;
7
8
function parseEntityFieldParams($params)
9
{
10
    $items = explode("\n", $params);
11
    return array_map(function ($item) {
12
        return explode("=", $item);
13
    }, $items);
14
}
15
16
function isChecked($value, $options)
17
{
18
    return in_array($value, explode(',', $options), true);
19
}
20
21
function xssFilter(Model $data)
22
{
23
    $attributes = $data->getAttributes();
24
    foreach ($attributes as &$v) {
25
        if (is_string($v)) {
26
            $v = htmlspecialchars($v, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8');
27
        }
28
    }
29
    $data->setRawAttributes($attributes);
30
}
31
32
function initTire()
33
{
34
    return Cache::rememberForever('sensitive_words_tire', function () {
35
        $tires = [];
36
37
        foreach (['noun', 'verb', 'exclusive'] as $v) {
38
            $words = SensitiveWord::query()->select($v)->where($v, '<>', '')->get();
39
40
            $tire = new Tire();
41
            foreach ($words as $k) {
42
                $tire->add($k->$v);
43
            }
44
            $tires[$v] = $tire;
45
        }
46
47
        return $tires;
48
    });
49
}
50
51
function mapTypeToVerbOfSensitiveWords()
52
{
53
    return Cache::rememberForever('sensitive_verb_words', function () {
54
        $words = SensitiveWord::query()->select('verb', 'type')->where('verb', '<>', '')->get();
55
56
        $data = [];
57
        foreach ($words as $word) {
58
            $data[$word->type <> '' ? $word->type : 'others'][] = $word->verb;
59
        }
60
61
        return $data;
62
    });
63
}
64
65
/**
66
 * 敏感词检查
67
 *
68
 * @param string $text 待检查文本
69
 * @param null $mode 检查模式。默认名词、动词、专用词都检查,显示可指定为 noun verb exclusive
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mode is correct as it would always require null to be passed?
Loading history...
70
 * @return array
71
 */
72
function checkSensitiveWords(string $text, $mode = null)
73
{
74
    if (!is_null($mode) && !in_array($mode, ['noun', 'verb', 'exclusive'])) {
0 ignored issues
show
introduced by
The condition is_null($mode) is always true.
Loading history...
75
        throw new \InvalidArgumentException('mode参数无效,只能为null值、noun、exclusive');
76
    }
77
78
    $tires = initTire();
79
    if (!is_null($mode)) {
80
        return $tires[$mode]->seek($text);
81
    }
82
83
    $result = [];
84
    $return = [];
85
    foreach ($tires as $k => $tire) {
86
        $result[$k] = $tire->seek($text);
87
    }
88
    if (!empty($result['noun']) && !empty($result['verb'])) {
89
        $data = mapTypeToVerbOfSensitiveWords();
90
        foreach ($result['noun'] as $noun) {
91
            $type = Cache::rememberForever('sensitive_words_noun_type', function () use ($noun) {
92
                return SensitiveWord::query()->where('noun', $noun)->value('type');
93
            });
94
            $type = $type ? $type : 'others';
95
            $verbs = array_intersect($data[$type], $result['verb']);
96
            if (!empty($verbs)) {
97
                array_push($verbs, $noun);
98
                $return[] = implode(' ', $verbs);
99
            }
100
        }
101
    }
102
    return array_merge($return, $result['exclusive']);
103
}
104