Passed
Push — master ( 878bd5...0b5a84 )
by Vsevolods
04:17
created

CookieJar::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.2963

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 12
cp 0.3333
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 7
crap 1.2963
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Http;
4
5
use DateInterval;
6
use DateTime;
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use InvalidArgumentException;
10
use Venta\Contracts\Http\Cookie as CookieContract;
11
use Venta\Contracts\Http\CookieJar as CookieJarContract;
12
13
/**
14
 * Class CookieJar
15
 *
16
 * @package Venta\Http
17
 */
18
final class CookieJar implements CookieJarContract
19
{
20
21
    /**
22
     * @var CookieContract[]
23
     */
24
    private $cookies = [];
25
26
    /**
27
     * @inheritDoc
28
     */
29 4
    public function add(
30
        string $name,
31
        string $value,
32
        $expiration,
33
        string $path = '',
34
        string $domain = '',
35
        bool $secure = false,
36
        bool $httpOnly = false
37
    ) {
38 4
        $expiration = $this->expirationToDateTime($expiration);
0 ignored issues
show
Bug introduced by
It seems like $expiration defined by $this->expirationToDateTime($expiration) on line 38 can also be of type object<DateTimeInterface>; however, Venta\Http\CookieJar::expirationToDateTime() does only seem to accept object<DateTime>|object<DateInterval>|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39 3
        $this->put(new Cookie($name, $value, $expiration, $path, $domain, $secure, $httpOnly));
40 3
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 1
    public function all(): array
46
    {
47 1
        return $this->cookies;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 5
    public function findByName(string $name)
54
    {
55 5
        return $this->cookies[$name] ?? null;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function forever(
62
        string $name,
63
        string $value = null,
64
        string $path = '',
65
        string $domain = '',
66
        bool $secure = false,
67
        bool $httpOnly = false
68
    ) {
69 1
        $this->add($name, $value, (new DateTime())->add(new DateInterval('P10Y')), $path, $domain, $secure, $httpOnly);
70 1
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 1
    public function forget(string $name)
76
    {
77 1
        $this->add($name, '', (new DateTime())->setTimestamp(1));
78 1
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 6
    public function put(CookieContract $cookie)
84
    {
85 6
        $this->cookies[$cookie->name()] = $cookie;
86 6
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 1
    public function session(
92
        string $name,
93
        string $value,
94
        string $path = '',
95
        string $domain = '',
96
        bool $secure = false,
97
        bool $httpOnly = false
98
    ) {
99 1
        $this->put(new Cookie($name, $value, null, $path, $domain, $secure, $httpOnly));
100 1
    }
101
102
    /**
103
     * Parses expiration time and returns valid DateTimeInterface implementation.
104
     *
105
     * @param DateTime|DateInterval|string $expires
106
     * @return DateTimeInterface
107
     * @throws InvalidArgumentException
108
     */
109 4
    private function expirationToDateTime($expires): DateTimeInterface
110
    {
111 4
        if ($expires instanceof DateInterval) {
112 1
            $expires = (new DateTime)->add($expires);
113 4
        } elseif (is_string($expires) || is_int($expires)) {
114 2
            $expires = new DateTime(is_numeric($expires) ? "@$expires" : $expires);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expires instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
115
        }
116
117 4
        if (!$expires instanceof DateTimeInterface) {
118 1
            throw new InvalidArgumentException(
119 1
                "Invalid cookie expiration time. Cannot be converted to DateTimeInterface."
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Invalid cookie expiratio...d to DateTimeInterface. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
120
            );
121
        }
122
123 3
        return DateTimeImmutable::createFromMutable($expires);
124
    }
125
126
}