• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 7146731309

01 Dec 2023 03:48PM UTC coverage: 39.661%. Remained the same
7146731309

push

github

twinh
style(plugin): phpstan 忽略历史方法

936 of 2360 relevant lines covered (39.66%)

18.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.1
/src/ConstTrait.php
1
<?php
2

3
namespace Miaoxing\Plugin;
4

5
use Miaoxing\Plugin\Model\ReqQueryTrait;
6
use Wei\Req;
7

8
/**
9
 * @property Req $request
10
 * @todo 支持 name 之外 ?
11
 */
12
trait ConstTrait
13
{
14
    /**
15
     * @var array
16
     */
17
    protected static $consts = [];
18

19
    /**
20
     * @var array
21
     */
22
    protected static $constNameToIds = [];
23

24
    /**
25
     * @var array
26
     */
27
    protected $constExcludes = [
28
        'STATE_DIRTY',
29
        'STATE_CLEAN',
30
    ];
31

32
    /**
33
     * Get constants table by prefix
34
     *
35
     * @param string $prefix
36
     * @return array
37
     */
38
    public function getConsts($prefix)
39
    {
40
        if (isset(self::$consts[$prefix])) {
12✔
41
            return self::$consts[$prefix];
×
42
        }
43

44
        // 1. Get all class constants
45
        $class = new \ReflectionClass($this);
12✔
46
        $consts = $class->getConstants();
12✔
47

48
        // 2. Use exiting constant configs
49
        $property = lcfirst(str_replace('_', '', ucwords($prefix, '_'))) . 'Names';
12✔
50
        if (isset($this->{$property})) {
12✔
51
            $names = $this->{$property};
12✔
52
        } else {
53
            $names = [];
×
54
        }
55

56
        // 3. Generate id and name
57
        $prefix .= '_';
12✔
58
        $data = [];
12✔
59
        $length = strlen($prefix);
12✔
60
        foreach ($consts as $name => $id) {
12✔
61
            if (0 !== stripos($name, $prefix)) {
12✔
62
                continue;
×
63
            }
64
            if (in_array($name, $this->constExcludes, true)) {
12✔
65
                continue;
×
66
            }
67
            $data[$id]['id'] = $id;
12✔
68
            $data[$id]['key'] = strtolower(strtr(substr($name, $length), ['_' => '-']));
12✔
69
            if (isset($names[$id])) {
12✔
70
                $data[$id]['name'] = $names[$id];
12✔
71
            }
72
        }
73

74
        self::$consts[$prefix] = $data;
12✔
75

76
        return $data;
12✔
77
    }
78

79
    public function getConstName($prefix, $id)
80
    {
81
        return $this->getConsts($prefix)[$id]['name'];
3✔
82
    }
83

84
    public function getConstsWithAll($prefix)
85
    {
86
        $consts = $this->getConsts($prefix);
×
87
        array_unshift($consts, [
×
88
            'id' => '',
×
89
            'key' => 'all',
90
            'name' => '全部',
91
        ]);
92
        return $consts;
×
93
    }
94

95
    /**
96
     * 将请求的key(字母)转换为id(数字)
97
     *
98
     * 用法
99
     * $curStatus = wei()->xxx->getConstId('status', $req['status']);
100
     *
101
     * @param string $prefix
102
     * @param string $reqKey
103
     * @return string
104
     */
105
    public function getConstId($prefix, $reqKey)
106
    {
107
        $keyToIds = $this->getConstKeyToIds($prefix);
3✔
108
        if (isset($keyToIds[$reqKey])) {
3✔
109
            return $keyToIds[$reqKey];
3✔
110
        }
111

112
        return '';
×
113
    }
114

115
    /**
116
     * 获取key和id关联数组
117
     *
118
     * @param string $prefix
119
     * @return array
120
     */
121
    public function getConstKeyToIds($prefix)
122
    {
123
        return array_column($this->getConsts($prefix), 'id', 'key');
3✔
124
    }
125

126
    /**
127
     * 将请求的key(字母)转换为key并用于查询
128
     *
129
     * @param string $prefix
130
     * @param string $reqKey
131
     * @return $this
132
     * @throws \Exception
133
     * @todo 改为判断是model才允许操作,或改为独立trait
134
     */
135
    public function whereConstKey($prefix, $reqKey = null)
136
    {
137
        // @phpstan-ignore-next-line
138
        if (!$this instanceof ReqQueryTrait) {
×
139
            throw new \Exception('Not support');
×
140
        }
141

142
        if (1 === func_num_args()) {
×
143
            $reqKey = $this->req->get($prefix);
×
144
        }
145

146
        $id = $this->getConstId($prefix, $reqKey);
×
147
        if ('' !== $id) {
×
148
            list($column) = $this->parseReqColumn($prefix);
×
149
            $this->where($column, $id);
×
150
        }
151

152
        return $this;
×
153
    }
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc