Passed
Push — 6.0 ( c251e6...05ff2c )
by liu
02:41
created

Cookie::prefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
/**
16
 * Cookie管理类
17
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
class Cookie
19
{
20
    /**
21
     * 配置参数
22
     * @var array
23
     */
24
    protected $config = [
25
        // cookie 保存时间
26
        'expire'   => 0,
27
        // cookie 保存路径
28
        'path'     => '/',
29
        // cookie 有效域名
30
        'domain'   => '',
31
        //  cookie 启用安全传输
32
        'secure'   => false,
33
        // httponly设置
34
        'httponly' => false,
35
    ];
36
37
    /**
38
     * Cookie写入数据
39
     * @var array
40
     */
41
    protected $cookie = [];
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
44
     * 构造方法
45
     * @access public
46
     */
47 8
    public function __construct(array $config = [])
48
    {
49 8
        $this->config = array_merge($this->config, array_change_key_case($config));
50 8
    }
51
52 8
    public static function __make(Config $config)
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Cookie::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Cookie::__make" must not be prefixed with an underscore
Loading history...
53
    {
54 8
        return new static($config->get('cookie'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('cookie') can also be of type null; however, parameter $config of think\Cookie::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        return new static(/** @scrutinizer ignore-type */ $config->get('cookie'));
Loading history...
55
    }
56
57
    /**
58
     * 获取cookie保存数据
59
     * @access public
60
     * @return array
61
     */
62
    public function getCookie(): array
63
    {
64
        return $this->cookie;
65
    }
66
67
    /**
68
     * Cookie 设置
69
     *
70
     * @access public
71
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
72
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
73
     * @param  mixed  $option 可选参数
74
     * @return void
75
     */
76
    public function set(string $name, string $value, $option = null): void
77
    {
78
        // 参数设置(会覆盖黙认设置)
79
        if (!is_null($option)) {
80
            if (is_numeric($option)) {
81
                $option = ['expire' => $option];
82
            }
83
84
            $config = array_merge($this->config, array_change_key_case($option));
85
        } else {
86
            $config = $this->config;
87
        }
88
89
        $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
90
91
        $this->setCookie($name, (string) $value, $expire, $config);
92
    }
93
94
    /**
95
     * Cookie 保存
96
     *
97
     * @access public
98
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
99
     * @param  mixed  $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
100
     * @param  int    $expire 有效期
101
     * @param  array  $option 可选参数
102
     * @return void
103
     */
104
    protected function setCookie(string $name, string $value, int $expire, array $option = []): void
105
    {
106
        $this->cookie[$name] = [$value, $expire, $option];
107
    }
108
109
    /**
110
     * 永久保存Cookie数据
111
     * @access public
112
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
113
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
114
     * @param  mixed  $option 可选参数 可能会是 null|integer|string
115
     * @return void
116
     */
117
    public function forever(string $name, string $value = '', $option = null): void
118
    {
119
        if (is_null($option) || is_numeric($option)) {
120
            $option = [];
121
        }
122
123
        $option['expire'] = 315360000;
124
125
        $this->set($name, $value, $option);
126
    }
127
128
    /**
129
     * Cookie删除
130
     * @access public
131
     * @param  string      $name cookie名称
132
     * @return void
133
     */
134
    public function delete(string $name): void
135
    {
136
        $this->setCookie($name, '', time() - 3600, $this->config);
137
    }
138
139
    /**
140
     * 保存Cookie
141
     * @access public
142
     * @return void
143
     */
144
    public function save(): void
145
    {
146
        foreach ($this->cookie as $name => $val) {
147
            list($value, $expire, $option) = $val;
148
149
            $this->saveCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'] ? true : false, $option['httponly'] ? true : false);
150
        }
151
    }
152
153
    /**
154
     * 保存Cookie
155
     * @access public
156
     * @param  string $name cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
157
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
158
     * @param  int    $expire cookie过期时间
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
159
     * @param  string $path 有效的服务器路径
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
160
     * @param  string $domain 有效域名/子域名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
161
     * @param  bool   $secure 是否仅仅通过HTTPS
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
162
     * @param  bool   $httponly 仅可通过HTTP访问
163
     * @return void
164
     */
165
    protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly): void
166
    {
167
        setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
168
    }
169
170
}
171