Issues (181)

src/services/CookiesService.php (43 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\services;
12
13
use Craft;
14
use craft\base\Component;
15
use yii\base\Exception;
16
use yii\base\InvalidConfigException;
17
use yii\web\Cookie;
18
19
/**
20
 * Cookies service
21
 *
22
 * @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...
23
 * @package   Cookies
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @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...
25
 */
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...
26
class CookiesService extends Component
27
{
28
    /**
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...
29
     * Set a cookie
30
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
31
    public function set(
32
        string $name = '',
33
        string $value = '',
34
        int    $expire = 0,
35
        string $path = '/',
36
        string $domain = '',
37
        bool   $secure = false,
38
        bool   $httpOnly = false,
39
        string $sameSite = 'Lax',
40
    ): void {
41
        if (empty($value)) {
42
            Craft::$app->response->cookies->remove($name);
43
        } else {
44
            $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain;
45
            if (PHP_VERSION_ID >= 70300) {
46
                setcookie($name, $value, [
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
47
                    'expires' => $expire,
48
                    'path' => $path,
49
                    'domain' => $domain,
50
                    'secure' => $secure,
51
                    'httponly' => $httpOnly,
52
                    'samesite' => $sameSite,
53
                ]);
0 ignored issues
show
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
54
            } else {
55
                setcookie($name, $value, ['expires' => $expire, 'path' => $path, 'domain' => $domain, 'secure' => $secure, 'httponly' => $httpOnly]);
56
            }
57
58
            $_COOKIE[$name] = $value;
59
        }
60
    }
61
62
    /**
0 ignored issues
show
Parameter $name should have a doc-comment as per coding-style.
Loading history...
63
     * Get a cookie
64
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
65
    public function get(string $name = ''): string
66
    {
67
        return $_COOKIE[$name] ?? '';
68
    }
69
70
    /**
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...
71
     * Set a secure cookie
72
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
73
    public function setSecure(
74
        string $name = '',
75
        string $value = '',
76
        int    $expire = 0,
77
        string $path = '/',
78
        string $domain = '',
79
        bool   $secure = false,
80
        bool   $httpOnly = false,
81
        string $sameSite = 'Lax',
82
    ): void {
83
        if (empty($value)) {
84
            Craft::$app->response->cookies->remove($name);
85
        } else {
86
            $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain;
87
            $cookie = new Cookie(['name' => $name, 'value' => '']);
88
89
            try {
90
                $cookie->value = Craft::$app->security->hashData(base64_encode(serialize($value)));
0 ignored issues
show
The call to yii\base\Security::hashData() has too few arguments starting with key. ( Ignorable by Annotation )

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

90
                /** @scrutinizer ignore-call */ 
91
                $cookie->value = Craft::$app->security->hashData(base64_encode(serialize($value)));

This check compares calls to functions or methods with their respective definitions. If the call has less 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.

Loading history...
91
            } catch (InvalidConfigException|Exception $e) {
92
                Craft::error(
93
                    'Error setting secure cookie: ' . $e->getMessage(),
94
                    __METHOD__
95
                );
96
97
                return;
98
            }
99
100
            $cookie->expire = $expire;
101
            $cookie->path = $path;
102
            $cookie->domain = $domain;
103
            $cookie->secure = $secure;
104
            $cookie->httpOnly = $httpOnly;
105
            if (PHP_VERSION_ID >= 70300) {
106
                $cookie->sameSite = $sameSite;
107
            }
108
109
            Craft::$app->response->cookies->add($cookie);
110
        }
111
    }
112
113
    /**
0 ignored issues
show
Parameter $name should have a doc-comment as per coding-style.
Loading history...
114
     * Get a secure cookie
115
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
116
    public function getSecure(string $name = ''): string
117
    {
118
        $result = '';
119
        $cookie = Craft::$app->request->cookies->get($name);
120
        if ($cookie !== null) {
121
            try {
122
                $data = Craft::$app->security->validateData($cookie->value);
0 ignored issues
show
The call to yii\base\Security::validateData() has too few arguments starting with key. ( Ignorable by Annotation )

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

122
                /** @scrutinizer ignore-call */ 
123
                $data = Craft::$app->security->validateData($cookie->value);

This check compares calls to functions or methods with their respective definitions. If the call has less 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.

Loading history...
123
            } catch (InvalidConfigException|Exception $e) {
124
                Craft::error(
125
                    'Error getting secure cookie: ' . $e->getMessage(),
126
                    __METHOD__
127
                );
128
                $data = false;
129
            }
130
131
            if (
0 ignored issues
show
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
132
                !empty($cookie->value)
0 ignored issues
show
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
133
                && $data !== false
134
            ) {
135
                $result = unserialize(base64_decode($data), ['allowed_classes' => false]);
136
            }
137
        }
138
139
        return $result;
140
    }
141
}
142