1 | <?php |
||||
2 | |||||
3 | namespace RafaelDms\Cookie; |
||||
4 | |||||
5 | /** |
||||
6 | * Class StaticCokie |
||||
7 | * @package RafaelDms\Cookie |
||||
8 | */ |
||||
9 | class StaticCookie |
||||
10 | { |
||||
11 | /** |
||||
12 | * @param string $name |
||||
13 | * @param mixed $value |
||||
14 | * @param int $minutes |
||||
15 | * @param string|null $path |
||||
16 | * @param string|null $domain |
||||
17 | * @param bool $secureOnly |
||||
18 | * @param bool $encrypt |
||||
19 | * @return bool |
||||
20 | */ |
||||
21 | public static function set( |
||||
22 | string $name, |
||||
23 | mixed $value, |
||||
24 | int $minutes, |
||||
25 | bool $secureOnly = true, |
||||
26 | bool $encrypt = true, |
||||
27 | ?string $path = null, |
||||
28 | ?string $domain = "" |
||||
29 | ): bool { |
||||
30 | //check if the cookie value is an array to save in json |
||||
31 | if (is_array($value)) { |
||||
32 | $cookie = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
||||
33 | $value = $encrypt ? self::encrypt($cookie) : $cookie; |
||||
34 | } else { |
||||
35 | $value = $encrypt ? self::encrypt($value) : $value; |
||||
36 | } |
||||
37 | return self::setCookie($name, $value, self::expire($minutes), $path, $path, $domain, $secureOnly); |
||||
0 ignored issues
–
show
$domain of type null|string is incompatible with the type boolean expected by parameter $secure of RafaelDms\Cookie\StaticCookie::setCookie() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * @param string $name |
||||
42 | * @param bool $decrypt |
||||
43 | * @return mixed|string|null |
||||
44 | */ |
||||
45 | public static function get(string $name, bool $decrypt = true): mixed |
||||
46 | { |
||||
47 | if (self::has($name)) { |
||||
48 | $cookie = ($decrypt ? self::decrypt(self::getCookie($name)) : self::getCookie($name)); |
||||
49 | if ($cookie) { |
||||
50 | if ($decode = json_decode($cookie, true)) { |
||||
51 | return $decode; |
||||
52 | } |
||||
53 | return $cookie; |
||||
54 | } |
||||
55 | return null; |
||||
56 | } |
||||
57 | return null; |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @param string $name |
||||
62 | * @param string|null $value |
||||
63 | * @param string|null $path |
||||
64 | * @return bool |
||||
65 | */ |
||||
66 | public static function destroy(string $name, ?string $value = '', ?string $path = null): bool |
||||
67 | { |
||||
68 | return self::setCookie($name, $value, -1, $path); |
||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * @param string $name |
||||
73 | * @param string|null $value |
||||
74 | * @param string|null $path |
||||
75 | * @return bool |
||||
76 | */ |
||||
77 | public static function destroyAndUnset(string $name, ?string $value = '', ?string $path = null): bool |
||||
78 | { |
||||
79 | self::unset($name); |
||||
80 | return self::setCookie($name, $value, -1, $path); |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * @param string $name |
||||
85 | * @return void |
||||
86 | */ |
||||
87 | public static function unset(string $name): void |
||||
88 | { |
||||
89 | unset($_COOKIE[$name]); |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @param string $name |
||||
94 | * @param string|null $value |
||||
95 | * @param bool $encrypt |
||||
96 | * @return bool |
||||
97 | */ |
||||
98 | public static function has(string $name, ?string $value = null, bool $encrypt = true): bool |
||||
99 | { |
||||
100 | $getCookie = self::getCookie($name); |
||||
101 | if (!$value) { |
||||
102 | if ($getCookie) { |
||||
103 | return true; |
||||
104 | } |
||||
105 | return false; |
||||
106 | } else { |
||||
107 | if ($getCookie == ($encrypt ? self::encrypt($value) : $value)) { |
||||
108 | return true; |
||||
109 | } |
||||
110 | } |
||||
111 | return false; |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * @param string $name |
||||
116 | * @param mixed $value |
||||
117 | * @param int $minutes |
||||
118 | * @param string|null $path |
||||
119 | * @param bool $removeHas |
||||
120 | * @return bool|null |
||||
121 | */ |
||||
122 | public static function setDoesntHave( |
||||
123 | string $name, |
||||
124 | mixed $value, |
||||
125 | int $minutes, |
||||
126 | ?string $path = null, |
||||
127 | bool $removeHas = false |
||||
128 | ): ?bool { |
||||
129 | if (!self::has($name)) { |
||||
130 | return self::set($name, $value, $minutes, $path); |
||||
0 ignored issues
–
show
$path of type null|string is incompatible with the type boolean expected by parameter $secureOnly of RafaelDms\Cookie\StaticCookie::set() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
131 | } |
||||
132 | if ($removeHas) { |
||||
133 | return self::destroy($name); |
||||
134 | } |
||||
135 | return null; |
||||
136 | } |
||||
137 | |||||
138 | /** |
||||
139 | * @param string $name |
||||
140 | * @param string|null $value |
||||
141 | * @param int $expire |
||||
142 | * @param string|null $path |
||||
143 | * @param string|null $domain |
||||
144 | * @param bool $secure |
||||
145 | * @return bool |
||||
146 | */ |
||||
147 | private static function setCookie( |
||||
148 | string $name, |
||||
149 | ?string $value, |
||||
150 | int $expire, |
||||
151 | ?string $path, |
||||
152 | ?string $domain = "", |
||||
153 | bool $secure = true |
||||
154 | ): bool { |
||||
155 | return setCookie($name, $value, $expire, ($path ?? "/"), ($domain ?? ""), $secure); |
||||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * @param string $name |
||||
160 | * @return mixed |
||||
161 | */ |
||||
162 | private static function getCookie(string $name): mixed |
||||
163 | { |
||||
164 | return filter_input(INPUT_COOKIE, $name, FILTER_DEFAULT); |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * @param int $minutes |
||||
169 | * @return int |
||||
170 | */ |
||||
171 | private static function expire(int $minutes): int |
||||
172 | { |
||||
173 | return time() + (60 * $minutes); |
||||
174 | } |
||||
175 | |||||
176 | /** |
||||
177 | * @param string $value |
||||
178 | * @return string |
||||
179 | */ |
||||
180 | private static function encrypt(string $value): string |
||||
181 | { |
||||
182 | return base64_encode($value); |
||||
183 | } |
||||
184 | |||||
185 | /** |
||||
186 | * @param string $value |
||||
187 | * @return string |
||||
188 | */ |
||||
189 | private static function decrypt(string $value): string |
||||
190 | { |
||||
191 | return base64_decode($value); |
||||
192 | } |
||||
193 | |||||
194 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.