Issues (181)

src/twigextensions/CookiesTwigExtension.php (42 issues)

1
<?php
2
3
/**
4
 * Cookies plugin for Craft CMS
5
 *
6
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
7
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
@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
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
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
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Cookies
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.1.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
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
Missing @return tag in function comment
Loading history...
30
    public function getName(): string
31
    {
32
        return 'Cookies';
33
    }
34
35
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
36
     * @inheritdoc
37
     */
0 ignored issues
show
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
Missing short description in doc comment
Loading history...
49
     * @inheritdoc
50
     */
0 ignored issues
show
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
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $expire should have a doc-comment as per coding-style.
Loading history...
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Parameter $domain should have a doc-comment as per coding-style.
Loading history...
Parameter $secure should have a doc-comment as per coding-style.
Loading history...
Parameter $httpOnly should have a doc-comment as per coding-style.
Loading history...
Parameter $sameSite should have a doc-comment as per coding-style.
Loading history...
62
     * Set a cookie
63
     */
0 ignored issues
show
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
Parameter $name should have a doc-comment as per coding-style.
Loading history...
87
     * Get a cookie
88
     */
0 ignored issues
show
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
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $expire should have a doc-comment as per coding-style.
Loading history...
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Parameter $domain should have a doc-comment as per coding-style.
Loading history...
Parameter $secure should have a doc-comment as per coding-style.
Loading history...
Parameter $httpOnly should have a doc-comment as per coding-style.
Loading history...
Parameter $sameSite should have a doc-comment as per coding-style.
Loading history...
95
     * Set a secure cookie
96
     */
0 ignored issues
show
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
Parameter $name should have a doc-comment as per coding-style.
Loading history...
120
     * Get a secure cookie
121
     */
0 ignored issues
show
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