Cookie::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 23
rs 9.9
cc 2
nc 2
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Libraries;
6
7
final class Cookie extends \WebServCo\Framework\AbstractLibrary
8
{
9
    public function get(string $name, string $defaultValue = ''): string
10
    {
11
        return $_COOKIE[$name] ?? $defaultValue;
12
    }
13
14
    public function remove(string $name): bool
15
    {
16
        if (!isset($_COOKIE[$name])) {
17
            return false;
18
        }
19
20
21
        unset($_COOKIE[$name]);
22
        $this->set($name, '', -1);
23
        return true;
24
    }
25
26
    /**
27
    * @param string $name,
28
    * @param string $value,
29
    * @param int $expires,
30
    * @param string $path,
31
    * @param string $domain,
32
    * @param bool $secure,
33
    * @param bool $httponly,
34
    * @param 'Lax'|'None'|'Strict' $samesite
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'Lax'|'None'|'Strict' at position 0 could not be parsed: Unknown type name ''Lax'' at position 0 in 'Lax'|'None'|'Strict'.
Loading history...
35
    */
36
    public function set(
37
        string $name,
38
        string $value = '',
39
        int $expires = 0,
40
        string $path = '',
41
        string $domain = '',
42
        bool $secure = true,
43
        bool $httponly = false,
44
        string $samesite = 'Lax'
45
    ): bool {
46
        if (!\in_array($samesite, ['Lax', 'None', 'Strict'], true)) {
47
            throw new \InvalidArgumentException('Invalid argument: samesite');
48
        }
49
        return \setcookie(
50
            $name,
51
            \WebServCo\Framework\Helpers\RequestHelper::sanitizeString($value),
52
            [
53
                'domain' => $domain,
54
                'expires' => $expires,
55
                'httponly' => $httponly,
56
                'path' => $path,
57
                'samesite' => $samesite,
58
                'secure' => $secure,
59
            ],
60
        );
61
    }
62
}
63