CookiesTwigExtension::setCookie()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 19
rs 9.9666

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
/**
4
 * Cookies plugin for Craft CMS
5
 *
6
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
7
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
8
 * @license   MIT License https://opensource.org/licenses/MIT
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
10
11
namespace nystudio107\cookies\twigextensions;
12
13
use nystudio107\cookies\Cookies;
14
use Twig\Extension\AbstractExtension;
15
use Twig\TwigFilter;
16
use Twig\TwigFunction;
17
18
/**
19
 * Cookies twig extension
20
 *
21
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Cookies
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.1.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
0 ignored issues
show
Coding Style introduced by
Missing @category 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...
25
class CookiesTwigExtension extends AbstractExtension
26
{
27
    /**
28
     * Return our Twig Extension name
29
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
30
    public function getName(): string
31
    {
32
        return 'Cookies';
33
    }
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @inheritdoc
37
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
38
    public function getFilters(): array
39
    {
40
        return [
41
            new TwigFilter('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
42
            new TwigFilter('getCookie', fn($name) => $this->getCookie($name)),
43
            new TwigFilter('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
44
            new TwigFilter('getSecureCookie', fn($name) => $this->getSecureCookie($name)),
45
        ];
46
    }
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @inheritdoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function getFunctions(): array
52
    {
53
        return [
54
            new TwigFunction('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
55
            new TwigFunction('getCookie', fn($name) => $this->getCookie($name)),
56
            new TwigFunction('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
57
            new TwigFunction('getSecureCookie', fn($name) => $this->getSecureCookie($name)),
58
        ];
59
    }
60
61
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expire should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $domain should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secure should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $httpOnly should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sameSite should have a doc-comment as per coding-style.
Loading history...
62
     * Set a cookie
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    public function setCookie(
65
        string $name = "",
66
        string $value = "",
67
        int    $expire = 0,
68
        string $path = "/",
69
        string $domain = "",
70
        bool   $secure = false,
71
        bool   $httpOnly = false,
72
        string $sameSite = 'Lax',
73
    ): void {
74
        Cookies::$plugin->cookies->set(
75
            $name,
76
            $value,
77
            $expire,
78
            $path,
79
            $domain,
80
            $secure,
81
            $httpOnly,
82
            $sameSite
83
        );
84
    }
85
86
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
87
     * Get a cookie
88
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
89
    public function getCookie(string $name): string
90
    {
91
        return Cookies::$plugin->cookies->get($name);
92
    }
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $expire should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $domain should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secure should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $httpOnly should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sameSite should have a doc-comment as per coding-style.
Loading history...
95
     * Set a secure cookie
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function setSecureCookie(
98
        string $name = "",
99
        string $value = "",
100
        int    $expire = 0,
101
        string $path = "/",
102
        string $domain = "",
103
        bool   $secure = false,
104
        bool   $httpOnly = false,
105
        string $sameSite = 'Lax',
106
    ): void {
107
        Cookies::$plugin->cookies->setSecure(
108
            $name,
109
            $value,
110
            $expire,
111
            $path,
112
            $domain,
113
            $secure,
114
            $httpOnly,
115
            $sameSite
116
        );
117
    }
118
119
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
120
     * Get a secure cookie
121
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
122
    public function getSecureCookie(string $name): string
123
    {
124
        return Cookies::$plugin->cookies->getSecure($name);
125
    }
126
}
127