Issues (480)

src/Cookie/Config/HttpCookies.php (12 issues)

1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Cookie\Config;
7
8
use Mvc5\Cookie\Cookies;
9
10
use function is_array;
11
use function is_string;
12
use function key;
13
14
use const Mvc5\{ COOKIE_EXPIRE_TIME, DOMAIN, EXPIRES, HTTP_ONLY, NAME, OPTIONS, PATH, SAMESITE, SECURE, VALUE };
0 ignored issues
show
The type Mvc5\OPTIONS was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\VALUE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\HTTP_ONLY was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\EXPIRES was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\NAME was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\SECURE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\PATH was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\SAMESITE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Mvc5\COOKIE_EXPIRE_TIME was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
trait HttpCookies
17
{
18
    /**
19
     * @var array
20
     */
21
    protected array $config = [];
22
23
    /**
24
     * @param array $cookies
25
     */
26 175
    function __construct(array $cookies = [])
27
    {
28 175
        $this->config = $cookies;
29 175
    }
30
31
    /**
32
     * @return array
33
     */
34 2
    function all() : array
35
    {
36 2
        return $this->config;
37
    }
38
39
    /**
40
     * @param array|string $name
41
     * @param array $options
42
     */
43 12
    function remove($name, array $options = []) : void
44
    {
45 12
        $this->set(expire(cookie(is_string($name) ? [NAME => $name] + $options : $name)));
46 10
    }
47
48
    /**
49
     * @param array|string $name
50
     * @param string|null $value
51
     * @param array $options
52
     * @return mixed
53
     */
54 12
    function set($name, $value = null, array $options = [])
55
    {
56 12
        if (is_array($name)) {
57 10
            $cookie = cookie($name);
58
59 10
            $this->config[$cookie[NAME]] = $cookie;
60
61 9
            return $name;
62
        }
63
64 2
        $this->config[$name] = [NAME => (string) $name, VALUE => (string) $value] + $options;
65
66 2
        return $value;
67
    }
68
69
    /**
70
     * @param array|string $name
71
     * @param string|null $value
72
     * @param array $options
73
     * @return Cookies|mixed
74
     */
75 6
    function with($name, $value = null, array $options = []) : Cookies
76
    {
77 6
        $new = clone $this;
78 6
        $new->set($name, $value, $options);
79 6
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $new returns the type Mvc5\Cookie\Config\HttpCookies which is incompatible with the type-hinted return Mvc5\Cookie\Cookies.
Loading history...
80
    }
81
82
    /**
83
     * @param array|string $name
84
     * @param array $options
85
     * @return Cookies|mixed
86
     */
87 7
    function without($name, array $options = []) : Cookies
88
    {
89 7
        $new = clone $this;
90 7
        $new->remove($name, $options);
91 5
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $new returns the type Mvc5\Cookie\Config\HttpCookies which is incompatible with the type-hinted return Mvc5\Cookie\Cookies.
Loading history...
92
    }
93
}
94
95
/**
96
 * @param array $cookie
97
 * @return array
98
 */
99
function cookie(array $cookie) : array
100
{
101 71
    return is_string(key($cookie)) ? $cookie : [
102 71
        NAME => $cookie[0],
103 6
        VALUE => $cookie[1] ?? null,
104 6
        EXPIRES => $cookie[2] ?? null,
105 6
        PATH => $cookie[3] ?? null,
106 6
        DOMAIN => $cookie[4] ?? null,
107 6
        SECURE => $cookie[5] ?? null,
108 6
        HTTP_ONLY => $cookie[6] ?? null,
109 6
        SAMESITE => $cookie[7] ?? null
110
    ];
111
}
112
113
/**
114
 * @param array $cookie
115
 * @return array
116
 */
117
function expire(array $cookie) : array
118
{
119 53
    $cookie[VALUE] = '';
120
121 53
    isset($cookie[OPTIONS]) ? $cookie[OPTIONS][EXPIRES] = COOKIE_EXPIRE_TIME : $cookie[EXPIRES] = COOKIE_EXPIRE_TIME;
122
123 53
    return $cookie;
124
}
125